cronks
v0.0.13
Published

Readme
![]()
Cronks
A robust package designed to guarantee that a task executes exactly once, seamlessly coordinating across multiple instances of a horizontally scaled application, ensuring reliability and consistency in distributed environments
Installation
Install cronks
npm install cronks yarn add cronksUsage/Examples
Registering module
import { Module } from '@nestjs/common';
import { CronksModule } from 'cronks';
@Module({
imports: [
CronksModule.register({
host: 'localhost',
port: 6379,
password: 'your-redis-password',
}),
],
})
export class AppModule {}Using decorator
import { Injectable } from '@nestjs/common';
import { Cron, CronPatterns } from 'cronks';
@Injectable()
export class MyService {
@Cron(CronPatterns.EVERY_10_SECONDS, { name: 'quick-task'})
async myJob() {
console.log('Runs every 10 seconds');
}
@Cron('*/5 * * * * *')
async myJob1() {
console.log('Runs every 5 seconds');
}
}Or dynamic way, via CronksService
import { Injectable } from '@nestjs/common';
import { CronksService, CronPatterns } from 'cronks';
@Injectable()
export class MyService {
constructor(private readonly cronksService: CronksService) {}
async deleteJob() {
try {
await this.cronksService.deleteCronJob(
'my-job',
CronPatterns.EVERY_10_SECONDS,
'America/Chicago', //if given tz
);
console.log('Job deleted');
} catch (error) {
console.error('Deletion failed:', error.message);
}
}
async registerJob() {
await this.cronksService.registerCronJob({
name: 'my-job',
pattern: CronPatterns.EVERY_10_SECONDS,
timezone: 'America/Chicago',
},
async (job) => {
console.log('Job executed:', job.name);
});
}
}
