• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

daycry / jobs / 24670508442

05 Apr 2026 08:48AM UTC coverage: 53.982% (-2.2%) from 56.164%
24670508442

push

github

daycry
Optimize

50 of 192 new or added lines in 14 files covered. (26.04%)

3 existing lines in 3 files now uncovered.

1220 of 2260 relevant lines covered (53.98%)

4.43 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Commands/HealthCheckCommand.php
1
<?php
2

3
declare(strict_types=1);
4

5
/**
6
 * This file is part of Daycry Queues.
7
 *
8
 * (c) Daycry <daycry9@proton.me>
9
 *
10
 * For the full copyright and license information, please view
11
 * the LICENSE file that was distributed with this source code.
12
 */
13

14
namespace Daycry\Jobs\Commands;
15

16
use CodeIgniter\CLI\BaseCommand;
17
use CodeIgniter\CLI\CLI;
18
use Daycry\Jobs\Libraries\RateLimiter;
19
use Daycry\Jobs\Models\JobsLogModel;
20
use Daycry\Jobs\Models\QueueModel;
21

22
/**
23
 * Health check command for monitoring queue system status.
24
 * Provides statistics about queues, workers, and job processing.
25
 */
26
class HealthCheckCommand extends BaseCommand
27
{
28
    protected $group       = 'Jobs';
29
    protected $name        = 'jobs:health';
30
    protected $description = 'Display health check and statistics for the Jobs system';
31
    protected $usage       = 'jobs:health [options]';
32
    protected $arguments   = [];
33
    protected $options     = [
34
        '--json'  => 'Output as JSON',
35
        '--queue' => 'Check specific queue',
36
    ];
37

38
    public function run(array $params): void
39
    {
40
        $json          = CLI::getOption('json');
×
41
        $specificQueue = CLI::getOption('queue');
×
42

43
        $stats = $this->collectStats($specificQueue);
×
44

45
        if ($json) {
×
46
            CLI::write(json_encode($stats, JSON_PRETTY_PRINT));
×
47
        } else {
48
            $this->displayStats($stats);
×
49
        }
50
    }
51

52
    private function collectStats(?string $specificQueue = null): array
53
    {
54
        $config      = config('Jobs');
×
55
        $queueModel  = new QueueModel();
×
56
        $logModel    = model(JobsLogModel::class);
×
57
        $rateLimiter = new RateLimiter();
×
58

59
        $stats = [
×
60
            'timestamp' => date('Y-m-d H:i:s'),
×
61
            'config'    => [
×
62
                'log_performance'   => $config->logPerformance,
×
63
                'retry_strategy'    => $config->retryBackoffStrategy,
×
64
                'job_timeout'       => $config->jobTimeout,
×
65
                'dead_letter_queue' => $config->deadLetterQueue ?? 'disabled',
×
66
                'batch_size'        => $config->batchSize,
×
67
            ],
×
68
            'queues' => [],
×
69
        ];
×
70

71
        // Get queue statistics
72
        $queues = is_array($config->queues) ? $config->queues : explode(',', $config->queues);
×
73

74
        foreach ($queues as $queue) {
×
75
            $queue = trim($queue);
×
76

77
            if ($specificQueue && $queue !== $specificQueue) {
×
78
                continue;
×
79
            }
80

81
            $queueStats = [
×
82
                'name'    => $queue,
×
83
                'pending' => $queueModel->where('queue', $queue)
×
84
                    ->where('status', 'pending')
×
85
                    ->countAllResults(false),
×
NEW
86
                'in_progress' => $queueModel->where('queue', $queue)
×
NEW
87
                    ->where('status', 'in_progress')
×
88
                    ->countAllResults(false),
×
89
                'completed' => $queueModel->where('queue', $queue)
×
90
                    ->where('status', 'completed')
×
91
                    ->countAllResults(false),
×
92
                'failed' => $queueModel->where('queue', $queue)
×
93
                    ->where('status', 'failed')
×
94
                    ->countAllResults(false),
×
95
            ];
×
96

97
            // Rate limit usage
98
            $rateLimit = $config->queueRateLimits[$queue] ?? 0;
×
99
            if ($rateLimit > 0) {
×
100
                $queueStats['rate_limit'] = [
×
101
                    'max_per_minute' => $rateLimit,
×
102
                    'current_usage'  => $rateLimiter->getUsage($queue),
×
103
                ];
×
104
            }
105

106
            $stats['queues'][$queue] = $queueStats;
×
107
        }
108

109
        // Job execution stats (last 24 hours)
110
        if ($config->logPerformance && $logModel) {
×
111
            $yesterday = date('Y-m-d H:i:s', strtotime('-24 hours'));
×
112

113
            $stats['last_24h'] = [
×
114
                'total_executions' => $logModel->where('created_at >=', $yesterday)->countAllResults(false),
×
115
                'successes'        => $logModel->where('created_at >=', $yesterday)
×
116
                    ->where('error', null)
×
117
                    ->countAllResults(false),
×
118
                'failures' => $logModel->where('created_at >=', $yesterday)
×
119
                    ->where('error !=', null)
×
120
                    ->countAllResults(false),
×
121
            ];
×
122

123
            // Average processing time
124
            $avgTime = $logModel->selectAvg('duration')
×
125
                ->where('created_at >=', $yesterday)
×
126
                ->get()
×
127
                ->getRow();
×
128

129
            $stats['last_24h']['avg_duration'] = $avgTime->duration ?? 0;
×
130
        }
131

132
        return $stats;
×
133
    }
134

135
    private function displayStats(array $stats): void
136
    {
137
        CLI::newLine();
×
138
        CLI::write('╔════════════════════════════════════════════════╗', 'cyan');
×
139
        CLI::write('║         Jobs System Health Check               ║', 'cyan');
×
140
        CLI::write('╚════════════════════════════════════════════════╝', 'cyan');
×
141
        CLI::newLine();
×
142

143
        CLI::write('Timestamp: ' . $stats['timestamp'], 'yellow');
×
144
        CLI::newLine();
×
145

146
        // Config
147
        CLI::write('Configuration:', 'green');
×
148
        CLI::write('  Retry Strategy: ' . $stats['config']['retry_strategy']);
×
149
        CLI::write('  Job Timeout: ' . $stats['config']['job_timeout'] . 's');
×
150
        CLI::write('  Dead Letter Queue: ' . $stats['config']['dead_letter_queue']);
×
151
        CLI::write('  Batch Size: ' . $stats['config']['batch_size']);
×
152
        CLI::newLine();
×
153

154
        // Queues
155
        CLI::write('Queue Status:', 'green');
×
156

157
        foreach ($stats['queues'] as $name => $queueStats) {
×
158
            CLI::write("  [{$name}]", 'yellow');
×
159
            CLI::write("    Pending: {$queueStats['pending']}");
×
NEW
160
            CLI::write("    In Progress: {$queueStats['in_progress']}");
×
161
            CLI::write("    Completed: {$queueStats['completed']}");
×
162
            CLI::write("    Failed: {$queueStats['failed']}");
×
163

164
            if (isset($queueStats['rate_limit'])) {
×
165
                $rl = $queueStats['rate_limit'];
×
166
                CLI::write("    Rate Limit: {$rl['current_usage']}/{$rl['max_per_minute']} per minute");
×
167
            }
168
            CLI::newLine();
×
169
        }
170

171
        // Last 24h stats
172
        if (isset($stats['last_24h'])) {
×
173
            CLI::write('Last 24 Hours:', 'green');
×
174
            CLI::write('  Total Executions: ' . $stats['last_24h']['total_executions']);
×
175
            CLI::write('  Successes: ' . $stats['last_24h']['successes'], 'green');
×
176
            CLI::write('  Failures: ' . $stats['last_24h']['failures'], 'red');
×
177
            CLI::write('  Avg Duration: ' . round($stats['last_24h']['avg_duration'], 2) . 's');
×
178
        }
179

180
        CLI::newLine();
×
181
    }
182
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc