Monitor TypeScript Cron Jobs with CronRabbit
Node.jsThe Problem
TypeScript projects need type-safe monitoring integration. Manually adding fetch calls everywhere leads to inconsistency and missed error handling.
The Solution
Create a typed CronRabbit utility class that wraps your job functions with automatic start/success/fail pings.
Type-Safe Monitoring Wrapper
Build a reusable wrapper function that handles start, success, and fail pings. This ensures consistent monitoring across all your scheduled tasks.
Code Examples
Reusable monitoring wrapper
TypeScriptclass CronMonitor {
constructor(private pingUrl: string) {}
async run<T>(fn: () => Promise<T>): Promise<T> {
await fetch(`${this.pingUrl}/start`).catch(() => {});
try {
const result = await fn();
await fetch(this.pingUrl).catch(() => {});
return result;
} catch (err) {
await fetch(`${this.pingUrl}/fail`).catch(() => {});
throw err;
}
}
}
// Usage
const monitor = new CronMonitor("https://ping.cronrabbit.com/your-id");
await monitor.run(async () => {
await generateDailyReport();
});