Files
MageSail/Magesail/Cron/DailyMonitor.php
T
2026-03-12 17:05:22 -05:00

44 lines
1.4 KiB
PHP

<?php
namespace MageSail\Magesail\Cron;
use Psr\Log\LoggerInterface;
use Magento\Framework\Shell;
use Magento\Framework\App\Filesystem\DirectoryList;
class DailyMonitor
{
protected $logger;
protected $shell;
protected $directoryList;
public function __construct(
LoggerInterface $logger,
Shell $shell,
DirectoryList $directoryList
) {
$this->logger = $logger;
$this->shell = $shell;
$this->directoryList = $directoryList;
}
public function execute()
{
$pidFile = $this->directoryList->getPath(DirectoryList::VAR_DIR) . '/magesail.pid';
if (file_exists($pidFile)) {
$pid = (int) trim(file_get_contents($pidFile));
try {
$this->shell->execute("ps -p $pid > /dev/null 2>&1");
$isRunning = true;
} catch (\Exception $e) {
$isRunning = false;
}
if (!$isRunning) {
unlink($pidFile);
$keyFile = $this->directoryList->getPath(DirectoryList::VAR_DIR) . '/magesail.key';
if (file_exists($keyFile)) {
unlink($keyFile);
}
$this->logger->warning('Magesail tunnel crashed; cleaned up PID files.');
// Optional: Auto-restart here if config allows
}
}
$this->logger->info('Magesail cron monitor executed.');
}
}