Options
All
  • Public
  • Public/Protected
  • All
Menu

Class QueueLoop

Introduction

QueueLoop allowed you do jobs by sequence and repeat them just one by one till the loop was stopped by invoke QueueLoop.stop

Example code

We add three jobs to the queue and start processing.

import { QueueLoop, TimeDuration } from 'noqueue';

const iQueueLoop = new QueueLoop();

iQueueLoop
  .add(
    'job-1',
    async () => {
      return ['job-1-result'];
    },
    TimeDuration.fromSecond(2), // After job-1 is done, wait for 2 seconds
  )
  .add(
    'job-2',
    async (job1Result: string) => {
      console.log('Processing job-1 with input params:', { job1Result });
    },
    TimeDuration.fromSecond(10), // After job-1 is done, wait for 10 seconds
  )
  .add(
    'job-3',
    async () => {
      throw new Error('This an annoying error');
    },
    TimeDuration.fromSecond(2), // After job-3 is done, wait for 2 seconds
  );

// Listen on success event
iQueueLoop.on('success', (eventName, ...params: any[]) => {
  console.log(`Event ${eventName} completed with result:`, params);
});

// Listen on error event
iQueueLoop.on('error', (eventName, error) => {
  console.log(`Event ${eventName} failed with error:`, error);
});

iQueueLoop.start();

Here is the result:

Event job-1 completed with result: [ 'job-1-result' ]
Processing job-1 with input params: { job1Result: 'job-1-result' }
Event job-2 completed with result: [ undefined ]
Event job-3 failed with error: Error: This an annoying error
    at testQueue.add.add.add (/home/user/gits/noqueue/test4.ts:23:13)
    at QueueLoop.worker (/home/user/gits/noqueue/src/queue-loop.ts:195:5)
    at Timeout.handler.setTimeout [as _onTimeout] (/home/user/gits/noqueue/src/queue-loop.ts:210:20)
    at ontimeout (timers.js:436:11)
    at tryOnTimeout (timers.js:300:5)
    at listOnTimeout (timers.js:263:5)
    at Timer.processTimers (timers.js:223:10)

Even there are an error, it won't break the loop. QueueLoop is just keep moving on.

export

Hierarchy

Index

Constructors

constructor

  • Creates an instance of QueueLoop.

    memberof

    QueueLoop

    Parameters

    • Optional conf: Partial<IConfiguration>

      Configuration that used to overwrite default configurations

    Returns QueueLoop

Events

Static error

error: string = "error"

Event will be emitted after one job is failed.

static
memberof

QueueLoop

Static success

success: string = "success"

Event will be emitted after one job is completed.

static
memberof

QueueLoop

Methods

add

  • Add job into queue and await for processing

    memberof

    QueueLoop

    Parameters

    • name: string

      Name of job

    • func: IQueueFunction

      Job will be added to queue

    • Default value paddingTimeTime: TimeDuration = new TimeDuration()

    Returns QueueLoop

catch

emit

eventNames

  • eventNames(): string[]

getCurrentJob

  • getCurrentJob(): string
  • Get current working job

    memberof

    QueueLoop

    Returns string

getJobs

  • getJobs(): string[]
  • Get list of job's name

    memberof

    QueueLoop

    Returns string[]

on

once

reject

remove

  • remove(name: string): boolean
  • Remove one job from queue by given name

    memberof

    QueueLoop

    Parameters

    • name: string

      Remove job from queue

    Returns boolean

removeAllListeners

resolve

start

  • start(): boolean

stop

  • stop(): boolean

then

Generated using TypeDoc