From 5f5da7aa7c68000b1fd8073fbecc9cf815884f0c Mon Sep 17 00:00:00 2001 From: Riddhesh Sanghvi Date: Thu, 30 Aug 2018 17:56:33 +0530 Subject: [PATCH 1/2] Update function acc to new namespace Signed-off-by: Riddhesh Sanghvi --- src/Cron_Command.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Cron_Command.php b/src/Cron_Command.php index 5be8b14..b11fcda 100644 --- a/src/Cron_Command.php +++ b/src/Cron_Command.php @@ -1,6 +1,7 @@ Date: Thu, 30 Aug 2018 18:07:17 +0530 Subject: [PATCH 2/2] Add doc comments and fix phpcs errors Signed-off-by: Riddhesh Sanghvi --- src/Cron_Command.php | 53 +++++++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 20 deletions(-) diff --git a/src/Cron_Command.php b/src/Cron_Command.php index b11fcda..c1d0369 100644 --- a/src/Cron_Command.php +++ b/src/Cron_Command.php @@ -8,7 +8,6 @@ * * @package ee-cli */ - class Cron_Command extends EE_Command { /** @@ -41,11 +40,11 @@ public function __construct() { * * 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 * * * * + * @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: @@ -72,9 +71,9 @@ public function __construct() { * * # Adds a cron job to host running EasyEngine * $ ee cron add host --command='wp media regenerate --yes' --schedule='@weekly' - * */ public function add( $args, $assoc_args ) { + EE\Utils\delem_log( 'ee cron add start' ); if ( ! isset( $args[0] ) || $args[0] !== 'host' ) { @@ -95,11 +94,11 @@ public function add( $args, $assoc_args ) { $this->validate_command( $command ); $command = $this->add_sh_c_wrapper( $command ); - Cron::create([ + Cron::create( [ 'site_url' => $site, 'command' => $command, 'schedule' => $schedule - ]); + ] ); $this->update_cron_config(); @@ -156,9 +155,9 @@ public function add( $args, $assoc_args ) { * * # 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 = []; @@ -217,8 +216,8 @@ public function update( $args, $assoc_args ) { * @subcommand list */ public function _list( $args, $assoc_args ) { - $where = []; - $all = EE\Utils\get_flag_value( $assoc_args, 'all' ); + + $all = EE\Utils\get_flag_value( $assoc_args, 'all' ); if ( ( ! isset( $args[0] ) || $args[0] !== 'host' ) && ! $all ) { $args = auto_site_name( $args, 'cron', 'list' ); @@ -226,8 +225,7 @@ public function _list( $args, $assoc_args ) { if ( isset( $args[0] ) ) { $crons = Cron::where( 'site_url', $args[0] ); - } - else { + } else { $crons = Cron::all(); } @@ -246,7 +244,6 @@ public function _list( $args, $assoc_args ) { private function update_cron_config() { $config = $this->generate_cron_config(); - file_put_contents( EE_CONF_ROOT . '/cron/config.ini', $config ); EE_DOCKER::restart_container( 'ee-cron-scheduler' ); } @@ -255,6 +252,7 @@ private function update_cron_config() { * Generates and returns cron config from DB */ private function generate_cron_config() { + $config_template = file_get_contents( __DIR__ . '/../templates/config.ini.mustache' ); $crons = Cron::all(); @@ -318,7 +316,7 @@ public function run_now( $args ) { */ public function delete( $args ) { - $id = $args[0]; + $id = $args[0]; $cron = Cron::find( $id ); if ( ! $cron ) { @@ -333,18 +331,26 @@ public function delete( $args ) { /** - * Returns php container name of a site + * Returns php container name of a site. + * + * @param string $site Name of the site whose container name is needed. + * + * @return string Container name. */ private function site_php_container( $site ) { + return str_replace( '.', '', $site ) . '_php_1'; } /** - * Ensures given command will not create problem with INI syntax + * 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. */ private function validate_command( $command ) { - // 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 + 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.' ); } @@ -353,6 +359,13 @@ private function validate_command( $command ) { } } + /** + * 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;