backend-store-tasks

0.1.1 • Public • Published

backend-store-tasks

Background tasks plugin for backend-store. It uses bull under the hood.

Install

$ yarn add backend-store-tasks

Usage

// store.js

import { Store } from 'backend-store'
import backendStoreTasks from 'backend-store-tasks'

const store = new Store()

store.plugin(backendStoreTasks, {
  redisUrl: process.env.REDIS_URL
})

store.define('myApi', async (payload, methodContext) => {
  const { createTask, context } = methodContext
  // you can create task from inside of any store method, context will be passed to task automatically
  await createTask('myTask', { some: 'payload' })
})

// define background task as normal store method
store.define('myTask', async (payload, methodContext) => {
  const { context } = methodContext
  // context is same as "received" by myApi method (it is passed to background task automatically)
  // payload is { some: 'payload' }
})
// worker.js

import store from './store'

store.processTasks('*')
// OR store.processTasks('myTask')
// OR store.processTasks('*', { concurrency: 10 })
// cron.js

import store from './store'

setInterval(async () => {
  // you can create tasks by using store.createTask directly
  await store.createTask('myTask', { another: 'payload' }, { custom: 'context' })
}, 10 * 1000)

API

Store.plugin(backendStoreTasks, options)

argument Description
options.redisUrl (required) Redis URL (for example redis://redis:pass@localhost)
options.queueOptions options passed to bull Queue (see options here)
options.defaultJobOptions default options used when creating bull job (see below)
options.queueName defaults to "default_queue"

options.defaultJobOptions

Default job options are as follows:

{
  attempts: 3,
  timeout: 60 * 1000, // 1 minute
  removeOnComplete: true,
  removeOnFail: true
}

You can override them with options.defaultJobOptions.
All available options are here.


Store#createTask (method, payload, context, options) => Promise<void>

Create background task. It takes same options as Store#dispatch method and additionally it supports options.jobOptions (see below).

argument Description
method (required) method name
payload method payload
context context
options.cid same as cid option passed to Store#dispatch
options.jobOptions bull job options (see all available options)
options.transformContext optional function to transform context passed to task (see below)

options.transformContext

Function of type (context: any) => any. When this option is set this function is used to transform context before saving task to Redis. This is useful if context is not serializable, for example it has circullar dependency. If for example you want to pass only user to background tasks use it like this:

store.plugin(backendStoreTasks, {
  redisUrl: process.env.REDIS_URL,

  transformContext (context) {
    // pick only "user" from context
    return context
      ? { user: context.user || null }
      : context
  }
})

Returns promise which is resolved as soon as task is saved to Redis.


Store#processTasks(taskName, options) => void

Starts listening and processing of tasks of given type (type is actually method name).

argument Description
taskName (required) method name or "*" to process all tasks
options.concurrency defaults to 1

Store#stopProcessingTasks() => Promise<void>

Closes Redis connection used by bull. Useful for graceful shutdown.
Returns promise that resolves when connection is closed.


methodContext#createTask (method, payload, options) => Promise<void>

Create background task. It takes same options as methodContext#dispatch method and additionally it supports options.jobOptions (see below).

argument Description
method (required) method name
payload method payload
options.jobOptions bull job options (see all available options)

Returns promise which is resolved as soon as task is saved to Redis.


Readme

Keywords

none

Package Sidebar

Install

npm i backend-store-tasks

Weekly Downloads

0

Version

0.1.1

License

MIT

Unpacked Size

112 kB

Total Files

6

Last publish

Collaborators

  • alekbe