5.2.2 ā€¢ Published 2 months ago

@forrestjs/service-fetchq-task v5.2.2

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

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.

5.2.2

2 months ago

5.1.1

1 year ago

5.1.0

1 year ago

5.0.6

1 year ago

5.0.5

1 year ago

5.0.4

1 year ago

5.2.1

1 year ago

5.0.3

1 year ago

5.2.0

1 year ago

5.0.2

1 year ago

5.0.1

1 year ago

5.0.0-alpha.39

1 year ago

5.0.0-alpha.37

1 year ago

5.0.0-alpha.38

1 year ago

5.0.0-alpha.35

1 year ago

5.0.0-alpha.36

1 year ago

5.0.0-alpha.33

1 year ago

5.0.0-alpha.34

1 year ago

5.0.0-alpha.40

1 year ago

5.0.0-alpha.31

2 years ago

5.0.0-alpha.32

2 years ago

5.0.0-alpha.30

2 years ago

5.0.0-alpha.28

2 years ago

5.0.0-alpha.29

2 years ago

5.0.0-alpha.26

2 years ago

5.0.0-alpha.27

2 years ago

5.0.0-alpha.25

2 years ago

5.0.0-alpha.9

2 years ago

5.0.0-alpha.19

2 years ago

5.0.0-alpha.17

2 years ago

5.0.0-alpha.18

2 years ago

5.0.0-alpha.15

2 years ago

5.0.0-alpha.16

2 years ago

5.0.0-alpha.13

2 years ago

5.0.0-alpha.14

2 years ago

5.0.0-alpha.11

2 years ago

5.0.0-alpha.12

2 years ago

5.0.0-y.0

2 years ago

5.0.0-alpha.10

2 years ago

5.0.0-alpha.24

2 years ago

5.0.0-alpha.22

2 years ago

5.0.0-alpha.23

2 years ago

5.0.0-alpha.20

2 years ago

5.0.0-alpha.21

2 years ago

5.0.0-alpha.8

2 years ago

5.0.0-alpha.7

2 years ago

5.0.0-alpha.6

2 years ago

5.0.0-alpha.5

2 years ago

5.0.0-alpha.3

2 years ago

5.0.0-alpha.2

2 years ago

5.0.0-alpha.1

2 years ago

5.0.0-alpha.0

2 years ago

4.7.2

2 years ago

4.7.1

2 years ago

4.7.1-alpha.1

2 years ago

4.7.1-alpha.0

2 years ago

4.7.0

2 years ago