diff --git a/src/Cron_Command.php b/src/Cron_Command.php index 5be8b14..c1d0369 100644 --- a/src/Cron_Command.php +++ b/src/Cron_Command.php @@ -1,13 +1,13 @@ 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(); @@ -155,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 = []; @@ -216,17 +216,16 @@ 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 = EE\SiteUtils\auto_site_name( $args, 'cron', 'list' ); + $args = auto_site_name( $args, 'cron', 'list' ); } if ( isset( $args[0] ) ) { $crons = Cron::where( 'site_url', $args[0] ); - } - else { + } else { $crons = Cron::all(); } @@ -245,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' ); } @@ -254,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(); @@ -317,7 +316,7 @@ public function run_now( $args ) { */ public function delete( $args ) { - $id = $args[0]; + $id = $args[0]; $cron = Cron::find( $id ); if ( ! $cron ) { @@ -332,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.' ); } @@ -352,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;