|
| 1 | +/** |
| 2 | + * Live monitoring dashboard for the job queue. |
| 3 | + * Refreshes at a configurable interval showing throughput, errors, and recent jobs. |
| 4 | + * |
| 5 | + * {code:bash} |
| 6 | + * wheels jobs monitor |
| 7 | + * wheels jobs monitor --interval=5 |
| 8 | + * wheels jobs monitor --queue=mailers |
| 9 | + * {code} |
| 10 | + */ |
| 11 | +component extends="../../base" { |
| 12 | + |
| 13 | + /** |
| 14 | + * @interval Refresh interval in seconds (default: 3) |
| 15 | + * @queue Filter by queue name (default: all queues) |
| 16 | + */ |
| 17 | + public void function run( |
| 18 | + numeric interval = 3, |
| 19 | + string queue = "" |
| 20 | + ) { |
| 21 | + local.appPath = getCWD(); |
| 22 | + |
| 23 | + if (!isWheelsApp(local.appPath)) { |
| 24 | + error("This command must be run from a Wheels application directory"); |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + print.line(); |
| 29 | + print.boldMagentaLine("Wheels Job Monitor"); |
| 30 | + print.line("Press Ctrl+C to stop"); |
| 31 | + print.line(); |
| 32 | + |
| 33 | + while (true) { |
| 34 | + // Build URL parameters |
| 35 | + local.urlParams = "&command=jobsMonitor"; |
| 36 | + if (Len(arguments.queue)) { |
| 37 | + local.urlParams &= "&queue=#arguments.queue#"; |
| 38 | + } |
| 39 | + |
| 40 | + try { |
| 41 | + local.result = $sendToCliCommand(urlstring = local.urlParams); |
| 42 | + |
| 43 | + if (StructKeyExists(local.result, "success") && local.result.success) { |
| 44 | + // Clear previous output with separator |
| 45 | + print.line(RepeatString("=", 60)); |
| 46 | + print.boldCyanLine("Job Queue Dashboard - #TimeFormat(Now(), "HH:mm:ss")#"); |
| 47 | + print.line(RepeatString("-", 60)); |
| 48 | + |
| 49 | + // Queue statistics |
| 50 | + if (StructKeyExists(local.result, "stats") && StructKeyExists(local.result.stats, "totals")) { |
| 51 | + local.t = local.result.stats.totals; |
| 52 | + print.line(); |
| 53 | + print.boldLine("Queue Summary:"); |
| 54 | + print.yellowLine(" Pending: #local.t.pending#"); |
| 55 | + print.cyanLine(" Processing: #local.t.processing#"); |
| 56 | + print.greenLine(" Completed: #local.t.completed#"); |
| 57 | + if (local.t.failed > 0) { |
| 58 | + print.redLine(" Failed: #local.t.failed#"); |
| 59 | + } else { |
| 60 | + print.line(" Failed: #local.t.failed#"); |
| 61 | + } |
| 62 | + print.line(" Total: #local.t.total#"); |
| 63 | + } |
| 64 | + |
| 65 | + // Throughput |
| 66 | + if (StructKeyExists(local.result, "monitor")) { |
| 67 | + local.m = local.result.monitor; |
| 68 | + |
| 69 | + print.line(); |
| 70 | + print.boldLine("Throughput (last 60 min):"); |
| 71 | + print.greenLine(" Completed: #local.m.throughput.completed#"); |
| 72 | + if (local.m.throughput.failed > 0) { |
| 73 | + print.redLine(" Failed: #local.m.throughput.failed#"); |
| 74 | + } else { |
| 75 | + print.line(" Failed: #local.m.throughput.failed#"); |
| 76 | + } |
| 77 | + if (local.m.errorRate > 0) { |
| 78 | + print.redLine(" Error rate: #local.m.errorRate#%"); |
| 79 | + } else { |
| 80 | + print.greenLine(" Error rate: 0%"); |
| 81 | + } |
| 82 | + |
| 83 | + if (Len(local.m.oldestPending)) { |
| 84 | + print.line(); |
| 85 | + print.line("Oldest pending job: #DateTimeFormat(local.m.oldestPending, 'yyyy-mm-dd HH:mm:ss')#"); |
| 86 | + } |
| 87 | + |
| 88 | + // Recent jobs |
| 89 | + if (ArrayLen(local.m.recentJobs)) { |
| 90 | + print.line(); |
| 91 | + print.boldLine("Recent Jobs:"); |
| 92 | + local.count = Min(ArrayLen(local.m.recentJobs), 5); |
| 93 | + for (local.i = 1; local.i <= local.count; local.i++) { |
| 94 | + local.job = local.m.recentJobs[local.i]; |
| 95 | + local.statusColor = "line"; |
| 96 | + if (local.job.status == "completed") local.statusColor = "greenLine"; |
| 97 | + else if (local.job.status == "failed") local.statusColor = "redLine"; |
| 98 | + else if (local.job.status == "processing") local.statusColor = "cyanLine"; |
| 99 | + else if (local.job.status == "pending") local.statusColor = "yellowLine"; |
| 100 | + |
| 101 | + print["#local.statusColor#"]( |
| 102 | + " [#local.job.status#] #local.job.jobClass# (#local.job.queue#) - #DateTimeFormat(local.job.updatedAt, 'HH:mm:ss')#" |
| 103 | + ); |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + // Timeout recoveries |
| 109 | + if (StructKeyExists(local.result, "timeoutsRecovered") && local.result.timeoutsRecovered > 0) { |
| 110 | + print.line(); |
| 111 | + print.yellowLine("Recovered #local.result.timeoutsRecovered# timed-out job(s)"); |
| 112 | + } |
| 113 | + } |
| 114 | + } catch (any e) { |
| 115 | + print.redLine("[#TimeFormat(Now(), "HH:mm:ss")#] Monitor error: #e.message#"); |
| 116 | + } |
| 117 | + |
| 118 | + sleep(arguments.interval * 1000); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | +} |
0 commit comments