Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

M2 recipe : handle cron stop in try catch #3828

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/contrib/cachetool.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,15 @@ Clear opcache cache


### cachetool:clear:apcu
[Source](https://github.com/deployphp/deployer/blob/master/contrib/cachetool.php#L99)
[Source](https://github.com/deployphp/deployer/blob/master/contrib/cachetool.php#L100)

Clears APCu system cache.

Clear APCu cache


### cachetool:clear:stat
[Source](https://github.com/deployphp/deployer/blob/master/contrib/cachetool.php#L110)
[Source](https://github.com/deployphp/deployer/blob/master/contrib/cachetool.php#L111)

Clears file status and realpath caches.

Expand Down
30 changes: 27 additions & 3 deletions docs/recipe/magento2.md
Original file line number Diff line number Diff line change
Expand Up @@ -690,8 +690,32 @@ Cleanup cache id_prefix env files.
After successful deployment, move the tmp_env.php file to env.php ready for next deployment


### magento:cron:stop
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L505)

Remove cron from crontab and kill running cron jobs.

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');
```


### magento:cron:install
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L521)

Install cron in crontab.

Install cron in crontab
To use this feature, add the following to your deployer scripts:
```php
after('magento:upgrade:db', 'magento:cron:install');
```


### artifact:prepare
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L499)
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L527)

Prepares an artifact on the target server.

Expand All @@ -711,7 +735,7 @@ This task is group task which contains next tasks:


### artifact:finish
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L512)
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L540)

Executes the tasks after artifact is released.

Expand All @@ -726,7 +750,7 @@ This task is group task which contains next tasks:


### artifact:deploy
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L521)
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L549)

Actually releases the artifact deployment.

Expand Down
32 changes: 32 additions & 0 deletions recipe/magento2.php
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,38 @@ 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')) {
try {
run('{{bin/php}} {{previous_release}}/{{magento_dir}}/bin/magento cron:remove');
} catch (RunException $e) {
// do nothing
}
}

run('pgrep -U "$(id -u)" -f "bin/magento +(cron:run|queue:consumers:start)" | xargs -r kill');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like this approach. What if cron job does not handle SIGTERM?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand, however I'm not sure to be able to improve this code (that is already merged in 7.4.0)

});

/**
* 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', [
Expand Down