Skip to content

Latest commit

History

98 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

dm-worker

Worker

Usage

  1. In project root create worker.js:

    constconf=require('nconf')require('dm-sharedb-server/nconf')constpath=require('path')// full path to workerActions.js and workerInit.jsprocess.env['WORKER_ACTIONS_PATH']=path.join(process.cwd(),'./build/workerActions.js')process.env['WORKER_INIT_PATH']=path.join(process.cwd(),'./build/workerInit.js')constTaskDispatcher=require('dm-worker')constdispatcher=newTaskDispatcher()dispatcher.start().catch((err)=>{console.log('Error starting worker',err)})
  2. In project root create workerInit.js. Do any initializations here (plug in hooks, ORM, etc.). Since this file may be compiled by webpack, use global.DM_WORKER_INIT instead of module.exports:

    import'dm-sharedb-server/nconf'importShareDBfrom'sharedb'importrichTextfrom'rich-text'importRacerfrom'racer'importderbyArfrom'derby-ar'importormEntitiesfrom'./model'importshareDbHooksfrom'sharedb-hooks'importhooksfrom'./server/hooks'letinit=global.DM_WORKER_INIT=function(backend){// Register rich-text type in ShareDBShareDB.types.register(richText.type)// Init ORMRacer.use(derbyAr)Racer.use(ormEntities)shareDbHooks(backend)hooks(backend)}
  3. In project root create workerActions.js. Put your tasks here (name of functions are the name of tasks). Since this file may be compiled by webpack, use global.DM_WORKER_ACTIONS instead of module.exports:

    letACTIONS=global.DM_WORKER_ACTIONS={}ACTIONS.test=function(model,task,done){console.log('>> Start test task',task.id)setTimeout(()=>{console.log('>> Finish test task',task.id)done()},5000)}
  4. Add worker, workerInit, workerActions to webpack.config.js of dm-react-webpack:

    module.exports={// ...backendApps: {server: path.join(__dirname,'server'),worker: path.join(__dirname,'worker'),workerActions: path.join(__dirname,'workerActions'),workerInit: path.join(__dirname,'workerInit')}// ... }

MIT Licence

Copyright (c) 2019 Decision Mapper

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

14 watching

Forks

Releases

Packages

Used by

Contributors

Languages