Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
"psr-4": {
"": "src/"
},
"files": [ "cron-command.php" ]
"files": [
"cron-command.php",
"src/helper/hooks.php"
]
},
"extra": {
"branch-alias": {
Expand Down
62 changes: 62 additions & 0 deletions src/helper/hooks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

if ( ! class_exists( 'EE' ) ) {
return;
}

use EE\Model\Cron;
use EE\Model\Site;

/**
* Hook to cleanup cron entries if any.
*
* @param string $site_url The site to be cleaned up.
*/
function cleanup_cron_entries( $site_url ) {

if ( ! Site::find( $site_url ) ) {
return;
}

$cron_jobs = Cron::where( [ 'site_url' => $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' );