|
| 1 | +import Baker, { Logger } from "../lib/index"; |
| 2 | + |
| 3 | +/** |
| 4 | + * Custom logger with colored output and timestamps. |
| 5 | + * You can replace this with any logger that implements the Logger interface, |
| 6 | + * such as pino, winston, bunyan, or logtape. |
| 7 | + */ |
| 8 | +const createColoredLogger = (prefix: string): Logger => { |
| 9 | + const colors = { |
| 10 | + reset: "\x1b[0m", |
| 11 | + dim: "\x1b[2m", |
| 12 | + blue: "\x1b[34m", |
| 13 | + yellow: "\x1b[33m", |
| 14 | + red: "\x1b[31m", |
| 15 | + cyan: "\x1b[36m", |
| 16 | + }; |
| 17 | + |
| 18 | + const timestamp = () => new Date().toISOString(); |
| 19 | + |
| 20 | + return { |
| 21 | + info: (message, ...args) => { |
| 22 | + console.log( |
| 23 | + `${colors.dim}${timestamp()}${colors.reset} ${colors.blue}[INFO]${ |
| 24 | + colors.reset |
| 25 | + } ${colors.cyan}[${prefix}]${colors.reset}`, |
| 26 | + message, |
| 27 | + ...args |
| 28 | + ); |
| 29 | + }, |
| 30 | + warn: (message, ...args) => { |
| 31 | + console.warn( |
| 32 | + `${colors.dim}${timestamp()}${colors.reset} ${colors.yellow}[WARN]${ |
| 33 | + colors.reset |
| 34 | + } ${colors.cyan}[${prefix}]${colors.reset}`, |
| 35 | + message, |
| 36 | + ...args |
| 37 | + ); |
| 38 | + }, |
| 39 | + error: (message, ...args) => { |
| 40 | + console.error( |
| 41 | + `${colors.dim}${timestamp()}${colors.reset} ${colors.red}[ERROR]${ |
| 42 | + colors.reset |
| 43 | + } ${colors.cyan}[${prefix}]${colors.reset}`, |
| 44 | + message, |
| 45 | + ...args |
| 46 | + ); |
| 47 | + }, |
| 48 | + debug: (message, ...args) => { |
| 49 | + console.debug( |
| 50 | + `${colors.dim}${timestamp()}${colors.reset} ${colors.dim}[DEBUG]${ |
| 51 | + colors.reset |
| 52 | + } ${colors.cyan}[${prefix}]${colors.reset}`, |
| 53 | + message, |
| 54 | + ...args |
| 55 | + ); |
| 56 | + }, |
| 57 | + }; |
| 58 | +}; |
| 59 | + |
| 60 | +async function main() { |
| 61 | + // Create a custom logger for the Baker instance |
| 62 | + const bakerLogger = createColoredLogger("cronbake"); |
| 63 | + |
| 64 | + const baker = Baker.create({ |
| 65 | + autoStart: true, |
| 66 | + logger: bakerLogger, |
| 67 | + }); |
| 68 | + |
| 69 | + // This job uses the Baker's logger (bakerLogger) |
| 70 | + baker.add({ |
| 71 | + name: "heartbeat", |
| 72 | + cron: "@every_5_seconds", |
| 73 | + immediate: true, |
| 74 | + callback: () => { |
| 75 | + console.log("💓 Heartbeat job executed successfully"); |
| 76 | + }, |
| 77 | + }); |
| 78 | + |
| 79 | + // This job has its own dedicated logger |
| 80 | + const paymentLogger = createColoredLogger("payments"); |
| 81 | + baker.add({ |
| 82 | + name: "process-payments", |
| 83 | + cron: "@every_10_seconds", |
| 84 | + logger: paymentLogger, // Override with job-specific logger |
| 85 | + immediate: true, |
| 86 | + callback: () => { |
| 87 | + console.log("💰 Processing payments..."); |
| 88 | + }, |
| 89 | + }); |
| 90 | + |
| 91 | + // This job will fail, demonstrating error logging |
| 92 | + baker.add({ |
| 93 | + name: "flaky-job", |
| 94 | + cron: "@every_15_seconds", |
| 95 | + immediate: true, |
| 96 | + delay: "2s", |
| 97 | + callback: () => { |
| 98 | + // Simulate a random failure |
| 99 | + if (Math.random() > 0.5) { |
| 100 | + throw new Error("Random failure occurred!"); |
| 101 | + } |
| 102 | + console.log("🎲 Flaky job succeeded this time!"); |
| 103 | + }, |
| 104 | + }); |
| 105 | + |
| 106 | + console.log("\n🚀 Logging example started!"); |
| 107 | + console.log(" - heartbeat: runs every 5 seconds (uses Baker logger)"); |
| 108 | + console.log( |
| 109 | + " - process-payments: runs every 10 seconds (uses custom payments logger)" |
| 110 | + ); |
| 111 | + console.log( |
| 112 | + " - flaky-job: runs every 15 seconds, fails randomly to demo error logging" |
| 113 | + ); |
| 114 | + console.log("\nPress Ctrl+C to stop.\n"); |
| 115 | + |
| 116 | + const shutdown = () => { |
| 117 | + console.log("\n👋 Stopping all jobs..."); |
| 118 | + baker.destroyAll(); |
| 119 | + process.exit(0); |
| 120 | + }; |
| 121 | + |
| 122 | + process.once("SIGINT", shutdown); |
| 123 | + process.once("SIGTERM", shutdown); |
| 124 | +} |
| 125 | + |
| 126 | +main().catch((error) => { |
| 127 | + console.error("Logging example failed:", error); |
| 128 | + process.exit(1); |
| 129 | +}); |
0 commit comments