npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

backend-store-tasks

v0.1.1

Published

Background tasks plugin for [backend-store](https://github.com/alekbarszczewski/backend-store). It uses [bull](https://github.com/OptimalBits/bull) under the hood.

Downloads

3

Readme

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.