4.7.4 • Published 1 year ago

dm-worker v4.7.4

Weekly downloads
84
License
ISC
Repository
-
Last release
1 year ago

dm-worker

Worker

Usage

  1. In project root create worker.js:

    const conf = require('nconf')
    require('dm-sharedb-server/nconf')
    
    const path = require('path')
    
    // full path to workerActions.js and workerInit.js
    process.env['WORKER_ACTIONS_PATH'] = path.join(process.cwd(), './build/workerActions.js')
    process.env['WORKER_INIT_PATH'] = path.join(process.cwd(), './build/workerInit.js')
    
    const TaskDispatcher = require('dm-worker')
    const dispatcher = new TaskDispatcher()
    
    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:

    ```js
    import 'dm-sharedb-server/nconf'
    import ShareDB from 'sharedb'
    import richText from 'rich-text'
    import Racer from 'racer'
    import derbyAr from 'derby-ar'
    import ormEntities from './model'
    import shareDbHooks from 'sharedb-hooks'
    import hooks from './server/hooks'
    
    let init = global.DM_WORKER_INIT = function (backend) {
      // Register rich-text type in ShareDB
      ShareDB.types.register(richText.type)
    
      // Init ORM
      Racer.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:

    ```js
    let ACTIONS = 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) 2022 Decision Mapper

4.7.4

1 year ago

4.7.3

2 years ago

4.7.2

2 years ago

4.7.1

2 years ago

4.6.7

2 years ago

4.6.8

2 years ago

4.7.0

2 years ago

4.6.6

2 years ago

4.6.5

3 years ago

4.6.4

3 years ago

4.6.3

5 years ago

4.6.2

5 years ago

4.6.1

5 years ago

4.6.0

5 years ago

4.5.2

5 years ago

4.5.1

5 years ago

4.5.0

5 years ago

4.4.2

5 years ago

4.4.1

5 years ago

4.4.0

5 years ago

4.3.1

6 years ago

4.3.0

6 years ago

4.2.2

6 years ago

4.2.1

6 years ago

4.2.0

6 years ago

2.2.5

6 years ago

2.2.4

6 years ago

2.2.3

6 years ago

4.1.1

6 years ago

2.2.2

6 years ago

4.1.0

7 years ago

4.0.2

7 years ago

4.0.1

7 years ago

4.0.0

7 years ago

3.0.6

7 years ago

3.0.5

7 years ago

3.0.4

7 years ago

3.0.3

7 years ago

3.0.2

7 years ago

3.0.1

7 years ago

3.0.0

7 years ago

2.2.1

7 years ago

2.2.0

7 years ago

2.1.1

7 years ago

2.1.0

7 years ago

2.0.0

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago