# Scheduler Architecture {#scheduler-architecture}


Background task scheduling infrastructure.


## Overview {#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 {#cron-scheduling}


```typescript
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 {#bullmq-job-queues}


```typescript
// 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 {#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 {#related-pages}


- [Background Jobs](https://sourcey-gauzy.pages.dev/architecture/background-jobs/index.md) — job processing
- [Worker Architecture](https://sourcey-gauzy.pages.dev/architecture/worker-architecture/index.md) — worker process
- [Redis Caching](https://sourcey-gauzy.pages.dev/architecture/redis-caching/index.md) — Redis config
