-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathCron_Command.php
More file actions
377 lines (330 loc) · 10.9 KB
/
Cron_Command.php
File metadata and controls
377 lines (330 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
<?php
use EE\Model\Cron;
use EE\Site\Utils as Site_Utils;
use EE\Utils as EE_Utils;
use EE\Cron\Utils as Cron_Utils;
/**
* Manages cron on easyengine sites and host machine.
*
* @package ee-cli
*/
class Cron_Command extends EE_Command {
/**
* Adds a cron job to run a command at specific interval etc.
*
* ## OPTIONS
*
* [<site-name>]
* : Name of site to run cron on.
*
* --command=<command>
* : Command to schedule.
*
* --schedule=<schedule>
* : Time to schedule. Format is same as Linux cron.
*
* [--user=<user>]
* : User to execute command as.
*
* We also have helper to easily specify scheduling format:
*
* | Entry | Description | Equivalent To
* | ----- | ----------- | -------------
* | @yearly (or @annually) | Run once a year, midnight, Jan. 1st | 0 0 1 1 *
* | @monthly | Run once a month, midnight, first of month | 0 0 1 * *
* | @weekly | Run once a week, midnight between Sat/Sun | 0 0 * * 0
* | @daily (or @midnight) | Run once a day, midnight | 0 0 * * *
* | @hourly | Run once an hour, beginning of hour | 0 * * * *
*
* You may also schedule a job to execute at fixed intervals, starting at the time it's added or cron is run.
* This is supported by following format:
*
* - @every <duration>
*
* Where duration can be combination of:
* <number>h - hour
* <number>m - minute
* <number>s - second
*
* So 1h10m2s is also a valid duration
*
* ## EXAMPLES
*
* # Adds a cron job on example.com every 10 minutes
* $ ee cron create example.com --command='wp cron event run --due-now' --schedule='@every 10m'
*
* # Adds a cron job on example.com every 1 minutes
* $ ee cron create example.com --command='wp cron event run --due-now' --schedule='* * * * *'
*
* # Adds a cron job on example.com every 1 minutes run as user www-data
* $ ee cron create example.com --command='wp cron event run --due-now' --schedule='* * * * *' --user=www-data
*
* # Adds a cron job to host running EasyEngine
* $ ee cron create host --command='wp cron event run --due-now' --schedule='@every 10m'
*
* # Adds a cron job to host running EasyEngine
* $ ee cron create host --command='wp media regenerate --yes' --schedule='@weekly'
*/
public function create( $args, $assoc_args ) {
if ( 'running' !== EE_DOCKER::container_status( EE_CRON_SCHEDULER ) ) {
$img_versions = EE_Utils\get_image_versions();
$cron_scheduler_run_command = 'docker run --name ' . EE_CRON_SCHEDULER . ' --restart=always -d -v ' . EE_ROOT_DIR . '/services/cron:/etc/ofelia:ro -v /var/run/docker.sock:/var/run/docker.sock:ro easyengine/cron:' . $img_versions['easyengine/cron'];
if ( ! EE_DOCKER::boot_container( EE_CRON_SCHEDULER, $cron_scheduler_run_command ) ) {
EE::error( 'There was some error in starting ' . EE_CRON_SCHEDULER . ' container. Please check logs.' );
}
}
EE_Utils\delem_log( 'ee cron add start' );
if ( ! isset( $args[0] ) || 'host' !== $args[0] ) {
$args = Site_Utils\auto_site_name( $args, 'cron', __FUNCTION__ );
}
$site = EE_Utils\remove_trailing_slash( $args[0] );
$command = EE_Utils\get_flag_value( $assoc_args, 'command' );
$schedule = EE_Utils\get_flag_value( $assoc_args, 'schedule' );
$user = EE_Utils\get_flag_value( $assoc_args, 'user' );
if ( 'host' !== $args[0] ) {
$site_info = Site_Utils\get_site_info( $args );
if ( ! EE_DOCKER::service_exists( 'php', $site_info['site_fs_path'] ) ) {
EE::error( $site . ' does not have PHP container.' );
}
}
if ( '@' !== substr( trim( $schedule ), 0, 1 ) ) {
// Filter out spaces but not 0. 'trim' filter removes 0 as well.
$schedule_length = count( array_filter( explode( ' ', $schedule ), function ( $value ) {
return preg_match( '#\S#', $value );
} ) );
if ( 5 !== $schedule_length ) {
EE::error( 'Schedule format should be same as Linux cron or schedule helper syntax(Check help for this)' );
}
$schedule = '0 ' . trim( $schedule );
}
$this->validate_command( $command );
$command = $this->add_sh_c_wrapper( $command );
$cron_data = [
'site_url' => $site,
'command' => $command,
'schedule' => $schedule,
];
if ( $user ) {
$cron_data['user'] = $user;
}
Cron::create( $cron_data );
Cron_Utils\update_cron_config();
EE::success( 'Cron created successfully' );
EE_Utils\delem_log( 'ee cron add end' );
}
/**
* Ensures given command will not create problem with INI syntax.
* Semicolons and Hash(#) in commands do not work for now due to limitation of INI style config ofelia uses.
* See https://github.com/EasyEngine/cron-command/issues/4.
*
* @param string $command Command whose syntax needs to be validated.
*
* @throws EE\ExitException
*/
private function validate_command( $command ) {
if ( strpos( $command, ';' ) !== false ) {
EE::error( 'Command chaining using `;` - semi-colon is not supported currently. You can either use `&&` or `||` or creating a second cron job for the chained command.' );
}
if ( strpos( $command, '#' ) !== false ) {
EE::error( 'EasyEngine does not support commands with #' );
}
}
/**
* Adds wrapper of `sh -c` to execute composite commands through docker exec properly.
*
* @param string $command Passed command.
*
* @return string Command with properly added wrapper.
*/
private function add_sh_c_wrapper( $command ) {
if ( strpos( $command, 'sh -c' ) !== false ) {
return $command;
}
return "sh -c '" . $command . "'";
}
/**
* Updates a cron job.
*
* ## OPTIONS
*
* <id>
* : ID of cron to update.
*
* [--site=<site>]
* : Command to schedule.
*
* [--command=<command>]
* : Command to schedule.
*
* [--schedule=<schedule>]
* : Time to schedule. Format is same as Linux cron.
*
* [--user=<user>]
* : User to execute command as.
*
* We also have helper to easily specify scheduling format:
*
* | Entry | Description | Equivalent To
* | ----- | ----------- | -------------
* | @yearly (or @annually) | Run once a year, midnight, Jan. 1st | 0 0 1 1 *
* | @monthly | Run once a month, midnight, first of month | 0 0 1 * *
* | @weekly | Run once a week, midnight between Sat/Sun | 0 0 * * 0
* | @daily (or @midnight) | Run once a day, midnight | 0 0 * * *
* | @hourly | Run once an hour, beginning of hour | 0 * * * *
*
* You may also schedule a job to execute at fixed intervals, starting at the time it's added or cron is run.
* This is supported by following format:
*
* - @every <duration>
*
* Where duration can be combination of:
* <number>h - hour
* <number>m - minute
* <number>s - second
*
* So 1h10m2s is also a valid duration
*
* ## EXAMPLES
*
* # Updates site to run cron on
* $ ee cron update 1 --site='example1.com'
*
* # Updates command of cron
* $ ee cron update 1 --command='wp cron event run --due-now'
*
* # Updates command and user of cron
* $ ee cron update 1 --command='wp cron event run --due-now' --user=root
*
* # Updates schedule of cron
* $ ee cron update 1 --schedule='@every 1m'
*/
public function update( $args, $assoc_args ) {
EE_Utils\delem_log( 'ee cron add start' );
$data_to_update = [];
$site = EE_Utils\get_flag_value( $assoc_args, 'site' );
$command = EE_Utils\get_flag_value( $assoc_args, 'command' );
$schedule = EE_Utils\get_flag_value( $assoc_args, 'schedule' );
$user = EE_Utils\get_flag_value( $assoc_args, 'user' );
$cron_id = $args[0];
if ( ! $site && ! $command && ! $schedule && ! $user ) {
EE::error( 'You should specify at least one of - site, command, schedule or user to update' );
}
if ( $site ) {
$data_to_update['site_url'] = $site;
}
if ( $user ) {
$data_to_update['user'] = $user;
}
if ( $command ) {
$this->validate_command( $command );
$command = $this->add_sh_c_wrapper( $command );
$data_to_update['command'] = $command;
}
if ( $schedule ) {
if ( '@' !== substr( trim( $schedule ), 0, 1 ) ) {
$schedule_length = strlen( implode( explode( ' ', trim( $schedule ) ) ) );
if ( 5 !== $schedule_length ) {
EE::error( 'Schedule format should be same as Linux cron or schedule helper syntax(Check help for this)' );
}
}
$data_to_update['schedule'] = $schedule;
}
Cron::update( [ 'id' => $cron_id ], $data_to_update );
Cron_Utils\update_cron_config();
EE::success( 'Cron update Successfully' );
EE_Utils\delem_log( 'ee cron add end' );
}
/**
* Lists scheduled cron jobs.
*
* ## OPTIONS
*
* [<site-name>]
* : Name of site whose cron will be displayed.
*
* [--all]
* : View all cron jobs.
*
* ## EXAMPLES
*
* # Lists all scheduled cron jobs
* $ ee cron list
*
* # Lists all scheduled cron jobs of a site
* $ ee cron list example.com
*
* @subcommand list
*/
public function _list( $args, $assoc_args ) {
$all = EE_Utils\get_flag_value( $assoc_args, 'all' );
if ( ( ! isset( $args[0] ) || 'host' !== $args[0] ) && ! $all ) {
$args = Site_Utils\auto_site_name( $args, 'cron', 'list' );
}
if ( isset( $args[0] ) ) {
$crons = Cron::where( 'site_url', $args[0] );
} else {
$crons = Cron::all();
}
if ( empty( $crons ) ) {
EE::error( 'No cron jobs found.' );
}
EE_Utils\format_items( 'table', $crons, [ 'id', 'site_url', 'command', 'schedule' ] );
}
/**
* Runs a cron job
*
* ## OPTIONS
*
* <cron-id>
* : ID of cron to run.
*
* ## EXAMPLES
*
* # Runs a cron job
* $ ee cron run-now 1
*
* @subcommand run-now
*/
public function run_now( $args ) {
$cron = Cron::find( $args[0] );
if ( empty( $cron ) ) {
EE::error( 'No such cron with id ' . $args[0] );
}
$container = Cron_Utils\site_php_container( $cron->site_url );
$command = $cron->command;
$user = empty( $cron->user ) ? 'root' : $cron->user;
if ( 'host' === $cron->site_url ) {
EE::exec( $command, true, true );
return;
}
EE::exec( "docker exec --user=$user $container $command", true, true );
}
/**
* Deletes a cron job
*
* ## OPTIONS
*
* <cron-id>
* : ID of cron to be deleted.
*
* ## EXAMPLES
*
* # Deletes a cron jobs
* $ ee cron delete 1
*
*/
public function delete( $args ) {
$id = $args[0];
$cron = Cron::find( $id );
if ( ! $cron ) {
EE::error( 'Unable to find cron with id ' . $id );
}
$cron->delete();
Cron_Utils\update_cron_config();
EE::success( 'Deleted cron with id ' . $id );
$cron_entries = Cron::all();
if ( empty( $cron_entries ) ) {
EE::exec( 'docker rm -f ' . EE_CRON_SCHEDULER );
}
}
}