-
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 8 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,19 @@ public function execute(\Magento\Framework\Event\Observer $observer) | |
continue; | ||
} | ||
|
||
if (!$this->lockManager->setLock(self::LOCK_PREFIX . $groupId, self::LOCK_TIMEOUT)) { | ||
$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 +276,8 @@ public function execute(\Magento\Framework\Event\Observer $observer) | |
} | ||
$schedule->save(); | ||
} | ||
|
||
$this->lockManager->releaseLock($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 | ||
*/ | ||
protected $model; | ||
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 use private visibility instead of protected (for all methods in this file)? |
||
|
||
/** | ||
* @var \Magento\Framework\ObjectManagerInterface | ||
*/ | ||
protected $objectManager; | ||
|
||
public 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->setLock($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,72 @@ | ||
<?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 | ||
*/ | ||
public function setLock(string $name, int $timeout = -1): bool | ||
{ | ||
$this->checkLength($name); | ||
|
||
return (bool)$this->resource->getConnection()->query("SELECT GET_LOCK(?, ?);", array((string)$name, (int)$timeout)) | ||
->fetchColumn(); | ||
} | ||
|
||
/** | ||
* Releases a lock for name | ||
* | ||
* @param string $name lock name | ||
* @return bool | ||
*/ | ||
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(?);", array((string)$name))->fetchColumn(); | ||
} | ||
|
||
/** | ||
* Tests of lock is set for name | ||
* | ||
* @param string $name lock name | ||
* @return bool | ||
*/ | ||
public function isLocked(string $name): bool | ||
{ | ||
$this->checkLength($name); | ||
|
||
return (bool)$this->resource->getConnection()->query("SELECT IS_USED_LOCK(?);", array((string)$name))->fetchColumn(); | ||
} | ||
|
||
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 setLock(string $name, int $timeout = -1): bool; | ||
|
||
/** | ||
* 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,80 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
namespace Magento\Framework\Lock\Test\Unit\Backend; | ||
|
||
use Magento\Framework\Lock\Backend\Database; | ||
|
||
class DatabaseTest extends \PHPUnit\Framework\TestCase | ||
{ | ||
/** | ||
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\ResourceConnection | ||
*/ | ||
private $resource; | ||
|
||
/** | ||
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\DB\Adapter\AdapterInterface | ||
*/ | ||
private $connection; | ||
|
||
/** | ||
* @var \PHPUnit_Framework_MockObject_MockObject|\Zend_Db_Statement_Interface | ||
*/ | ||
private $statement; | ||
|
||
/** | ||
* @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager | ||
*/ | ||
private $objectManager; | ||
|
||
/** @var Database $database */ | ||
private $database; | ||
|
||
protected function setUp() | ||
{ | ||
$this->connection = $this->getMockBuilder(\Magento\Framework\DB\Adapter\AdapterInterface::class) | ||
->disableOriginalConstructor() | ||
->getMockForAbstractClass(); | ||
$this->resource = $this->getMockBuilder(\Magento\Framework\App\ResourceConnection::class) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
$this->statement = $this->getMockBuilder(\Zend_Db_Statement_Interface::class) | ||
->disableOriginalConstructor() | ||
->getMockForAbstractClass(); | ||
|
||
$this->resource->expects($this->any()) | ||
->method('getConnection') | ||
->willReturn($this->connection); | ||
|
||
$this->connection->expects($this->any()) | ||
->method('query') | ||
->willReturn($this->statement); | ||
|
||
$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); | ||
|
||
/** @var Database $database */ | ||
$this->database = $this->objectManager->getObject( | ||
Database::class, | ||
['resource' => $this->resource] | ||
); | ||
} | ||
|
||
public function testSetLock() | ||
{ | ||
$this->statement->expects($this->once()) | ||
->method('fetchColumn') | ||
->willReturn(true); | ||
|
||
$this->assertTrue($this->database->setLock('testLock')); | ||
} | ||
|
||
/** | ||
* @expectedException \Magento\Framework\Exception\InputException | ||
*/ | ||
public function testSetLockWithTooLongName() | ||
{ | ||
$this->database->setLock('BbXbyf9rIY5xuAVdviQJmh76FyoeeVHTDpcjmcImNtgpO4Hnz4xk76ZGEyYALvrQu'); | ||
} | ||
} |
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).