@startupjs/worker v0.55.12
@startupjs/worker
Worker to perform specified actions
Usage
In project root create folder
worker
and createindex.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 asmodules
.In folder
worker
createworkerInit.mjs
. Do any initializations here (plug in hooks, ORM, etc.):export let init = global.DM_WORKER_INIT = function (backend) { // do initializations here }
In folder
worker
createworkerActions.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) } }
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
1 month ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
4 months ago
4 months ago
4 months ago
4 months ago
5 months ago
5 months ago
6 months ago
7 months ago
6 months ago
7 months ago
9 months ago
6 months ago
6 months ago
7 months ago
7 months ago
12 months ago
12 months ago
12 months ago
1 year ago
1 year ago
1 year ago
2 years ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago