-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
Instantly apply configuration changes in the cron schedule #9957
Conversation
This is in preparation of listening for changes in the configuration to check if a scheduled cron job is still enabled. When you disable a cron job in te config and flush the config cache, the cron observer should remove any scheduled cron jobs that are pending. If the cron observer runs the jobs before cleaning up, cron jobs that have been disabled will still run 1 time before they are removed from the schedule after disabling them. The extra advantage of this setup is that cron jobs will run directly after they have been scheduled, in the same run. So you don't have to run the cron job twice to see jobs executing. One for scheduling and one for running. If there is a perfectly good reason not to generate the schedule and clean jobs before running them I would very much like to know.
As I heard from @ishakhsuvarov Magento does not encourage usage of `protected` methods, because they do not encourage inheritance-based API. So composition in favor of inheritance. It's easier to maintain and reuse. I myself have already encountered scenarios where I wanted to use methods in this observer elsewhere in custom code, but I had to write duplicate code because these methods were `protected`.
When you change a cron expression in the config, you do not want jobs that were scheduled ahead to be executed on the old config cron expression. The cleanup makes the config change take immediate effect.
Some unit tests did not flag as failed even though the thing they were testing did not occur. I have rewritten these tests to be more responsive when things go wrong. There is however a big step to be made to make these tests more readable and more consistent.
of this class. The public functions are either useful outside of this class or useful to disable or extend using plugins
…and if disabled jobs will be deleted
Hi @ajpevers Thank you for continuous contribution to Magento 2 project. Great job! This comment is less about this particular PR, but more about overall contribution. Please, let me know if you have any questions. |
@okorshenko you're right, @vkublytskyi and @ishakhsuvarov have pointed out to me what I should take into account and how I should work around backward incompatibility in specific cases. Thanks for the heads-up. From now on I'll keep it in mind. I've rolled back backward incompatible rafactoring that I've done. If there is a need to refactor these files I'll leave it to you and I'll just add the functional changes. ... and I see that I've made a typo in the commit messages just now... I meant backward of course. |
@ajpevers Hi. After applying your PR nothing changed in my Magento behavior. My Steps to check issue:
Actual Result: jobs with status pending is still exist in table. Note: And in step №7 in your scenario new jobs with new expression should be displayed ? If yes then in my case there are is missing. Displayed only jobs with old expression with status success and pending. Could you help me with checking your PR ??? Thanks. |
@ajpevers Sorry for disturbing I had problem on my side. Your PR works fine. |
…dule #9957 - cron observer must have only 1 public method
Hi guys, I've installed Magento 2.2 with this change in it and with my schedules strange things are happened. There are often duplicated schedules, created exactly at the same time and scheduled exactly at the same minute. There are not regular. Could you check if this is happening with you? In 2.1 everythig works fine and this commit is a bridge between old and new |
@maqlec it is still possible for Magento to run multiple overlapping instances of You could try to apply this patch to eliminate that scenario if you are running linux and have diff --git a/Observer/ProcessCronQueueObserver.php b/Observer/ProcessCronQueueObserver.php
index f772a6c0c8..3e3d0ccdce 100644
--- a/Observer/ProcessCronQueueObserver.php
+++ b/Observer/ProcessCronQueueObserver.php
@@ -201,6 +201,13 @@ class ProcessCronQueueObserver implements ObserverInterface
) == 1
)
) {
+ if (shell_exec(
+ 'pgrep -f -x \'' . $phpPath . ' ' . BP . '/bin/magento cron:run --group=' . $groupId . ' --' . Cli::INPUT_KEY_BOOTSTRAP . '='
+ . self::STANDALONE_PROCESS_STARTED . '=1\''
+ )) {
+ continue;
+ }
+
$this->_shell->execute(
$phpPath . ' %s cron:run --group=' . $groupId . ' --' . Cli::INPUT_KEY_BOOTSTRAP . '='
. self::STANDALONE_PROCESS_STARTED . '=1', |
I've followed your change and I resolved my problem by moving one line at original place. diff --git a/Observer/ProcessCronQueueObserver.php b/Observer/ProcessCronQueueObserver.php
index f772a6c..bf99dc0 100644
--- a/Observer/ProcessCronQueueObserver.php
+++ b/Observer/ProcessCronQueueObserver.php
@@ -187,7 +187,6 @@ class ProcessCronQueueObserver implements ObserverInterface
foreach ($jobGroupsRoot as $groupId => $jobsRoot) {
$this->_cleanup($groupId);
- $this->_generate($groupId);
if ($this->_request->getParam('group') !== null
&& $this->_request->getParam('group') !== '\'' . ($groupId) . '\''
&& $this->_request->getParam('group') !== $groupId
@@ -247,6 +246,7 @@ class ProcessCronQueueObserver implements ObserverInterface
}
$schedule->save();
}
+ $this->_generate($groupId);
}
} |
Description
This pull request:
This fixes #3380 (moved to forums)
Fixed Issues (if relevant)
Manual testing scenarios
cron_schedule
tablebin/magento cron:run
commandcron_schedule
table to see thatcatalog_product_outdated_price_values_cleanup
is scheduled every minuteapp/code/Magento/Catalog/etc/crontab.xml
and change the cron expression from* * * * *
to*/10 * * * *
bin/magento cron:run
commandcatalog_product_outdated_price_values_cleanup
back to* * * * *
and truncate thecron_schedule
tablebin/magento cron:run
commandcron_schedule
table to see thatcatalog_product_outdated_price_values_cleanup
is scheduled every minuteapp/code/Magento/Catalog/etc/crontab.xml
and remove the cron expression tagbin/magento cron:run
commandcatalog_product_outdated_price_values_cleanup
have been removed, and nothing elseYou can also play around with enabling/disabling the sitemap generator or another cron job that has configuration for enable/disable and schedules.
Contribution checklist