Skip to content

Commit

Permalink
Fixed locking - follow up to #7386 (#7418)
Browse files Browse the repository at this point in the history
* Fixed locking - follow up to #7386

* Update lib/Maintenance/Tasks/CacheCleanupTask.php

Co-authored-by: Sebastian Blank <blank@data-factory.net>

* Update lib/Maintenance/Tasks/LowQualityImagePreviewTask.php

Co-authored-by: Sebastian Blank <blank@data-factory.net>

* micro-optimization

* micro-optimization

Co-authored-by: Sebastian Blank <blank@data-factory.net>
  • Loading branch information
brusch and blankse committed Oct 30, 2020
1 parent 28ee424 commit 4a460fe
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 16 deletions.
Expand Up @@ -1074,7 +1074,7 @@ protected function getLock(): LockInterface
*/
protected function checkIndexLock(bool $throwException = true): bool
{
if ($this->getLock()->isAcquired()) {
if (!$this->getLock()->acquire()) {
$errorMessage = sprintf('Index is currently locked by "%s" as reindex is in progress.', self::REINDEXING_LOCK_KEY);
if ($throwException) {
throw new \Exception($errorMessage);
Expand Down
3 changes: 1 addition & 2 deletions lib/Maintenance/Tasks/CacheCleanupTask.php
Expand Up @@ -54,9 +54,8 @@ public function __construct(CoreHandlerInterface $cacheHandler, LoggerInterface
*/
public function execute()
{
if (!$this->lock->isAcquired() && date('H') <= 4) {
if (date('H') <= 4 && $this->lock->acquire()) {
// execution should be only sometime between 0:00 and 4:59 -> less load expected
$this->lock->acquire();
$this->logger->debug('Execute purge() on cache handler');
$this->cacheHandler->purge();
} else {
Expand Down
3 changes: 1 addition & 2 deletions lib/Maintenance/Tasks/LowQualityImagePreviewTask.php
Expand Up @@ -46,9 +46,8 @@ public function __construct(LoggerInterface $logger, LockFactory $lockFactory)
*/
public function execute()
{
if (!$this->lock->isAcquired() && date('H') <= 4) {
if (date('H') <= 4 && $this->lock->acquire()) {
// execution should be only sometime between 0:00 and 4:59 -> less load expected
$this->lock->acquire();
$this->logger->debug('Execute low quality image preview generation');

$listing = new Asset\Listing();
Expand Down
21 changes: 10 additions & 11 deletions models/Asset/Document/ImageThumbnail.php
Expand Up @@ -93,17 +93,16 @@ public function generate($deferredAllowed = true)
\Pimcore\File::mkdir(dirname($path));
}

$lock = \Pimcore::getContainer()->get(LockFactory::class)->createLock('document-thumbnail-' . $this->asset->getId() . '-' . $this->page);

if (!is_file($path) && !$lock->isAcquired()) {
$lock->acquire();
$converter->saveImage($path, $this->page);
$generated = true;
$lock->release();
} elseif ($lock->isAcquired()) {
$this->filesystemPath = PIMCORE_WEB_ROOT . '/bundles/pimcoreadmin/img/please-wait.png';

return;
if(!is_file($path)) {
$lock = \Pimcore::getContainer()->get(LockFactory::class)->createLock('document-thumbnail-' . $this->asset->getId() . '-' . $this->page);
if ($lock->acquire()) {
$converter->saveImage($path, $this->page);
$generated = true;
$lock->release();
} else {
$this->filesystemPath = PIMCORE_WEB_ROOT . '/bundles/pimcoreadmin/img/please-wait.png';
return;
}
}
}

Expand Down

0 comments on commit 4a460fe

Please sign in to comment.