Adb netzgebiet/add manual rimo import

This commit is contained in:
Luca Haid
2026-01-08 09:38:01 +00:00
parent 36c398126e
commit 6558caf2f3
3 changed files with 329 additions and 3 deletions
@@ -24,6 +24,9 @@ class ADBNetzgebietController extends mfBaseController {
"GET_URL" => $this::getUrl("ADBNetzgebiet/getNetzgebiete"),
"SAVE_URL" => $this::getUrl("ADBNetzgebiet/save"),
"HISTORY_URL" => $this::getUrl("ADBNetzgebiet/getHistory"),
"START_RIMO_IMPORT_URL" => $this::getUrl("ADBNetzgebiet/startRimoImport"),
"GET_RIMO_IMPORT_STATUS_URL" => $this::getUrl("ADBNetzgebiet/getRimoImportStatus"),
"GET_RIMO_IMPORT_LOG_URL" => $this::getUrl("ADBNetzgebiet/getRimoImportLog"),
"NETWORK_URL" => $this::getUrl("Network/Index"),
"NETWORK_CREATE_URL" => $this::getUrl("Network/add"),
"CAMPAIGN_URL" => $this::getUrl("Preordercampaign/edit"),
@@ -130,6 +133,193 @@ class ADBNetzgebietController extends mfBaseController {
self::returnJson(['success' => true, 'data' => $history]);
}
protected function startRimoImportAction(): void {
$id = $_GET['id'] ?? null;
if (empty($id)) {
self::returnJson(['success' => false, 'message' => "Netzgebiet ID required."]);
return;
}
$netzgebiet = ADBNetzgebiet::get($id);
if (!$netzgebiet || !$netzgebiet->id) {
self::returnJson(['success' => false, 'message' => "Netzgebiet not found."]);
return;
}
if (strpos($netzgebiet->source, 'rimo-') !== 0) {
self::returnJson(['success' => false, 'message' => "This action is only for RIMO-source Netzgebiete."]);
return;
}
if (empty($netzgebiet->source_id)) {
self::returnJson(['success' => false, 'message' => "Netzgebiet has no Source ID."]);
return;
}
$safeSourceId = preg_replace('/[^a-zA-Z0-9_-]/', '_', $netzgebiet->source_id);
$importTempDir = TEMP_DIR . "/ADBNetzgebietRimoImport/";
$logDir = $importTempDir . $safeSourceId;
$logFile = $logDir . "/import.log";
$lockFile = $logDir . "/import.lock";
if (is_dir($importTempDir)) {
foreach (glob($importTempDir . "*") as $dir) {
if (is_dir($dir) && (time() - filemtime($dir)) > 86400) {
// simple cleanup
if (file_exists($dir . "/import.log")) @unlink($dir . "/import.log");
if (file_exists($dir . "/import.lock")) @unlink($dir . "/import.lock");
@rmdir($dir);
}
}
}
if (!is_dir($logDir)) {
mkdir($logDir, 0755, true);
}
if (file_exists($lockFile)) {
if ((time() - filemtime($lockFile)) > 3600) { // stale lock for 1h
@unlink($lockFile);
} else {
self::returnJson(['success' => false, 'message' => "Import is already running.", 'status' => 'running']);
return;
}
}
if (file_exists($logFile) && (time() - filemtime($logFile)) < 900) {
$remaining = 900 - (time() - filemtime($logFile));
self::returnJson(['success' => false, 'message' => "Please wait before starting another import.", 'status' => 'cooldown', 'remaining' => $remaining]);
return;
}
touch($lockFile);
$projectRoot = dirname(dirname(__DIR__));
$scriptRelativePath = 'scripts/adb-rimo-import/rimo-import.php';
$scriptFullPath = $projectRoot . '/' . $scriptRelativePath;
if (!file_exists($scriptFullPath)) {
self::returnJson(['success' => false, 'message' => "Import script not found."]);
return;
}
$php_executable = "php";
$command = "$php_executable $scriptRelativePath " . escapeshellarg($netzgebiet->source_id);
$bgCommand = 'cd ' . escapeshellarg($projectRoot) . ' && ' . $command . ' > ' . escapeshellarg($logFile) . ' 2>&1 & echo $!';
$pid = shell_exec($bgCommand);
if(empty($pid) || !is_numeric(trim($pid))) {
self::returnJson(['success' => false, 'message' => "Failed to start background process."]);
return;
}
file_put_contents($lockFile, trim($pid));
self::returnJson(['success' => true, 'message' => 'RIMO import started.']);
}
protected function getRimoImportStatusAction(): void {
$ids = $this->postData['ids'] ?? [];
if (empty($ids)) {
self::returnJson(['success' => true, 'data' => []]);
return;
}
$statuses = [];
foreach ($ids as $id) {
$netzgebiet = ADBNetzgebiet::get($id);
if (!$netzgebiet || !$netzgebiet->id || strpos($netzgebiet->source, 'rimo-') !== 0 || empty($netzgebiet->source_id)) {
$statuses[$id] = ['status' => 'not_applicable'];
continue;
}
$safeSourceId = preg_replace('/[^a-zA-Z0-9_-]/', '_', $netzgebiet->source_id);
$logDir = TEMP_DIR . "/ADBNetzgebietRimoImport/" . $safeSourceId;
$logFile = $logDir . "/import.log";
$lockFile = $logDir . "/import.lock";
if (file_exists($lockFile)) {
$pid = trim(file_get_contents($lockFile));
// Check if process is still running. posix_getpgid returns false if process does not exist.
if (is_numeric($pid) && posix_getpgid((int)$pid) !== false) {
$statuses[$id] = ['status' => 'running'];
} else {
// Stale lock file, process is gone.
@unlink($lockFile);
// Check for cooldown based on log file from the finished process
if (file_exists($logFile) && (time() - filemtime($logFile)) < 900) {
$statuses[$id] = [
'status' => 'cooldown',
'remaining' => 900 - (time() - filemtime($logFile))
];
} else {
$statuses[$id] = ['status' => 'idle'];
}
}
} elseif (file_exists($logFile) && (time() - filemtime($logFile)) < 900) {
$statuses[$id] = [
'status' => 'cooldown',
'remaining' => 900 - (time() - filemtime($logFile))
];
} else {
$statuses[$id] = ['status' => 'idle'];
}
}
self::returnJson(['success' => true, 'data' => $statuses]);
}
protected function getRimoImportLogAction(): void {
$id = $_GET['id'] ?? null;
if (empty($id)) {
self::returnJson(['success' => false, 'message' => "Netzgebiet ID required."]);
return;
}
$netzgebiet = ADBNetzgebiet::get($id);
if (!$netzgebiet || !$netzgebiet->id || empty($netzgebiet->source_id)) {
self::returnJson(['success' => false, 'message' => "Netzgebiet not found or not applicable."]);
return;
}
$safeSourceId = preg_replace('/[^a-zA-Z0-9_-]/', '_', $netzgebiet->source_id);
$logDir = TEMP_DIR . "/ADBNetzgebietRimoImport/" . $safeSourceId;
$logFile = $logDir . "/import.log";
$lockFile = $logDir . "/import.lock";
$logContent = "";
if (file_exists($logFile)) {
$logContent = file_get_contents($logFile);
}
$status = 'idle';
if (file_exists($lockFile)) {
$pid = trim(file_get_contents($lockFile));
if (is_numeric($pid) && posix_getpgid((int)$pid) !== false) {
$status = 'running';
} else {
@unlink($lockFile); // Stale lock, process is gone
}
}
if ($status !== 'running') {
if (file_exists($logFile) && (time() - filemtime($logFile)) < 900) {
$status = 'cooldown';
} else {
$status = file_exists($logFile) ? 'finished' : 'idle';
}
}
self::returnJson([
'success' => true,
'data' => [
'log' => $logContent,
'status' => $status,
'timestamp' => file_exists($logFile) ? filemtime($logFile) : null
]
]);
}
// TODO: Implement RIMO API check
protected function checkRimoSourceIdAction(): void {
self::returnJson(['success' => false, 'message' => "RIMO API check not available."]);