Currently, long running tasks will overlap (same task runs multiple instances at the same time) if the time necessary to finish the ask is greater than the interval. example NestJS service below
import { Injectable, Logger } from '@nestjs/common';
import { Interval } from '@nestjs/schedule';
function timeout(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
let c = 1
@Injectable()
export class TasksService {
private readonly logger = new Logger(TasksService.name);
@Interval(1000)
async handleCron() {
this.logger.debug(`start ${c}`);
await timeout(3000)
this.logger.debug(`end ${c}`);
c += 1
}
}
Is it possible to keep these tasks from overlapping and only calling the task with one instance at a time? Technically, we could keep track of a lock variable, but this would only allow us to skip an instance, if one is already running. Ideally, we could call set an option to allow intervals based on task end-time, rather than fixed intervals (aka start-time).