diff --git a/composer.json b/composer.json index d0a1b3c..dc62481 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,10 @@ "psr-4": { "": "src/" }, - "files": [ "cron-command.php" ] + "files": [ + "cron-command.php", + "src/helper/hooks.php" + ] }, "extra": { "branch-alias": { diff --git a/src/helper/hooks.php b/src/helper/hooks.php new file mode 100644 index 0000000..98db627 --- /dev/null +++ b/src/helper/hooks.php @@ -0,0 +1,62 @@ + $site_url ] ); + + if ( ! empty( $cron_jobs ) ) { + foreach ( $cron_jobs as $cron_job ) { + $cron_job->delete(); + } + regenerate_cron_config(); + } +} + +/** + * Regenerates cron config from DB. + */ +function regenerate_cron_config() { + + $config_template = file_get_contents( __DIR__ . '/../../templates/config.ini.mustache' ); + $crons = Cron::all(); + + if ( empty( $cron ) ) { + EE::exec( 'docker rm -f ' . EE_CRON_SCHEDULER ); + } + + foreach ( $crons as &$cron ) { + $job_type = 'host' === $cron->site_url ? 'job-local' : 'job-exec'; + $id = $cron->site_url . '-' . preg_replace( '/[^a-zA-Z0-9\@]/', '-', $cron->command ) . '-' . EE\Utils\random_password( 5 ); + $id = preg_replace( '/--+/', '-', $id ); + $cron->job_type = $job_type; + $cron->id = $id; + + if ( 'host' !== $cron->site_url ) { + $cron->container = str_replace( '.', '', $cron->site_url ) . '_php_1'; + } + } + + $me = new Mustache_Engine(); + $config = $me->render( $config_template, $crons ); + + file_put_contents( EE_ROOT_DIR . '/services/cron/config.ini', $config ); + EE_DOCKER::restart_container( EE_CRON_SCHEDULER ); +} + +EE::add_hook( 'site_cleanup', 'cleanup_cron_entries' );