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

Prevent running again already running cron group #12497

Merged
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
c959393
Add LockManager and Backend to Framework functionalities
paveq Nov 30, 2017
702c93a
Implement Locker in CronQueueObserver
paveq Nov 30, 2017
fcda71d
Do cleanup and generate of schedules under cron group lock
paveq Nov 30, 2017
016387c
Add scalar type hints, slight change of wording
paveq Nov 30, 2017
23cb078
Code style fix
paveq Nov 30, 2017
b69945b
Add static prefix for cron locks
paveq Dec 1, 2017
071df9f
Add doc block
paveq Dec 1, 2017
7a88a9a
Add unit and integration tests for locking
paveq Dec 1, 2017
2972e21
Fix failing tests
paveq Dec 1, 2017
4ad9c5d
Amend docblocks
paveq Dec 1, 2017
1e546c8
Add README for Lock library, fix issue with lock release, add a comment
paveq Dec 1, 2017
47759c8
rename setLock to acquireLock for consistency
paveq Dec 1, 2017
dd0b861
Add lockManagerMock for ProcessCronQueueObserverTest
paveq Dec 1, 2017
d1b56b2
Code style fix
paveq Dec 1, 2017
c2e5520
Fix ProcessCronQueueObserverTest
paveq Dec 1, 2017
7992e1e
Add DB name as lock prefix
paveq Dec 5, 2017
2a7099e
Fix failing unit test
paveq Dec 5, 2017
79310b9
Fix code style violations
paveq Dec 5, 2017
229476f
Add checks for pre-MySQL 5.7 locking, print out lock name if too long
paveq Feb 21, 2018
d12581e
Fix failing test case
paveq Feb 21, 2018
bf8409b
Fix failing static test
paveq Feb 21, 2018
209b2bd
Simplify method verbs in LockInterface
paveq Feb 22, 2018
c8be788
#12497 Prevent running again already running cron group
kandy Mar 24, 2018
83b17fe
Merge remote-tracking branch 'mainline/2.2-develop' into cron_group_l…
kandy Mar 25, 2018
79d60cb
#12497 Prevent running again already running cron group
kandy Mar 25, 2018
50dcc3d
magento/magento2#12497
Mar 28, 2018
175229e
magento/magento2#12497
Mar 28, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 34 additions & 3 deletions app/code/Magento/Cron/Observer/ProcessCronQueueObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Contributor

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.

Copy link
Contributor Author

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).

Copy link
Contributor

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).


/**
* Static lock prefix for cron group locking
*/
const LOCK_PREFIX = 'CRON_GROUP_';

/**
* @var \Magento\Cron\Model\ResourceModel\Schedule\Collection
*/
Expand Down Expand Up @@ -116,6 +126,11 @@ class ProcessCronQueueObserver implements ObserverInterface
*/
private $state;

/**
* @var \Magento\Framework\Lock\LockManagerInterface
*/
private $lockManager;

/**
* @var array
*/
Expand All @@ -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(
Expand All @@ -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

Choose a reason for hiding this comment

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

Shouldn't BC be preserved?

Copy link
Contributor

Choose a reason for hiding this comment

The 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;
Expand All @@ -164,6 +181,7 @@ public function __construct(
$this->phpExecutableFinder = $phpExecutableFinderFactory->create();
$this->logger = $logger;
$this->state = $state;
$this->lockManager = $lockManager;
}

/**
Expand All @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -247,6 +276,8 @@ public function execute(\Magento\Framework\Event\Observer $observer)
}
$schedule->save();
}

$this->lockManager->releaseLock($groupId);
}
}

Expand Down
1 change: 1 addition & 0 deletions app/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
<preference for="Magento\Framework\Locale\ListsInterface" type="Magento\Framework\Locale\TranslatedLists" />
<preference for="Magento\Framework\Locale\AvailableLocalesInterface" type="Magento\Framework\Locale\Deployed\Codes" />
<preference for="Magento\Framework\Locale\OptionInterface" type="Magento\Framework\Locale\Deployed\Options" />
<preference for="Magento\Framework\Lock\LockManagerInterface" type="Magento\Framework\Lock\Backend\Database" />
<preference for="Magento\Framework\Api\AttributeTypeResolverInterface" type="Magento\Framework\Reflection\AttributeTypeResolver" />
<preference for="Magento\Framework\Api\Search\SearchResultInterface" type="Magento\Framework\Api\Search\SearchResult" />
<preference for="Magento\Framework\Api\Search\SearchCriteriaInterface" type="Magento\Framework\Api\Search\SearchCriteria"/>
Expand Down
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;
Copy link
Contributor

@ihor-sviziev ihor-sviziev Dec 1, 2017

Choose a reason for hiding this comment

The 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));
}
}
72 changes: 72 additions & 0 deletions lib/internal/Magento/Framework/Lock/Backend/Database.php
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
Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, I'll work on implementing some test cases for this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you add @throws to phpdoc block there?

{
$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)
Copy link
Contributor

Choose a reason for hiding this comment

The 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'));
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

}
}
}
44 changes: 44 additions & 0 deletions lib/internal/Magento/Framework/Lock/LockManagerInterface.php
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');
}
}