From a013fb8cc4e05384de741f2c8ec265b5cb1fff81 Mon Sep 17 00:00:00 2001 From: sagarnasit Date: Wed, 10 Oct 2018 20:03:58 +0530 Subject: [PATCH 1/4] Delete container if no crons are in table --- src/Cron_Command.php | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/Cron_Command.php b/src/Cron_Command.php index 3039e2b..6d3dfb0 100644 --- a/src/Cron_Command.php +++ b/src/Cron_Command.php @@ -1,3 +1,4 @@ + generate_cron_config(); file_put_contents( EE_ROOT_DIR . '/services/cron/config.ini', $config ); - EE_DOCKER::restart_container( 'ee-cron-scheduler' ); + EE_DOCKER::restart_container( self::EE_CRON_SCHEDULER ); } /** @@ -402,5 +407,10 @@ public function delete( $args ) { $this->update_cron_config(); EE::success( 'Deleted cron with id ' . $id ); + + $cron_entries = EE::db()->table( 'cron' )->all(); + if ( empty( $cron_entries ) ) { + EE::exec( 'docker rm -f ' . self::EE_CRON_SCHEDULER ); + } } } From ccc90e7c49c150fd7d0b67429e660717f321fd96 Mon Sep 17 00:00:00 2001 From: sagarnasit Date: Thu, 11 Oct 2018 11:55:42 +0530 Subject: [PATCH 2/4] Update ee-cron-scheduler with constant --- cron-command.php | 2 ++ src/Cron_Command.php | 19 ++++++++++--------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/cron-command.php b/cron-command.php index 68a5f89..b1a55c6 100644 --- a/cron-command.php +++ b/cron-command.php @@ -4,6 +4,8 @@ return; } +define( 'EE_CRON_SCHEDULER', 'ee-cron-scheduler' ); + $autoload = dirname( __FILE__ ) . '/vendor/autoload.php'; if ( file_exists( $autoload ) ) { require_once $autoload; diff --git a/src/Cron_Command.php b/src/Cron_Command.php index 6d3dfb0..fec6684 100644 --- a/src/Cron_Command.php +++ b/src/Cron_Command.php @@ -11,8 +11,6 @@ */ class Cron_Command extends EE_Command { - CONST EE_CRON_SCHEDULER = 'ee-cron-scheduler'; - /** * Runs cron container if it's not running */ @@ -78,10 +76,10 @@ public function __construct() { */ public function create( $args, $assoc_args ) { - if ( 'running' !== EE_DOCKER::container_status( self::EE_CRON_SCHEDULER ) ) { - $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:v' . EE_VERSION; - if ( ! EE_DOCKER::boot_container( self::EE_CRON_SCHEDULER, $cron_scheduler_run_command ) ) { - EE::error( 'There was some error in starting ee-cron-scheduler container. Please check logs.' ); + if ( 'running' !== EE_DOCKER::container_status( EE_CRON_SCHEDULER ) ) { + $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:v' . EE_VERSION; + 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.' ); } } @@ -121,6 +119,7 @@ public function create( $args, $assoc_args ) { $this->update_cron_config(); + EE::success( 'Cron created successfully' ); EE\Utils\delem_log( 'ee cron add end' ); } @@ -165,7 +164,7 @@ private function update_cron_config() { $config = $this->generate_cron_config(); file_put_contents( EE_ROOT_DIR . '/services/cron/config.ini', $config ); - EE_DOCKER::restart_container( self::EE_CRON_SCHEDULER ); + EE_DOCKER::restart_container( EE_CRON_SCHEDULER ); } /** @@ -299,6 +298,8 @@ public function update( $args, $assoc_args ) { $this->update_cron_config(); + EE::success( 'Cron update Successfully'); + EE\Utils\delem_log( 'ee cron add end' ); } @@ -408,9 +409,9 @@ public function delete( $args ) { EE::success( 'Deleted cron with id ' . $id ); - $cron_entries = EE::db()->table( 'cron' )->all(); + $cron_entries = Cron::all(); if ( empty( $cron_entries ) ) { - EE::exec( 'docker rm -f ' . self::EE_CRON_SCHEDULER ); + EE::exec( 'docker rm -f ' . EE_CRON_SCHEDULER ); } } } From 8a0d2821765906b51e9de690b481aa07083a09aa Mon Sep 17 00:00:00 2001 From: sagarnasit Date: Thu, 11 Oct 2018 12:03:52 +0530 Subject: [PATCH 3/4] Add check before defining constant --- cron-command.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cron-command.php b/cron-command.php index b1a55c6..770e5db 100644 --- a/cron-command.php +++ b/cron-command.php @@ -4,7 +4,9 @@ return; } -define( 'EE_CRON_SCHEDULER', 'ee-cron-scheduler' ); +if ( ! defined( 'EE_CRON_SCHEDULER' ) ) { + define( 'EE_CRON_SCHEDULER', 'ee-cron-scheduler' ); +} $autoload = dirname( __FILE__ ) . '/vendor/autoload.php'; if ( file_exists( $autoload ) ) { From 037a4ad46658c7d612a892202ea8a74a46efeb6a Mon Sep 17 00:00:00 2001 From: sagarnasit Date: Thu, 11 Oct 2018 12:07:07 +0530 Subject: [PATCH 4/4] Fix spacing issue --- src/Cron_Command.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Cron_Command.php b/src/Cron_Command.php index fec6684..c1398cc 100644 --- a/src/Cron_Command.php +++ b/src/Cron_Command.php @@ -77,7 +77,7 @@ public function __construct() { public function create( $args, $assoc_args ) { if ( 'running' !== EE_DOCKER::container_status( EE_CRON_SCHEDULER ) ) { - $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:v' . EE_VERSION; + $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:v' . EE_VERSION; 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.' ); }