Files
MageSail/Magesail/Controller/Adminhtml/Tunnel/Logtail.php
T
snxraven cc456a4cfa feat(magesail): multi-tunnel registry, per-tunnel stores, and admin UX
- Add TunnelRegistry (var/magesail_tunnels.json) and TunnelProcessPaths;
  migrate legacy magesail_tunnel_store.json and PID/key files
- Provision unique store codes per tunnel; website/group selection;
  stop vs remove; per-tunnel NGINX map, cron, status/logtail
- Fix log path: use DirectoryList::LOG (not LOG_DIR)
- Ignore missing store codes for MAGE_RUN_* (stale nginx / magesail_tunnel)
- Allow duplicate local ports; Admin port presets 443/80/8080 + custom;
  update docs and system.xml comments
2026-03-21 00:49:07 -05:00

63 lines
2.0 KiB
PHP

<?php
declare(strict_types=1);
namespace MageSail\Magesail\Controller\Adminhtml\Tunnel;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Controller\ResultFactory;
use MageSail\Magesail\Model\TunnelStatus;
class Logtail extends Action
{
private const BYTES = 8192;
private const LINES = 40;
public function __construct(
Context $context,
private readonly TunnelStatus $tunnelStatus
) {
parent::__construct($context);
}
public function execute()
{
$tunnelId = trim((string) $this->getRequest()->getParam('tunnel_id', ''));
if ($tunnelId === '') {
$json = $this->resultFactory->create(ResultFactory::TYPE_JSON);
$json->setData(['lines' => [], 'error' => 'missing_tunnel_id']);
return $json;
}
$path = $this->tunnelStatus->getLogFilePath($tunnelId);
$lines = [];
if (\is_readable($path)) {
$size = filesize($path);
if ($size !== false && $size > 0) {
$fp = fopen($path, 'rb');
if ($fp) {
$start = max(0, $size - self::BYTES);
fseek($fp, $start);
if ($start > 0) {
fgets($fp);
}
$chunk = stream_get_contents($fp) ?: '';
fclose($fp);
$lines = preg_split('/\R/', $chunk) ?: [];
$lines = array_values(array_filter($lines, static fn ($l) => $l !== ''));
if (\count($lines) > self::LINES) {
$lines = \array_slice($lines, -self::LINES);
}
}
}
}
$json = $this->resultFactory->create(ResultFactory::TYPE_JSON);
$json->setData(['lines' => $lines]);
return $json;
}
protected function _isAllowed()
{
return $this->_authorization->isAllowed('MageSail_Magesail::tunnel');
}
}