Scheduler Architecture
Background task scheduling infrastructure.
Overviewโ
Gauzy uses multiple scheduling mechanisms:
- NestJS Schedule โ cron-based in-process tasks
- BullMQ โ Redis-backed job queues
- Custom Timers โ interval-based operations
Cron Schedulingโ
import { Cron, CronExpression } from "@nestjs/schedule";
@Injectable()
export class CleanupService {
@Cron(CronExpression.EVERY_DAY_AT_MIDNIGHT)
async cleanupExpiredSessions() {
await this.sessionRepository
.createQueryBuilder()
.delete()
.where("expiresAt < :now", { now: new Date() })
.execute();
}
@Cron("0 */5 * * * *") // Every 5 minutes
async syncTimerStatus() {
await this.timerService.syncAllActiveTimers();
}
}
BullMQ Job Queuesโ
// Producer
@Injectable()
export class EmailService {
constructor(@InjectQueue("email") private emailQueue: Queue) {}
async sendWelcome(employee: IEmployee) {
await this.emailQueue.add("welcome", {
to: employee.user.email,
template: "welcome-user",
context: { firstName: employee.firstName },
});
}
}
// Consumer
@Processor("email")
export class EmailProcessor {
@Process("welcome")
async handleWelcome(job: Job) {
await this.mailerService.send(job.data);
}
}
Scheduled Tasksโ
| Task | Schedule | Purpose |
|---|---|---|
| Session cleanup | Daily midnight | Remove expired |
| Timer sync | Every 5 min | Sync active timers |
| Report generation | Weekly Monday | Generate reports |
| Backup trigger | Daily 2 AM | Database backup |
| Integration sync | Every hour | Sync external data |
Related Pagesโ
- Background Jobs โ job processing
- Worker Architecture โ worker process
- Redis Caching โ Redis config