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

Introduce new cronjob to regularly cleanup outdated lock files if fil… #39372

Open
wants to merge 1 commit into
base: 2.4-develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
39 changes: 39 additions & 0 deletions app/code/Magento/Backend/Cron/CleanLocks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* Copyright 2024 Adobe
* All Rights Reserved.
*/
declare(strict_types=1);

namespace Magento\Backend\Cron;

use Magento\Framework\Lock\Backend\FileLock;
use Magento\Framework\Lock\LockBackendFactory;
use Psr\Log\LoggerInterface;

class CleanLocks
{
/**
* @param LockBackendFactory $lockFactory
* @param LoggerInterface $logger
*/
public function __construct(
private readonly LockBackendFactory $lockFactory,
private readonly LoggerInterface $logger,
) {
}

/**
* Cron job to cleanup old locks
*/
public function execute(): void
{
$locker = $this->lockFactory->create();

if ($locker instanceof FileLock) {
$numberOfLockFilesDeleted = $locker->cleanupOldLocks();

$this->logger->info(sprintf('Deleted %d old lock files', $numberOfLockFilesDeleted));
}
}
}
7 changes: 5 additions & 2 deletions app/code/Magento/Backend/etc/crontab.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
* Copyright 2015 Adobe
* All Rights Reserved.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd">
<group id="default">
<job name="backend_clean_cache" instance="Magento\Backend\Cron\CleanCache" method="execute">
<schedule>30 2 * * *</schedule>
</job>
<job name="backend_clean_locks" instance="Magento\Backend\Cron\CleanLocks" method="execute">
<schedule>20 2 * * *</schedule>
</job>
</group>
</config>
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
* Copyright 2019 Adobe
* All Rights Reserved.
*/
declare(strict_types=1);

Expand All @@ -22,12 +22,16 @@ class FileLockTest extends \PHPUnit\Framework\TestCase
*/
private $objectManager;

/** @var string */
private string $lockPath;

protected function setUp(): void
{
$this->lockPath = '/tmp/magento-test-locks';
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
$this->model = $this->objectManager->create(
\Magento\Framework\Lock\Backend\FileLock::class,
['path' => '/tmp']
['path' => $this->lockPath]
);
}

Expand All @@ -52,4 +56,28 @@ public function testUnlockWithoutExistingLock()
$this->assertFalse($this->model->isLocked($name));
$this->assertFalse($this->model->unlock($name));
}

public function testCleanupOldFile()
{
$name = 'test_lock';

$this->assertTrue($this->model->lock($name));
$this->assertTrue($this->model->unlock($name));

touch(sprintf('%s/%s', $this->lockPath, $name), strtotime('30 hours ago'));

$this->assertEquals(1, $this->model->cleanupOldLocks());
}

public function testDontCleanupNewFile()
{
$name = 'test_lock';

$this->assertTrue($this->model->lock($name));
$this->assertTrue($this->model->unlock($name));

touch(sprintf('%s/%s', $this->lockPath, $name), strtotime('1 hour ago'));

$this->assertEquals(0, $this->model->cleanupOldLocks());
}
}
44 changes: 41 additions & 3 deletions lib/internal/Magento/Framework/Lock/Backend/FileLock.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
* Copyright 2019 Adobe
* All Rights Reserved.
*/
declare(strict_types=1);

Expand Down Expand Up @@ -42,7 +42,7 @@ class FileLock implements LockManagerInterface
/**
* The mapping list of the path lock with the file resource
*
* @var array
* @var array<string, resource>
*/
private $locks = [];

Expand Down Expand Up @@ -105,6 +105,44 @@ public function lock(string $name, int $timeout = -1): bool
return true;
}

/**
* Find lock files that haven't been touched in the last 24 hours and are unlocked, and delete those
*/
public function cleanupOldLocks(): int
{
if (!$this->fileDriver->isExists($this->path)) {
return 0;
}

$numberOfLocksDeleted = 0;
$timestamp24HoursAgo = strtotime('24 hours ago');

$lockFiles = $this->fileDriver->readDirectory($this->path);
foreach ($lockFiles as $lockFile) {
if (!$this->fileDriver->isFile($lockFile)) {
continue;
}

$modifiedTimestamp = filemtime($lockFile);
if ($timestamp24HoursAgo < $modifiedTimestamp) {
continue;
}

if ($this->isLocked(basename($lockFile))) {
continue;
}

try {
$this->fileDriver->deleteFile($lockFile);
++$numberOfLocksDeleted;
} catch (FileSystemException $exception) { // phpcs:ignore Magento2.CodeAnalysis.EmptyBlock.DetectedCatch
// do nothing
}
}

return $numberOfLocksDeleted;
}

/**
* Checks if a lock exists by name
*
Expand Down