1.1.3 • Published 5 years ago

chat-app-workers v1.1.3

Weekly downloads
3
License
ISC
Repository
github
Last release
5 years ago

CHAT APP WORKERS

This app uses bull.js with is Redis-based queue for Node.

STRUCTURE

The main goal is to communicating with external services (slack, autopilot, etc...). This lead to specific structure, made of different queues specific for each service. To call it just return object with queue value equal to corespondent queue name and data as object with method and params.

How to use

Facing all over again same issues we decide to put all hard logic into sequence factory. This allow to build speed up the process of adding new jobs. Let's take a look into this:

CLI

At first install cli:

npm i chat-app-workers-cli -g

and then call generate-workers name, where name is desire name of directory to be created and follow up with selecting type of your new queue.

File structure

generate-workers command will create a directory with 3 files. You need to interact with all of it. At first set name for queue in index.js file. This should be imported from @autopilot/chat-app-core. You don't need to worry about importing it. It'll be done automatically. Then, modify you eventListener.js file if you need any event to catch. Then you are ready to go and write main content inside `processors.js' file.

The processors.js file should be structure this way:

module.exports = {
    // __default__ is key word to name first function to call in queue.
    // All functions takes two arguments, _job_  and _app_ if needed.
    __default__: (job, app) => {
      return {
        queue: queues.slack.name, // if queue is set it will add job with job.data to that queue.
        // Otherwise it will try to add job to self (if nextStep is set)
        params: {
          method: 'chat.postMessage', // method that will be called in queue, with params 
          params: { token: 'xxx' }
        },
        extraData: {
          name: 'name' // this will extend job.data that will be passed to next step (if nextStep is set)
        },
        nextStep: 'secondStep' // next method in THIS queue that will be called
      }
    },
    secondStep: (job) => {
      console.log(job.data) // will have name === 'name' as we passed it in previous extraData value.
      const { res } = job.data // res from external will be passed to job.data in res key.

      return {
        queue: queues.slack.name, // if queue is set it will add job with job.data to that queue.
        // Otherwise it will try to add job to self (if nextStep is set)
        params: {
          method: 'chat.delete', // method that will be called in queue, with params 
          params: {
            token: 'xxx',
            channel: '1',
            ts: '123'
          },
        }
      }
    //this will be last step as nextStep is not set
    }
}