1.0.2 • Published 4 years ago

w3tech-cronloader v1.0.2

Weekly downloads
2
License
ISC
Repository
github
Last release
4 years ago

cronLoader

Cron system for https://github.com/w3tecch/express-typescript-boilerplate

Description

Sometimes, in our projects, we would like to have cron functionality inside our node.js process.

Using https://github.com/w3tecch/express-typescript-boilerplate we can add loader and decorator for this

Example

Register loader

app.js

bootstrapMicroframework({
    /**
     * Loader is a place where you can configure all your modules during microframework
     * bootstrap process. All loaders are executed one by one in a sequential order.
     */
    loaders: [
        // ...other loaders
        cronLoader,
    ],
})

And in loader code example

import { MicroframeworkLoader, MicroframeworkSettings } from 'microframework-w3tec';
import { CronLoader } from 'w3tech-cronloader';
import { Container } from 'typedi';

export const cronLoader: MicroframeworkLoader = (settings?: MicroframeworkSettings | undefined) => {
    CronLoader.runCron((target) => Container.get(target.constructor));
};

And in, near to your cronMethod, you can use decorator

import { CronLoader } from 'w3tech-cronloader';
import cronMethod = CronLoader.cronMethod;

export class Handler {
    @cronMethod({
        runOnInit: true,
        cronTime: '* * * * * *',
    })
    public test(): void {
        console.log(1);
    }
}

options in CronMethod cronTime: cron syntax for runing task runOnInit: run this command on the beginning arguments: array of arguments for your method;

Using DI

runCron method has signature

function runCron(getInstance?: (target: Object) => any);

getInstance - method which you can use to connect your DI.

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago