Skip to content

Documentation / @andrew_l/app / defineWorker

Function: defineWorker() ​

defineWorker<P, S, M, C>(definition): WorkerDefinition<P, S, M, C>

Define a background worker with a pluggable execution strategy. Returns a WorkerDefinition that can be run with createWorkerInstance + startApp.

Type Parameters ​

P ​

P extends ObjectPropsOptions<Data>

S ​

S extends Record<string, any>

M ​

M extends Record<string, AnyFunction>

C ​

C extends WorkerStrategy<Context> = WorkerStrategy<Context>

Parameters ​

definition ​

WorkerDefinition<P, S, M, C>

Returns ​

WorkerDefinition<P, S, M, C>

Example ​

ts
import { defineWorker, IntervalStrategy } from '@andrew_l/app';

export default defineWorker({
  name: 'clock',
  executeStrategy: new IntervalStrategy({
    intervalSeconds: 1,
  }),
  entry() {
    this.log.info('tick=%d', this.timerSequence);
  },
  async shutdown() {
    await delay(1000);
  },
});