|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Quantum PHP Framework |
|
5
|
|
|
* |
|
6
|
|
|
* An open source software development framework for PHP |
|
7
|
|
|
* |
|
8
|
|
|
* @package Quantum |
|
9
|
|
|
* @author Arman Ag. <[email protected]> |
|
10
|
|
|
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) |
|
11
|
|
|
* @link http://quantum.softberg.org/ |
|
12
|
|
|
* @since 3.0.0 |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Quantum\Console\Commands; |
|
16
|
|
|
|
|
17
|
|
|
use Quantum\Libraries\Cron\Exceptions\CronException; |
|
18
|
|
|
use Quantum\Libraries\Cron\CronManager; |
|
19
|
|
|
use Quantum\Console\QtCommand; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class CronRunCommand |
|
23
|
|
|
* @package Quantum\Console |
|
24
|
|
|
*/ |
|
25
|
|
|
class CronRunCommand extends QtCommand |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* The console command name. |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $name = 'cron:run'; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* The console command description. |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $description = 'Run scheduled cron tasks'; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Command help text. |
|
41
|
|
|
* @var string |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $help = 'Executes scheduled tasks defined in the cron directory. Use --task to run a single task or --force to bypass locks.'; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Command options |
|
47
|
|
|
* @var array<int, list<string|null>> |
|
|
|
|
|
|
48
|
|
|
*/ |
|
49
|
|
|
protected $options = [ |
|
50
|
|
|
['force', 'f', 'none', 'Force run tasks ignoring locks'], |
|
51
|
|
|
['task', 't', 'optional', 'Run a specific task by name'], |
|
52
|
|
|
['path', 'p', 'optional', 'Custom cron directory path'], |
|
53
|
|
|
]; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Executes the command |
|
57
|
|
|
*/ |
|
58
|
|
|
public function exec() |
|
59
|
|
|
{ |
|
60
|
|
|
$force = (bool) $this->getOption('force'); |
|
61
|
|
|
$taskName = $this->getOption('task'); |
|
62
|
|
|
$cronPath = $this->getOption('path') ?: cron_config('path'); |
|
63
|
|
|
|
|
64
|
|
|
try { |
|
65
|
|
|
$manager = new CronManager($cronPath); |
|
66
|
|
|
|
|
67
|
|
|
if ($taskName) { |
|
68
|
|
|
$this->runSpecificTask($manager, $taskName, $force); |
|
69
|
|
|
} else { |
|
70
|
|
|
$this->runAllDueTasks($manager, $force); |
|
71
|
|
|
} |
|
72
|
|
|
} catch (CronException $e) { |
|
73
|
|
|
$this->error($e->getMessage()); |
|
74
|
|
|
} catch (\Throwable $e) { |
|
75
|
|
|
$this->error('Unexpected error: ' . $e->getMessage()); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Run all due tasks |
|
81
|
|
|
* @param CronManager $manager |
|
82
|
|
|
* @param bool $force |
|
83
|
|
|
* @return void |
|
84
|
|
|
*/ |
|
85
|
|
|
private function runAllDueTasks(CronManager $manager, bool $force): void |
|
86
|
|
|
{ |
|
87
|
|
|
$this->info('Running scheduled tasks...'); |
|
88
|
|
|
|
|
89
|
|
|
$stats = $manager->runDueTasks($force); |
|
90
|
|
|
|
|
91
|
|
|
$this->output(''); |
|
92
|
|
|
$this->info('Execution Summary:'); |
|
93
|
|
|
$this->output(" Total tasks: {$stats['total']}"); |
|
94
|
|
|
$this->output(" Executed: <info>{$stats['executed']}</info>"); |
|
95
|
|
|
$this->output(" Skipped: {$stats['skipped']}"); |
|
96
|
|
|
|
|
97
|
|
|
if ($stats['locked'] > 0) { |
|
98
|
|
|
$this->output(" Locked: <comment>{$stats['locked']}</comment>"); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
if ($stats['failed'] > 0) { |
|
102
|
|
|
$this->output(" Failed: <error>{$stats['failed']}</error>"); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
$this->output(''); |
|
106
|
|
|
|
|
107
|
|
|
if ($stats['executed'] > 0) { |
|
108
|
|
|
$this->info('✓ Tasks completed successfully'); |
|
109
|
|
|
} elseif ($stats['total'] === 0) { |
|
110
|
|
|
$this->comment('No tasks found in cron directory'); |
|
111
|
|
|
} else { |
|
112
|
|
|
$this->comment('No tasks were due to run'); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Run a specific task |
|
118
|
|
|
* @param CronManager $manager |
|
119
|
|
|
* @param string $taskName |
|
120
|
|
|
* @param bool $force |
|
121
|
|
|
* @return void |
|
122
|
|
|
* @throws CronException |
|
123
|
|
|
*/ |
|
124
|
|
|
private function runSpecificTask(CronManager $manager, string $taskName, bool $force): void |
|
125
|
|
|
{ |
|
126
|
|
|
$this->info("Running task: {$taskName}"); |
|
127
|
|
|
|
|
128
|
|
|
$manager->runTaskByName($taskName, $force); |
|
129
|
|
|
|
|
130
|
|
|
$stats = $manager->getStats(); |
|
131
|
|
|
|
|
132
|
|
|
if ($stats['executed'] > 0) { |
|
133
|
|
|
$this->info("✓ Task '{$taskName}' completed successfully"); |
|
134
|
|
|
} elseif ($stats['failed'] > 0) { |
|
135
|
|
|
$this->error("✗ Task '{$taskName}' failed"); |
|
136
|
|
|
} elseif ($stats['locked'] > 0) { |
|
137
|
|
|
$this->comment("⚠ Task '{$taskName}' is locked"); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|