mirror of
https://codeberg.org/PostERG/xamxam.git
synced 2026-06-25 08:09:18 +02:00
Add periodic cleanup of orphaned drafts: cleanup job, just command, deploy cron
This commit is contained in:
55
scripts/cleanup-drafts.php
Executable file
55
scripts/cleanup-drafts.php
Executable file
@@ -0,0 +1,55 @@
|
||||
#!/usr/bin/env php
|
||||
<?php
|
||||
/**
|
||||
* cleanup-drafts.php — Delete orphaned draft theses older than 24h.
|
||||
*
|
||||
* Draft theses are created with status='draft' during the two-phase commit
|
||||
* in ThesisCreateController. If the file phase throws after COMMIT, the
|
||||
* draft remains orphaned — no files attached, but blocks the identifier.
|
||||
*
|
||||
* Usage:
|
||||
* php scripts/cleanup-drafts.php # dry-run (list candidates)
|
||||
* php scripts/cleanup-drafts.php --no-dry-run # actually delete
|
||||
*
|
||||
* Exit codes: 0 on success, 1 on error.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
$root = dirname(__DIR__);
|
||||
define('APP_ROOT', $root . '/app');
|
||||
|
||||
require_once APP_ROOT . '/src/Database.php';
|
||||
|
||||
$dryRun = !in_array('--no-dry-run', $argv, true);
|
||||
|
||||
try {
|
||||
$db = new Database();
|
||||
$result = $db->cleanupOrphanedDrafts(24, $dryRun);
|
||||
} catch (Exception $e) {
|
||||
error_log('[cleanup-drafts] Error: ' . $e->getMessage());
|
||||
exit(1);
|
||||
}
|
||||
|
||||
$count = count($result['candidates']);
|
||||
|
||||
if ($count === 0) {
|
||||
exit(0); // nothing to do — quiet exit
|
||||
}
|
||||
|
||||
if ($dryRun) {
|
||||
foreach ($result['candidates'] as $row) {
|
||||
printf(
|
||||
"DRY-RUN → #%d %s \"%s\" (submitted %s)\n",
|
||||
$row['id'],
|
||||
$row['identifier'],
|
||||
$row['title'],
|
||||
$row['submitted_at']
|
||||
);
|
||||
}
|
||||
echo "Found {$count} orphaned draft(s). Re-run with --no-dry-run to delete.\n";
|
||||
exit(0);
|
||||
}
|
||||
|
||||
printf("Deleted %d orphaned draft(s).\n", $result['deleted']);
|
||||
exit(0);
|
||||
Reference in New Issue
Block a user