@startupjs/worker
Worker to perform specified actions
Last updated 2 days ago by yska .
MIT · Original npm · Tarball · package.json
$ cnpm install @startupjs/worker 
SYNC missed versions from official npm registry.

@startupjs/worker

Worker to perform specified actions

Usage

  1. In project root create folder worker and create index.mjs:

    import { TaskDispatcher }  from '@startupjs/worker'
    import path from 'path'
    
    // full path to workerActions.mjs and workerInit.mjs
    // by default 'worker/workerActions.mjs'
    process.env.WORKER_ACTIONS_PATH = path.join(process.cwd(), './workerActions.mjs') 
    // by default worker/initWorker.mjs
    process.env.WORKER_INIT_PATH = path.join(process.cwd(), './workerInit.mjs') 
        
    const dispatcher = new TaskDispatcher()
    
    dispatcher.start().catch((err) => {
      console.log('Error starting worker', err)
    })
    

    N There is need to create init and action file with extention .mjs. This is necessary for node to treat these files as modules.

  2. In folder worker create workerInit.mjs. Do any initializations here (plug in hooks, ORM, etc.):

    export let init = global.DM_WORKER_INIT = function (backend) {
      // do initializations here
    }
    
  3. In folder worker create workerActions.mjs. Put your tasks here (name of functions are the type of tasks):

    export let ACTIONS = {
      test: (model, task, done) {
        console.log('>> Start test task', task.id)
        setTimeout(() => {
          console.log('>> Finish test task', task.id)
          done()
        }, 5000)
      }
    }
    
  4. Run in console:

      cd worker && node index.mjs
    

Defaults

Worker has default parameters. These values ​​can be changed if necessary in index.mjs by specifying them in process.env.

WORKER_CHILDREN_NUM: '2', // workers amount in TaskDispatcher 
WORKER_TASK_DEFAULT_TIMEOUT: '30000', // time on execute task
WORKER_THROTTLE_TIMEOUT: '3000', // time on execute task
WORKER_TASK_COLLECTION: 'tasks', // task collection name
WORKER_MONGO_QUERY_TIMEOUT: '600', // mongo lock time
WORKER_MONGO_QUERY_INTERVAL: '200', // timeout for Mongo requests
WORKER_MONGO_QUERY_LIMIT: '100', // number of tasks in one request
WORKER_REDIS_QUEUE_INTERVAL: '100', // timeout for Redis requests
MONGO_URL: 'mongodb://localhost:27017/tasks',
REDIS_URL: 'redis://localhost:6379/0',
WORKER_ACTIONS_PATH: path.join(process.cwd(), './workerActions.mjs'),
WORKER_INIT_PATH: path.join(process.cwd(), './initWorker.mjs')

MIT Licence

Copyright (c) 2020 Decision Mapper

Current Tags

  • 0.29.0                                ...           latest (2 days ago)

8 Versions

  • 0.29.0                                ...           2 days ago
  • 0.28.0                                ...           17 days ago
  • 0.27.0                                ...           19 days ago
  • 0.26.0                                ...           a month ago
  • 0.25.0                                ...           a month ago
  • 0.24.0                                ...           a month ago
  • 0.23.45                                ...           2 months ago
  • 0.23.44                                ...           2 months ago
Downloads
Today 0
This Week 9
This Month 27
Last Day 1
Last Week 0
Last Month 27
Dependencies (4)
Dev Dependencies (5)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |