Skip to content

One more cron-based library for running cron tasks in separate worker threads

Notifications You must be signed in to change notification settings

hramov/node-cron-worker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Node Cron Worker Lib

  • Write cron jobs with typescript
  • Run jobs in parallel with worker_threads

Github repository

Based on node-cron scheduler + time parser (classic cron times like 0/5 * * * *) and worker_threads.

Try this at your own risk! npm i cron-worker-threads

Examples can be found in ./example

Basic usage

const options = {
        timezone: 'Europe/Moscow',
        poolMin: 1,
        poolMax: 5,
        logs: true, // show logs in console or not
    }

    const task = {
        name: 'Job',
        path: join(__dirname, 'jobs', 'job.ts'),
        enabled: true,
        cronTime: '*/2 * * * * *',
        params: {
            foo: 'bar'
        },
        runOnce: false, // if true stops job after first execution
    };

    const supervisor = new Supervisor([task], options);
    supervisor.start();

You can also add job to executing after starting the scheduler

...
supervisor.addTask(task) // add one job
supervisor.addTasks([task]) // add multiple jobs
...

If job property enabled is set to true, it's immediately added to execution.

If you want to use your custom logger, just pass it to Supervisor constructor after options

...
const supervisor = new Supervisor([task], options, logger);
supervisor.start();
...

Logger should implement ILogger interface

export interface ILogger {
    debug(message: string, context?: string): void;
    info(message: string, context?: string): void;
    warning(message: string, context?: string): void;
    error(message: string, stack?: string, context?: string): void;
}

Default logger output

{"ts":"28.05.2023, 12:43:58","level":"info","message":"Start cron thread"}
{"ts":"28.05.2023, 12:43:58","level":"info","message":"Send command to start jobs"}
{"ts":"28.05.2023, 12:43:58","level":"info","message":"Cron scheduler is online"}
{"ts":"28.05.2023, 12:44:00","level":"info","message":"Task Job scheduled"}
{"ts":"28.05.2023, 12:44:00","level":"info","message":"Task Job moved to execution queue"}
{"ts":"28.05.2023, 12:44:00","level":"info","message":"Worker d19af155-04ac-479b-a1cf-69204f6c5f25 online"}
{"ts":"28.05.2023, 12:44:00","level":"info","message":"Worker 6fab4a75-cf71-45fe-b22c-eacf37c7f395 online"}

Job communication with parent thread

  1. To indicate that job is finished successfully, just return something you want to see in logs that describes job's finish
  2. To indicate an error, throw an exception inside run function with error description
export async function run(params: any) {
    const result = //... your code
    if (error) {
        throw new Error(error);
    }
    return result;
}

Worker status

You can easily get worker status by invoking supervisor method getStat()

...
supervisor.start();
setInterval(async () => {
    const stat = await supervisor.getStat();
    console.table(stat)
}, 1000);
...

It returns object with following fields:

  1. poolSize - count of active/waiting worker threads
  2. taskPoolSize - count of tasks in processing queue
  3. taskProcessing - count of tasks currently processing by workers

Should you use it?

Advantages

  • Cron scheduler starts in separate thread and thus doesn't affect main thread and doesn't depend on its event loop
  • Every cron job implements as a separate TypeScript file that allows you to use all power of typing
  • Every cron job starts in its own thread that allows you to perform complex calculations and CPU intensive tasks with no effect on your server performance
  • Get worker status at every moment of execution

Disadvantages

  • Still in alpha
  • No tests
  • Not all bugs are found

About

One more cron-based library for running cron tasks in separate worker threads

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published