# SilentCronX Implementation Guide

This guide shows a practical production setup for SilentCronX.

## 1. Create a Scheduler Module

```ts
// src/jobs/scheduler.ts
import { createSilentCronX } from "silent-cronx";

export const scheduler = createSilentCronX({
  timezone: "Asia/Kolkata",
  maxWorkers: 4,
  maxConcurrency: 25,
  defaultTimeout: 30_000,
  preventOverlapping: true,
  lockTimeout: 30_000,
  maxPayloadBytes: 1024 * 1024,
  logger: console,
});
```

## 2. Register Jobs

```ts
export async function registerJobs() {
  await scheduler.schedule("daily-report", {
    cron: "0 9 * * *",
    preventOverlap: true,
    retry: { attempts: 3, backoff: "exponential", delayMs: 1000, maxDelayMs: 10_000, jitter: true },
    task: async ({ signal }) => {
      if (signal.aborted) return;
      console.log("Daily report started");
    },
  });

  scheduler.queue("notification-queue", {
    concurrency: 5,
    maxSize: 1000,
    rateLimit: { limit: 300, intervalMs: 60_000 },
    processor: async ({ payload }) => {
      console.log("Send notification", payload);
    },
  });
}
```

## 3. Start from App Bootstrap

```ts
import { scheduler } from "./jobs/scheduler";
import { registerJobs } from "./jobs/registerJobs";

await registerJobs();
scheduler.start();

process.once("SIGTERM", async () => {
  await scheduler.shutdown();
  process.exit(0);
});
```

## 4. Trigger Jobs from API Routes

```ts
await scheduler.addJob("notification-queue", {
  name: "welcome-message",
  payload: { userId: 101 },
});
```

Your React, React Native, Android, or web frontend should call this API route. SilentCronX stays on the backend where Node.js workers, timers, and storage are available.

## 5. Production Recommendations

- Use a persistent custom `StorageAdapter` for multi-instance apps.
- Keep every handler idempotent.
- Validate payloads before calling `schedule`, `delay`, `runNow`, or `addJob`.
- Use `preventOverlap` for critical jobs.
- Subscribe to failed, timeout, retry, and error events.
- Use worker module references for CPU-heavy work.
- Expose `getQueueStats()` and `getEventHistory()` through protected admin APIs for monitoring.
