24
24
use Symfony \Component \Process \PhpExecutableFinder ;
25
25
use Symfony \Component \Yaml \Yaml ;
26
26
27
- class CronCommand extends AbstractCommand {
27
+ class CronCommand extends AbstractCommand
28
+ {
28
29
protected static $ resolver = null ;
29
30
protected static $ crontabLines = [];
30
31
31
- public static function setResolver (ResolverInterface $ resolver ) {
32
+ public static function setResolver (ResolverInterface $ resolver )
33
+ {
32
34
static ::$ resolver = $ resolver ;
33
35
}
34
36
35
- public static function getResolver () {
37
+ public static function getResolver ()
38
+ {
36
39
// Initialize resolver if null
37
- if (!static ::$ resolver instanceOf ResolverInterface) {
40
+ if (!static ::$ resolver instanceof ResolverInterface) {
38
41
static ::setResolver (new ArrayResolver ());
39
42
}
40
43
return static ::$ resolver ;
41
44
}
42
45
43
- public static function addJob (JobInterface $ job ) {
46
+ public static function addJob (JobInterface $ job )
47
+ {
44
48
return static ::getResolver ()->addJob ($ job );
45
49
}
46
50
@@ -49,7 +53,8 @@ public static function addJob(JobInterface $job) {
49
53
* @param [type] $command the shell executable program ()
50
54
* @param [type] $schedule the crontab time line (ex: 5 * * * *)
51
55
*/
52
- public static function addSchedule ($ command , $ schedule ) {
56
+ public static function addSchedule ($ command , $ schedule )
57
+ {
53
58
chdir (GOTEO_PATH );
54
59
$ job = new ShellJob ();
55
60
$ job ->setCommand ($ command );
@@ -67,12 +72,13 @@ public static function addSchedule($command, $schedule) {
67
72
* 'nice' => true|false
68
73
* )
69
74
*/
70
- public static function addCrontabLine (array $ job ) {
75
+ public static function addCrontabLine (array $ job )
76
+ {
71
77
$ executable = $ job ['command ' ];
72
- if ($ job ['type ' ] == 'php ' ) {
78
+ if ($ job ['type ' ] == 'php ' ) {
73
79
$ executable = (new PhpExecutableFinder ())->find () . " $ executable " ;
74
80
}
75
- if ($ job ['nice ' ]) {
81
+ if ($ job ['nice ' ]) {
76
82
$ executable = "nice $ executable " ;
77
83
}
78
84
static ::$ crontabLines [] = [$ executable , $ job ['schedule ' ]];
@@ -88,11 +94,11 @@ public function __construct($name = null)
88
94
// Change to Goteo dir
89
95
chdir (GOTEO_PATH );
90
96
// Initializes crontab from yml resource file
91
- if (is_file (GOTEO_PATH . 'Resources/crontab.yml ' )) {
97
+ if (is_file (GOTEO_PATH . 'Resources/crontab.yml ' )) {
92
98
$ crontab = Yaml::parse (file_get_contents (GOTEO_PATH . 'Resources/crontab.yml ' ));
93
99
$ env = Config::get ('env ' );
94
- if (isset ($ crontab [$ env ])) {
95
- foreach ($ crontab [$ env ] as $ job ) {
100
+ if (isset ($ crontab [$ env ])) {
101
+ foreach ($ crontab [$ env ] as $ job ) {
96
102
static ::addCrontabLine ($ job );
97
103
}
98
104
}
@@ -102,12 +108,13 @@ public function __construct($name = null)
102
108
protected function configure ()
103
109
{
104
110
$ this ->setName ("cron " )
105
- ->setDescription ("The cron program executor " )
106
- ->setDefinition (array (
107
- new InputOption ('crontab ' , 'c ' , InputOption::VALUE_NONE , 'List the crontab defined and exit ' ),
108
- new InputOption ('jobs ' , 'j ' , InputOption::VALUE_NONE , 'List the jobs that will be executed now and exit ' ),
109
- ))
110
- ->setHelp (<<<EOT
111
+ ->setDescription ("The cron program executor " )
112
+ ->setDefinition (array (
113
+ new InputOption ('crontab ' , 'c ' , InputOption::VALUE_NONE , 'List the crontab defined and exit ' ),
114
+ new InputOption ('jobs ' , 'j ' , InputOption::VALUE_NONE , 'List the jobs that will be executed now and exit ' ),
115
+ ))
116
+ ->setHelp (
117
+ <<<EOT
111
118
Centralizes the execution of scheduled tasks.
112
119
113
120
Put in your crontab:
@@ -141,30 +148,30 @@ protected function configure()
141
148
+------------------------- min (0 - 59)
142
149
</>
143
150
EOT
144
- );
151
+ );
145
152
}
146
153
147
154
protected function execute (InputInterface $ input , OutputInterface $ output )
148
155
{
149
- if (count (static ::$ crontabLines ) == 0 ) {
156
+ if (count (static ::$ crontabLines ) == 0 ) {
150
157
$ output ->writeln ("<fg=red>No crontab at all!</> " );
151
158
return ;
152
159
}
153
- if ($ input ->getOption ('crontab ' )) {
154
- foreach (static ::$ crontabLines as $ line ) {
160
+ if ($ input ->getOption ('crontab ' )) {
161
+ foreach (static ::$ crontabLines as $ line ) {
155
162
$ output ->writeln ($ line [1 ] . ' ' . $ line [0 ]);
156
163
}
157
164
return ;
158
165
}
159
166
160
- if ($ input ->getOption ('jobs ' )) {
167
+ if ($ input ->getOption ('jobs ' )) {
161
168
$ jobs = static ::getResolver ()->resolve ();
162
- foreach ($ jobs as $ i => $ job ) {
169
+ foreach ($ jobs as $ i => $ job ) {
163
170
$ schedule = $ job ->getSchedule ()->getPattern ();
164
171
$ task = $ job ->getProcess ()->getCommandLine ();
165
172
$ output ->writeln ("$ schedule $ task " );
166
173
}
167
- if ($ i == 0 ) {
174
+ if ($ i == 0 ) {
168
175
$ output ->writeln ("<fg=cyan>No jobs now</> " );
169
176
}
170
177
return ;
@@ -175,48 +182,52 @@ protected function execute(InputInterface $input, OutputInterface $output)
175
182
$ cron ->setResolver (static ::getResolver ());
176
183
$ cron_report = $ cron ->run ();
177
184
$ num = count ($ cron_report ->getReports ());
185
+
186
+ $ this ->info ("---CRON COMMAND--- " );
187
+ $ this ->info (sprintf ("Date %s " , (new \DateTime ())->format (\DateTime::ISO8601_EXPANDED )));
178
188
$ this ->info ("Running $ num processes " , ['processes ' => $ num ]);
189
+
179
190
// wait
180
- while ($ cron ->isRunning ()) {
191
+ while ($ cron ->isRunning ()) {
181
192
$ output ->write (". " );
182
193
usleep (100000 );
183
194
}
184
- $ output ->writeln ("\n" );
185
195
186
196
$ i = 0 ;
187
- foreach ($ cron_report ->getReports () as $ report ) {
197
+ foreach ($ cron_report ->getReports () as $ report ) {
188
198
$ process = $ report ->getJob ()->getProcess ();
189
199
$ task = $ report ->getJob ()->getProcess ()->getCommandLine ();
190
200
$ time = $ report ->getEndTime () - $ report ->getStartTime ();
191
201
$ schedule = $ report ->getJob ()->getSchedule ()->getPattern ();
192
- if ($ output ->isVerbose ()) {
193
- $ output ->writeln ('[<fg=cyan> ' . $ task . '</>] <option=bold;fg= ' . ($ report ->isSuccessful () ? 'green>OK ' : 'red>ERROR ' ) . '</> ' );
202
+
203
+ if ($ output ->isVerbose ()) {
204
+ $ output ->writeln ('[<fg=cyan> ' . $ task . '</>] <option=bold;fg= ' . ($ report ->isSuccessful () ? 'green>OK ' : 'red>ERROR ' ) . '</> ' );
194
205
}
195
- if ($ report ->isSuccessful ()) {
206
+
207
+ if ($ report ->isSuccessful ()) {
196
208
$ this ->info ("Completed [ $ task] " . round ($ time , 3 ) . " seconds " , ['time ' => $ time , 'job ' => $ task , 'schedule ' => $ schedule ]);
197
- if ($ output ->isVerbose ()) {
198
- $ output ->writeln ('<fg=blue> ' . implode ("\n" , $ report ->getOutput ()) . '</> ' );
209
+ if ($ output ->isVerbose ()) {
210
+ $ output ->writeln ('<fg=blue> ' . implode ("\n" , $ report ->getOutput ()) . '</> ' );
199
211
}
200
-
201
212
} else {
202
213
$ errors = [];
203
- foreach ($ report ->getError () as $ err ) {
214
+ foreach ($ report ->getError () as $ err ) {
204
215
$ errors = array_merge ($ errors , explode ("\n" , $ err ));
205
216
}
206
217
$ this ->error ("Failed [ $ task] " . round ($ time , 3 ) . " seconds " , ['time ' => $ time , 'job ' => $ task , 'schedule ' => $ schedule , 'error ' => $ errors ]);
207
- if ($ output ->isVerbose ()) {
208
- $ output ->writeln ('<fg=red> ' . implode ("\n" , $ report ->getError ()) . '</> ' );
218
+ if ($ output ->isVerbose ()) {
219
+ $ output ->writeln ('<fg=red> ' . implode ("\n" , $ report ->getError ()) . '</> ' );
209
220
}
210
221
}
222
+
211
223
$ i ++;
212
224
}
213
- if ($ i == 0 ) {
225
+
226
+ if ($ i == 0 ) {
214
227
$ output ->writeln ("<fg=cyan>No jobs now</> " );
215
- }
216
- elseif ($ cron_report ->isSuccessful ()) {
228
+ } elseif ($ cron_report ->isSuccessful ()) {
217
229
$ output ->writeln ("<info>All done</info> " );
218
- }
219
- else {
230
+ } else {
220
231
$ output ->writeln ("<error>Completed with errors</error> " );
221
232
}
222
233
}
0 commit comments