Refactor bin scripts: shared CLI bootstrap, robust module migrate, and runtime locking
This commit is contained in:
@@ -114,3 +114,53 @@ function moduleCliBootstrapApp(): void
|
||||
|
||||
$booted = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs one module CLI step with shared bootstrap, failure handling and vendor warning summary.
|
||||
*
|
||||
* @param callable():int $runner
|
||||
*/
|
||||
function moduleCliRunStep(string $stepName, callable $runner): int
|
||||
{
|
||||
$warningsBefore = moduleCliVendorWarningsIgnoredTotal();
|
||||
moduleCliBootstrapApp();
|
||||
|
||||
$exitCode = 0;
|
||||
try {
|
||||
$exitCode = $runner();
|
||||
} catch (Throwable $exception) {
|
||||
fwrite(STDERR, sprintf("%s: FAILED: %s\n", $stepName, $exception->getMessage()));
|
||||
$exitCode = 1;
|
||||
}
|
||||
|
||||
$vendorWarningsIgnored = moduleCliVendorWarningsIgnoredSince($warningsBefore);
|
||||
fwrite(STDOUT, sprintf("%s: vendor_warnings_ignored=%d\n", $stepName, $vendorWarningsIgnored));
|
||||
|
||||
return $exitCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes a callable under a non-blocking file lock.
|
||||
*
|
||||
* @param callable():int $runner
|
||||
*/
|
||||
function moduleCliWithFileLock(string $lockFile, string $busyMessage, callable $runner): int
|
||||
{
|
||||
$lockDir = dirname($lockFile);
|
||||
if (!is_dir($lockDir) && !mkdir($lockDir, 0775, true) && !is_dir($lockDir)) {
|
||||
throw new RuntimeException("Failed to create lock directory: {$lockDir}");
|
||||
}
|
||||
|
||||
$lock = fopen($lockFile, 'c');
|
||||
if ($lock === false || !flock($lock, LOCK_EX | LOCK_NB)) {
|
||||
fwrite(STDERR, $busyMessage . PHP_EOL);
|
||||
return 0;
|
||||
}
|
||||
|
||||
try {
|
||||
return $runner();
|
||||
} finally {
|
||||
flock($lock, LOCK_UN);
|
||||
fclose($lock);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user