magento2: Cron starts when it's already running
I noticed that when I start a cron that’s already running it will run again. For example: I have a task that takes 15 minutes, but the cron executes it’s every 5 minutes. In the database the cron gets the status running and the other scheduled jobs have pending. But after 5 minutes, the other job (with the same code) also starts to run. So now I have the same job running twice!
Preconditions
- Magento 2.1.7
Steps to reproduce
- Create a cron that takes a while (for example, sleep 15 minutes)
- Schedule it to run every 5 minutes
Expected result
The other jobs should wait for the first job to complete, even though they are scheduled to run every 5 minutes.
Actual result
The new job starts, ignoring the fact that it’s already running…
I’m not sure if this is intended behavior or a bug. Am I expected to create my own lock / flag for this?
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 3
- Comments: 26 (12 by maintainers)
@ihor-sviziev
We’re a hosting provider with many live customers on M2 with this problem. We need a fix right now and one that works for every version.
We don’t know what Magento’s own plans are for a fix. This cron issue has been open for a very long time. It has not been properly addressed. It is impacting customers severely. See 1.
We submitted a PR with fixes initially. No response. See 1.
PR merges are very slow. Releases with fixes are very slow. See 1.
M2 fixes are not being back ported. Customers on live M2 stores are not able to upgrade to the latest version. See 1.
We’re happy to work with the core team to get this merged into the core. We will be at Imagine in April at the hackathon and will discuss it further with the comm eng team. See 1.
@ihor-sviziev , I can just assume but I saw tickets and problems with already created Pull Requests being ignored for months. And as we know, major bugfixes often just get into the next major release, which happens once a year.
As users working with production environments, we need solutions asap. Same happend when I found out paypal is no longer working starting with 2.2.0 - needed a community module like this to fix this.
We wrote an extension to fix these bugs, speed up performance, and control the execution of tasks: https://github.com/magemojo/m2-ce-cron
@ericvhileman why have you decided to create separate module instead creating PR with all your improvements?
@s-hoffman Indeed, it looks like this was introduced in 2.2 here https://github.com/magento/magento2/commit/eacb7024e2640f71199b0bf943a4409d5d883548
Either it does not work, or if it does it causes even larger issues. If we blindly check for “running” status, since crashed job will cause that job to never run again until the row is removed from schedule table.
In my opinion MySQL based locking which gets released when connection closes is better solution here, as it actually depends on the running process.
@s-hoffman I think what Magento does in $schedule->tryLockJob() is locking individual scheduled execution run of a job, so that specific job run is not triggered again. What it does not prevent is running the same job code (but different scheduled execution run) at the same time, and it was never intended to do so. $schedule is referring to a row in cron_schedule table, where there are multiple rows per job code.
What’s the state of this? We have 2.2.2. CE running and cron is not running correctly and needs to be fixed asap.
This issue also exists in CE 2.2.x. Its hilariously server breaking.
@gabrielqs-redstage reassigned.
@gabrielqs-redstage Sorry I was already working on it yesterday and sent the PR. It was not marked bc I forgot it while the ticket was open on my side.
Hey there. I’m working on it #SQUASHTOBERFEST