From 8f3d6151caf3eee507d5e3451d44001af83b3285 Mon Sep 17 00:00:00 2001 From: gbobts <11231286+gbobts@users.noreply.github.com> Date: Fri, 29 Mar 2024 11:25:49 +0100 Subject: [PATCH] Add feature to stop cron before deployment and start cron after --- recipe/magento2.php | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/recipe/magento2.php b/recipe/magento2.php index 09c1b12d9..cc77dd4e6 100644 --- a/recipe/magento2.php +++ b/recipe/magento2.php @@ -494,6 +494,34 @@ function magentoDeployAssetsSplit(string $area) run('{{bin/symlink}} {{deploy_path}}/shared/' . ENV_CONFIG_FILE_PATH . ' {{release_path}}/' . ENV_CONFIG_FILE_PATH); }); +/** + * Remove cron from crontab and kill running cron jobs + * To use this feature, add the following to your deployer scripts: + * ```php + * after('magento:maintenance:enable-if-needed', 'magento:cron:stop'); + * ``` + */ +desc('Remove cron from crontab and kill running cron jobs'); +task('magento:cron:stop', function () { + if (has('previous_release')) { + run('{{bin/php}} {{previous_release}}/{{magento_dir}}/bin/magento cron:remove'); + } + + run('pgrep -U "$(id -u)" -f "bin/magento +(cron:run|queue:consumers:start)" | xargs -r kill'); +}); + +/** + * Install cron in crontab + * To use this feature, add the following to your deployer scripts: + * ```php + * after('magento:upgrade:db', 'magento:cron:install'); + * ``` + */ +desc('Install cron in crontab'); +task('magento:cron:install', function () { + run('cd {{release_or_current_path}}'); + run('{{bin/php}} {{bin/magento}} cron:install'); +}); desc('Prepares an artifact on the target server'); task('artifact:prepare', [