Skip to content

librark/schedulark

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

53 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Schedulark

Job Scheduling Library

Usage

First, you should define your jobs so that they can be registered, referenced and dispatched by your application. A Job is an object with an execute(context) method which holds the information required by the scheduler to enqueue and process it.

import { Job } from '@knowark/schedulark/lib/index.js'

class MaintenanceJob extends Job {
  async execute(self, context) {
    const number = context.number || 1000
    let [first, second] = [0, 1]
    while (first < number) {
      first = second
      second = first + second
    }

    return { data: first }
  }
}

Then you can create an Scheduler instance to control the arrangement and processing of its registered jobs.

import { Scheduler } from '@knowark/schedulark/lib/index.js'

const scheduler = new Scheduler()
scheduler.register(MaintenanceJob)

Finally, you might schedule (using cron expressions) one of the jobs you have previously registered so that it can be enqueued for execution.

scheduler.schedule('MaintenanceJob', {n: 777}, '0 0 * * *')

Summing up, the complete program using Schedulark would look like:

import { Job, Scheduler } from '@knowark/schedulark/lib/index.js'

class MaintenanceJob extends Job {
  async execute(context) {
    const number = context.number || 1000
    let [first, second] = 0, 1
    while (first < number) {
      first = second
      second = first + second
    }

    return { data: first }
  }
}

function main () {
  const scheduler = new Scheduler()
  scheduler.register(MaintenanceJob)

  scheduler.schedule('MaintenanceJob', {number: 765}, '0 0 * * *')
  scheduler.start()
}

main()