0.1.1 • Published 6 years ago

backend-store-tasks v0.1.1

Weekly downloads
2
License
MIT
Repository
github
Last release
6 years ago

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)

argumentDescription
options.redisUrl (required)Redis URL (for example redis://redis:pass@localhost)
options.queueOptionsoptions passed to bull Queue (see options here)
options.defaultJobOptionsdefault options used when creating bull job (see below)
options.queueNamedefaults 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).

argumentDescription
method (required)method name
payloadmethod payload
contextcontext
options.cidsame as cid option passed to Store#dispatch
options.jobOptionsbull job options (see all available options)
options.transformContextoptional 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).

argumentDescription
taskName (required)method name or "*" to process all tasks
options.concurrencydefaults 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).

argumentDescription
method (required)method name
payloadmethod payload
options.jobOptionsbull job options (see all available options)

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