Skip to content

Commit af5ec62

Browse files
Merge pull request #14 from mrrobot47/update-post-refactor
Update site utility function usage
2 parents a43b624 + f913b53 commit af5ec62

1 file changed

Lines changed: 36 additions & 22 deletions

File tree

src/Cron_Command.php

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<?php
22

33
use EE\Model\Cron;
4+
use function EE\Site\Utils\auto_site_name;
45

56
/**
67
* Manages cron on easyengine.
78
*
89
* @package ee-cli
910
*/
10-
1111
class Cron_Command extends EE_Command {
1212

1313
/**
@@ -40,11 +40,11 @@ public function __construct() {
4040
*
4141
* Entry | Description | Equivalent To
4242
* ----- | ----------- | -------------
43-
* @yearly (or @annually) | Run once a year, midnight, Jan. 1st | 0 0 1 1 *
44-
* @monthly | Run once a month, midnight, first of month | 0 0 1 * *
45-
* @weekly | Run once a week, midnight between Sat/Sun | 0 0 * * 0
46-
* @daily (or @midnight) | Run once a day, midnight | 0 0 * * *
47-
* @hourly | Run once an hour, beginning of hour | 0 * * * *
43+
* @yearly (or @annually) | Run once a year, midnight, Jan. 1st | 0 0 1 1 *
44+
* @monthly | Run once a month, midnight, first of month | 0 0 1 * *
45+
* @weekly | Run once a week, midnight between Sat/Sun | 0 0 * * 0
46+
* @daily (or @midnight) | Run once a day, midnight | 0 0 * * *
47+
* @hourly | Run once an hour, beginning of hour | 0 * * * *
4848
*
4949
* You may also schedule a job to execute at fixed intervals, starting at the time it's added or cron is run.
5050
* This is supported by following format:
@@ -71,13 +71,13 @@ public function __construct() {
7171
*
7272
* # Adds a cron job to host running EasyEngine
7373
* $ ee cron add host --command='wp media regenerate --yes' --schedule='@weekly'
74-
*
7574
*/
7675
public function add( $args, $assoc_args ) {
76+
7777
EE\Utils\delem_log( 'ee cron add start' );
7878

7979
if ( ! isset( $args[0] ) || $args[0] !== 'host' ) {
80-
$args = EE\SiteUtils\auto_site_name( $args, 'cron', __FUNCTION__ );
80+
$args = auto_site_name( $args, 'cron', __FUNCTION__ );
8181
}
8282

8383
$site = EE\Utils\remove_trailing_slash( $args[0] );
@@ -94,11 +94,11 @@ public function add( $args, $assoc_args ) {
9494
$this->validate_command( $command );
9595
$command = $this->add_sh_c_wrapper( $command );
9696

97-
Cron::create([
97+
Cron::create( [
9898
'site_url' => $site,
9999
'command' => $command,
100100
'schedule' => $schedule
101-
]);
101+
] );
102102

103103

104104
$this->update_cron_config();
@@ -155,9 +155,9 @@ public function add( $args, $assoc_args ) {
155155
*
156156
* # Updates schedule of cron
157157
* $ ee cron update 1 --schedule='@every 1m'
158-
*
159158
*/
160159
public function update( $args, $assoc_args ) {
160+
161161
EE\Utils\delem_log( 'ee cron add start' );
162162

163163
$data_to_update = [];
@@ -216,17 +216,16 @@ public function update( $args, $assoc_args ) {
216216
* @subcommand list
217217
*/
218218
public function _list( $args, $assoc_args ) {
219-
$where = [];
220-
$all = EE\Utils\get_flag_value( $assoc_args, 'all' );
219+
220+
$all = EE\Utils\get_flag_value( $assoc_args, 'all' );
221221

222222
if ( ( ! isset( $args[0] ) || $args[0] !== 'host' ) && ! $all ) {
223-
$args = EE\SiteUtils\auto_site_name( $args, 'cron', 'list' );
223+
$args = auto_site_name( $args, 'cron', 'list' );
224224
}
225225

226226
if ( isset( $args[0] ) ) {
227227
$crons = Cron::where( 'site_url', $args[0] );
228-
}
229-
else {
228+
} else {
230229
$crons = Cron::all();
231230
}
232231

@@ -245,7 +244,6 @@ public function _list( $args, $assoc_args ) {
245244
private function update_cron_config() {
246245

247246
$config = $this->generate_cron_config();
248-
249247
file_put_contents( EE_CONF_ROOT . '/cron/config.ini', $config );
250248
EE_DOCKER::restart_container( 'ee-cron-scheduler' );
251249
}
@@ -254,6 +252,7 @@ private function update_cron_config() {
254252
* Generates and returns cron config from DB
255253
*/
256254
private function generate_cron_config() {
255+
257256
$config_template = file_get_contents( __DIR__ . '/../templates/config.ini.mustache' );
258257
$crons = Cron::all();
259258

@@ -317,7 +316,7 @@ public function run_now( $args ) {
317316
*/
318317
public function delete( $args ) {
319318

320-
$id = $args[0];
319+
$id = $args[0];
321320
$cron = Cron::find( $id );
322321

323322
if ( ! $cron ) {
@@ -332,18 +331,26 @@ public function delete( $args ) {
332331

333332

334333
/**
335-
* Returns php container name of a site
334+
* Returns php container name of a site.
335+
*
336+
* @param string $site Name of the site whose container name is needed.
337+
*
338+
* @return string Container name.
336339
*/
337340
private function site_php_container( $site ) {
341+
338342
return str_replace( '.', '', $site ) . '_php_1';
339343
}
340344

341345
/**
342-
* Ensures given command will not create problem with INI syntax
346+
* Ensures given command will not create problem with INI syntax.
347+
* Semicolons and Hash(#) in commands do not work for now due to limitation of INI style config ofelia uses.
348+
* See https://github.com/EasyEngine/cron-command/issues/4.
349+
*
350+
* @param string $command Command whose syntax needs to be validated.
343351
*/
344352
private function validate_command( $command ) {
345-
// Semicolons and Hash(#) in commands do not work for now due to limitation of INI style config ofelia uses
346-
// See https://github.com/EasyEngine/cron-command/issues/4
353+
347354
if ( strpos( $command, ';' ) !== false ) {
348355
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.' );
349356
}
@@ -352,6 +359,13 @@ private function validate_command( $command ) {
352359
}
353360
}
354361

362+
/**
363+
* Adds wrapper of `sh -c` to execute composite commands through docker exec properly.
364+
*
365+
* @param string $command Passed command.
366+
*
367+
* @return string Command with properly added wrapper.
368+
*/
355369
private function add_sh_c_wrapper( $command ) {
356370
if ( strpos( $command, 'sh -c' ) !== false ) {
357371
return $command;

0 commit comments

Comments
 (0)