Skip to content

JWK-76 unlock lock files #72

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

Open
wants to merge 1 commit into
base: master
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
86 changes: 86 additions & 0 deletions Cron/CheckLockFiles.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php
/**
* @package Walkthechat\Walkthechat
*
* @author WalktheChat <info@walkthechat.com>
* @copyright 2021 Walkthechat
*
* @license See LICENSE.txt for license details.
*/

namespace Walkthechat\Walkthechat\Cron;

/**
* Class CheckLockFiles
*
* @package Walkthechat\Walkthechat\Cron
*/
class CheckLockFiles
{
/**
* @var \Magento\Framework\Filesystem
*/
protected $filesystem;

/**
* @var integer
*/
const LOCK_FILE_MINUTES = 30;

/**
* CheckLockFiles constructor.
* @param \Magento\Framework\Filesystem $filesystem
*/
public function __construct(
\Magento\Framework\Filesystem $filesystem
)
{
$this->filesystem = $filesystem;
}

/**
*
* @throws \Magento\Framework\Exception\CronException
* @throws \Exception
*/
public function execute()
{
$directory = $this->filesystem->getDirectoryWrite(\Magento\Framework\App\Filesystem\DirectoryList::VAR_DIR);

if ($directory->isExist(\Walkthechat\Walkthechat\Cron\ProcessQueue::QUEUE_LOCK_FILE_NAME_LOCKED)) {
$stat = $directory->stat(\Walkthechat\Walkthechat\Cron\ProcessQueue::QUEUE_LOCK_FILE_NAME_LOCKED);

if (isset($stat['ctime']) && $stat['ctime'] < (time() - self::LOCK_FILE_MINUTES * 60)) {
try {
$directory->renameFile(\Walkthechat\Walkthechat\Cron\ProcessQueue::QUEUE_LOCK_FILE_NAME_LOCKED, \Walkthechat\Walkthechat\Cron\ProcessQueue::QUEUE_LOCK_FILE_NAME_UNLOCKED);
} catch (\Magento\Framework\Exception\FileSystemException $e) {
throw new \Magento\Framework\Exception\CronException(__('Unable to unlock the walkthechat queue.'));
}
}
}

if ($directory->isExist(\Walkthechat\Walkthechat\Cron\SyncImages::SYNC_IMAGES_LOCK_FILE_NAME_LOCKED)) {
$stat = $directory->stat(\Walkthechat\Walkthechat\Cron\SyncImages::SYNC_IMAGES_LOCK_FILE_NAME_LOCKED);

if (isset($stat['ctime']) && $stat['ctime'] < (time() - self::LOCK_FILE_MINUTES * 60)) {
try {
$directory->renameFile(\Walkthechat\Walkthechat\Cron\SyncImages::SYNC_IMAGES_LOCK_FILE_NAME_LOCKED, \Walkthechat\Walkthechat\Cron\SyncImages::SYNC_IMAGES_LOCK_FILE_NAME_UNLOCKED);
} catch (\Magento\Framework\Exception\FileSystemException $e) {
throw new \Magento\Framework\Exception\CronException(__('Unable to unlock the walkthechat sync images.'));
}
}
}

if ($directory->isExist(\Walkthechat\Walkthechat\Cron\SyncInventory::LOCK_FILE_NAME_LOCKED)) {
$stat = $directory->stat(\Walkthechat\Walkthechat\Cron\SyncInventory::LOCK_FILE_NAME_LOCKED);

if (isset($stat['ctime']) && $stat['ctime'] < (time() - self::LOCK_FILE_MINUTES * 60)) {
try {
$directory->renameFile(\Walkthechat\Walkthechat\Cron\SyncInventory::LOCK_FILE_NAME_LOCKED, \Walkthechat\Walkthechat\Cron\SyncInventory::LOCK_FILE_NAME_UNLOCKED);
} catch (\Magento\Framework\Exception\FileSystemException $e) {
throw new \Magento\Framework\Exception\CronException(__('Unable to unlock the walkthechat inventory.'));
}
}
}
}
}
3 changes: 3 additions & 0 deletions etc/crontab.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@
<job name="walkthechat_sync_orders" instance="Walkthechat\Walkthechat\Cron\SyncOrders" method="execute">
<schedule>0 * * * *</schedule>
</job>
<job name="walkthechat_check_lock_files" instance="Walkthechat\Walkthechat\Cron\CheckLockFiles" method="execute">
<schedule>*/5 * * * *</schedule>
</job>
</group>
</config>