-
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
Prevent running again already running cron group #12497
Changes from 15 commits
c959393
702c93a
fcda71d
016387c
23cb078
b69945b
071df9f
7a88a9a
2972e21
4ad9c5d
1e546c8
47759c8
dd0b861
d1b56b2
c2e5520
7992e1e
2a7099e
79310b9
229476f
d12581e
bf8409b
209b2bd
c8be788
83b17fe
79d60cb
50dcc3d
175229e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,6 +56,16 @@ class ProcessCronQueueObserver implements ObserverInterface | |
*/ | ||
const SECONDS_IN_MINUTE = 60; | ||
|
||
/** | ||
* How long to wait for cron group to become unlocked | ||
*/ | ||
const LOCK_TIMEOUT = 5; | ||
|
||
/** | ||
* Static lock prefix for cron group locking | ||
*/ | ||
const LOCK_PREFIX = 'CRON_GROUP_'; | ||
|
||
/** | ||
* @var \Magento\Cron\Model\ResourceModel\Schedule\Collection | ||
*/ | ||
|
@@ -116,6 +126,11 @@ class ProcessCronQueueObserver implements ObserverInterface | |
*/ | ||
private $state; | ||
|
||
/** | ||
* @var \Magento\Framework\Lock\LockManagerInterface | ||
*/ | ||
private $lockManager; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
|
@@ -138,6 +153,7 @@ class ProcessCronQueueObserver implements ObserverInterface | |
* @param \Magento\Framework\Process\PhpExecutableFinderFactory $phpExecutableFinderFactory | ||
* @param \Psr\Log\LoggerInterface $logger | ||
* @param \Magento\Framework\App\State $state | ||
* @param \Magento\Framework\Lock\LockManagerInterface $lockManager | ||
* @SuppressWarnings(PHPMD.ExcessiveParameterList) | ||
*/ | ||
public function __construct( | ||
|
@@ -151,7 +167,8 @@ public function __construct( | |
\Magento\Framework\Stdlib\DateTime\DateTime $dateTime, | ||
\Magento\Framework\Process\PhpExecutableFinderFactory $phpExecutableFinderFactory, | ||
\Psr\Log\LoggerInterface $logger, | ||
\Magento\Framework\App\State $state | ||
\Magento\Framework\App\State $state, | ||
\Magento\Framework\Lock\LockManagerInterface $lockManager | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't BC be preserved? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @adrian-martinez-interactiv4, We treat Observers and Plugins as internal implementation and don't try to preserve BC for them |
||
) { | ||
$this->_objectManager = $objectManager; | ||
$this->_scheduleFactory = $scheduleFactory; | ||
|
@@ -164,6 +181,7 @@ public function __construct( | |
$this->phpExecutableFinder = $phpExecutableFinderFactory->create(); | ||
$this->logger = $logger; | ||
$this->state = $state; | ||
$this->lockManager = $lockManager; | ||
} | ||
|
||
/** | ||
|
@@ -186,8 +204,6 @@ public function execute(\Magento\Framework\Event\Observer $observer) | |
$phpPath = $this->phpExecutableFinder->find() ?: 'php'; | ||
|
||
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 | ||
|
@@ -211,6 +227,21 @@ public function execute(\Magento\Framework\Event\Observer $observer) | |
continue; | ||
} | ||
|
||
// Note: we acquire the lock here instead of above, as it should be taken by standalone (child) process, | ||
// not by the parent process. | ||
if (!$this->lockManager->acquireLock(self::LOCK_PREFIX . $groupId, self::LOCK_TIMEOUT)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Regarding the groups vs jobs discussion. Groups are just logical grouping for the user. There is no guarantee that different groups would not have dependent jobs. So locking mechanism should be implemented per job. Dependent jobs is a good idea for the future cron evolution. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we have any examples where one cron job has dependency to another one? Also currently they could be ran in parallel, so we already have this issue. Not sure we need to introduce fix for it as part of this PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I previously commented that I would prefer cron groups being removed, but let's wait comments from Core team. At this moment Groups are logical grouping, which would itself be ideal for this use. I can see scenario where example product import related jobs could be put into the same group, so that they do not run parallel, causing less stress on indexing and avoiding possible deadlocks. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For now, groups are used for grouping settings (cron run frequency) and it's not guaranteed that jobs from different groups can be run in parallel. As I mentioned before, I agree that it would be good to implement cron job dependencies, so some jobs could run in parallel. For now, jobs should run consequently, independently on the group. Note: I represent core team, and discussed this question internally. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @buskamuza I am not sure how to proceed with this PR after your comment. We can move locking mechanism outside "foreach" loop and have single global lock for cron runner, at least as a stop gap measure. Yet I would like to see Magento being able to run at least some tasks parallel. Cron groups currently implement a toggle "run in a separate process". Would it be safe to assume those groups can run parallel to main process, otherwise what would be the purpose of this setting? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As cron already runs per groups, let's just leave your implementation of locking in scope of this PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just for clarification: no further actions are needed here. Just review other comments in the PR. Thanks. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After internal discussion with a development team that faced some issue with 3rd party module, we thought that maybe bit more strict path could be taken here. Implementation could be changed to use single global lock for all groups except for those that have "run in a separate process" enabled. Thoughts on this? I would say it's safer way to go. However even my current solution is better than what we have now, as there's no limitation whatsoever. |
||
$this->logger->warning( | ||
sprintf( | ||
"Could not acquire lock for cron group: %s, skipping run", | ||
$groupId | ||
) | ||
); | ||
continue; | ||
} | ||
|
||
$this->_cleanup($groupId); | ||
$this->_generate($groupId); | ||
|
||
/** @var \Magento\Cron\Model\Schedule $schedule */ | ||
foreach ($pendingJobs as $schedule) { | ||
$jobConfig = isset($jobsRoot[$schedule->getJobCode()]) ? $jobsRoot[$schedule->getJobCode()] : null; | ||
|
@@ -247,6 +278,8 @@ public function execute(\Magento\Framework\Event\Observer $observer) | |
} | ||
$schedule->save(); | ||
} | ||
|
||
$this->lockManager->releaseLock(self::LOCK_PREFIX . $groupId); | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
/** | ||
* \Magento\Framework\Lock\Backend\Database test case | ||
*/ | ||
namespace Magento\Framework\Lock\Backend; | ||
|
||
class DatabaseTest extends \PHPUnit\Framework\TestCase | ||
{ | ||
/** | ||
* @var \Magento\Framework\Lock\Backend\Database | ||
*/ | ||
private $model; | ||
|
||
/** | ||
* @var \Magento\Framework\ObjectManagerInterface | ||
*/ | ||
private $objectManager; | ||
|
||
protected function setUp() | ||
{ | ||
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); | ||
$this->model = $this->objectManager->create(\Magento\Framework\Lock\Backend\Database::class); | ||
} | ||
|
||
public function testLockAndRelease() | ||
{ | ||
$name = 'test_lock'; | ||
|
||
$this->assertFalse($this->model->isLocked($name)); | ||
|
||
$this->assertTrue($this->model->acquireLock($name)); | ||
$this->assertTrue($this->model->isLocked($name)); | ||
|
||
$this->assertTrue($this->model->releaseLock($name)); | ||
$this->assertFalse($this->model->isLocked($name)); | ||
} | ||
|
||
public function testReleaseLockWithoutExistingLock() | ||
{ | ||
$name = 'test_lock'; | ||
|
||
$this->assertFalse($this->model->isLocked($name)); | ||
$this->assertFalse($this->model->releaseLock($name)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
namespace Magento\Framework\Lock\Backend; | ||
|
||
use Magento\Framework\App\ResourceConnection; | ||
use Magento\Framework\Exception\InputException; | ||
use Magento\Framework\Phrase; | ||
|
||
class Database implements \Magento\Framework\Lock\LockManagerInterface | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be great to cover this class with unit and integration tests. Could you add them? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, I'll work on implementing some test cases for this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Test cases added. |
||
{ | ||
/** @var ResourceConnection */ | ||
private $resource; | ||
|
||
public function __construct( | ||
ResourceConnection $resource | ||
) { | ||
$this->resource = $resource; | ||
} | ||
|
||
/** | ||
* Sets a lock for name | ||
* | ||
* @param string $name lock name | ||
* @param int $timeout How long to wait lock acquisition in seconds, negative value means infinite timeout | ||
* @return bool | ||
* @throws InputException | ||
*/ | ||
public function acquireLock(string $name, int $timeout = -1): bool | ||
{ | ||
$this->checkLength($name); | ||
|
||
return (bool)$this->resource->getConnection()->query("SELECT GET_LOCK(?, ?);", [(string)$name, (int)$timeout]) | ||
->fetchColumn(); | ||
} | ||
|
||
/** | ||
* Releases a lock for name | ||
* | ||
* @param string $name lock name | ||
* @return bool | ||
* @throws InputException | ||
*/ | ||
public function releaseLock(string $name): bool | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add |
||
{ | ||
$this->checkLength($name); | ||
|
||
return (bool)$this->resource->getConnection()->query("SELECT RELEASE_LOCK(?);", [(string)$name])->fetchColumn(); | ||
} | ||
|
||
/** | ||
* Tests of lock is set for name | ||
* | ||
* @param string $name lock name | ||
* @return bool | ||
* @throws InputException | ||
*/ | ||
public function isLocked(string $name): bool | ||
{ | ||
$this->checkLength($name); | ||
|
||
return (bool)$this->resource->getConnection()->query("SELECT IS_USED_LOCK(?);", [(string)$name])->fetchColumn(); | ||
} | ||
|
||
/** | ||
* Checks for max length of lock name | ||
* | ||
* Limited to 64 characters in MySQL. | ||
* | ||
* @param string $name | ||
* @throws InputException | ||
*/ | ||
private function checkLength(string $name) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add phpdoc block there? |
||
{ | ||
if (strlen($name) > 64) { | ||
throw new InputException(new Phrase('Lock name too long')); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add full name (including prefix) to the exception message, so it's easy to understand what name caused the ussed. |
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
namespace Magento\Framework\Lock; | ||
|
||
/** | ||
* Interface of a lock manager | ||
* | ||
* @api | ||
*/ | ||
interface LockManagerInterface | ||
{ | ||
/** | ||
* Sets a lock | ||
* | ||
* @param string $name lock name | ||
* @param int $timeout How long to wait lock acquisition in seconds, negative value means infinite timeout | ||
* @return bool | ||
* @api | ||
*/ | ||
public function acquireLock(string $name, int $timeout = -1): bool; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe better to have lock($name), unlock($name), tryLock($name, $timeout) methods? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can change to lock and unlock if there's consensus these are better action verbs. However I would avoid tryLock as ambiguous term, for example in Java is has different meaning than just testing the lock status. However testLock could be one alternative? EDIT: lock and unlock seem to be common terms in eg. Linux kernel and also Java, changing this implementation to conform to those wise spread terms. |
||
|
||
/** | ||
* Releases a lock | ||
* | ||
* @param string $name lock name | ||
* @return bool | ||
* @api | ||
*/ | ||
public function releaseLock(string $name): bool; | ||
|
||
/** | ||
* Tests if lock is set | ||
* | ||
* @param string $name lock name | ||
* @return bool | ||
* @api | ||
*/ | ||
public function isLocked(string $name): bool; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Lock library | ||
|
||
Lock library provides mechanism to acquire Magento system-wide lock. Default implementation is based on MySQL locks, where any locks are automatically released on connection close. | ||
|
||
The library provides interface *LockManagerInterface* which provides following methods: | ||
* *acquireLock* - Acquires a named lock | ||
* *releaseLock* - Releases a named lock | ||
* *isLocked* - Tests if a named lock exists |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would not hard-code timeout. Some processes can take more than 5min based on the amount of the processed data. The easiest solution would be to make it configurable (either in Admin Panel or in config.php). Better solution could be to adjust automatically (e.g., make each process output something and if there is no output consider it hung and release the lock), but it might be a more complex solution.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is actually five seconds, after that we give up and continue to the next group.
Reasoning being: cron:run is triggered from system crontab every minute, so next check will happen within so small period, that it does not make sense to wait for a long time. I suppose this could be reduced to even one or zero seconds (skip group immediately if lock could not be taken).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense. Thanks. I think it's ok to leave it as is (5s).