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

@forrestjs/service-fetchq-task

v5.2.2

Published

Simple API to run singleton tasks over a Fetchq queue.

Downloads

2,118

Readme

Fetchq Task

Let you add singleton tasks to a Fetchq queue.

👉 Each task gets executed by one single worker at the time, no matter the horizontal scalability of the queue.

You keep scaling the associated workers as so to run different tasks in parallel.

This is suitable for running stuff akin to a CRON Job.

Configuration

forrest.run({
  settings: {
    fetchq: {
      task: {
        // Register tasks at config time:
        // (see "Add Tasks" paragraph for details)
        register: [
          {
            subject: 'foobar',
            handler: (doc) => doc.reschedule('+1m')
          }
        ],

        queue: {
          // Customize the queue name:
          name: 'foobar',

          // Fine tune the queue performances:
          // https://github.com/fetchq/node-client#queues-configuration
          settings: {}
        },
        
        worker: {
          // Fine tune the worker performances:
          // https://github.com/fetchq/node-client#workers-configuration
          settings: {}
        }
      }
    }
  }
})

Add Tasks

As configuration:

forrest.run({
  settings: {
    fetchq: {
      task: {
        register: [
          {
            // Document in the tasks' queue:
            subject: "cqrs-todos",
            payload: { target: "todos" },
            // Worker for this specific task:
            handler: (doc, ctx) => {
              console.log("cqrs-todos", doc.payload);
              return doc.reschedule("+1s");
            }
          }
        ]
      }
    }
  }
})

As an extension:

// Declarative form:
// you can return one single task, or an array of tasks
const myFeature = () => [
  {
    target: "$FETCHQ_REGISTER_TASK",
    handler: {
      // Document in the tasks' queue:
      subject: "cqrs-todos",
      payload: { target: "todos" },
      // Worker for this specific task:
      handler: (doc, ctx) => {
        console.log("cqrs-todos", doc.payload);
        return doc.reschedule("+1s");
      }
    }
  }
];

// Functional form:
// you can return one single task, or an array of tasks
const myFeature = () => [
  {
    target: "$FETCHQ_REGISTER_TASK",
    handler: [
      {
        // Document in the tasks' queue:
        subject: "cqrs-todos",
        payload: { target: "todos" },
        // Worker for this specific task:
        handler: (doc, ctx) => {
          console.log("cqrs-todos", doc.payload);
          return doc.reschedule("+1s");
        }
      },
      {
        subject: 'foobar',
        handler: d => d.complete()
      }
    ]
  }
];

Task Configuration

subject and handler are mandatory.

subject

type: String

payload

type: Object

firstIteration

type: Time (absolute or relative)

Delay the first execution of the task.

{
  firstIteration: '+1h',
  firstIteration: '1970-01-01 10:22',
}

nextIteration

type: Time (absolute or relative)

If provided, it schedules the task for a next execution when the handler completes returning undefined.

{
  firstIteration: '+1h',
  firstIteration: '1970-01-01 10:22',
}

handler

type: Function args: doc, ctx

Provide the logic to perform for the task.

👉 Refer to the Fetchq documentation for details on the arguments and returning value.

The hander can return a valid Fetchq Action, or simply skip returning.

In case of returning undefined, the task will be rescheduled according to the nextIteration setting.

In case nextIteration was not provided, the task will be marked as completed (single execution mode).

resetOnBoot

type: Boolean

Set it to true and the task will be completely reset at boot time.

APIs

Run a Task

You can programmatically run any task immediately:

const run = getContext('fetchq.task.run');
await run('taskSubject', 'log info message')

The log message is optional.

Reset a Task

You can programmatically reset any task to its original state:

const reset = getContext('fetchq.task.reset');
await reset('taskSubject', 'log info message')

The log message is optional.