Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 2 additions & 11 deletions src/Phaseolies/Console/Commands/Cron/CronDaemonCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Phaseolies\Console\Commands\Cron;

use Phaseolies\Console\Schedule\Command;
use Phaseolies\Console\Schedule\SchedulePool;

class CronDaemonCommand extends Command
{
Expand Down Expand Up @@ -267,17 +268,7 @@ protected function isDaemonRunning(): bool
*/
protected function isProcessRunning(int $pid): bool
{
if ($pid <= 0) {
return false;
}

if (function_exists('posix_kill')) {
return posix_kill($pid, 0);
}

// Fallback
$output = shell_exec(sprintf("ps -p %d -o pid=", $pid));
return !empty(trim($output));
return SchedulePool::isProcessRunning($pid);
}

/**
Expand Down
39 changes: 31 additions & 8 deletions src/Phaseolies/Console/Commands/Cron/CronFinishCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,30 @@ public function handle(): int
$exitCode = (int)$this->argument('exit_code');

// Find and clean up the process
$pidFiles = glob(sys_get_temp_dir() . "/doppar_cron_lock_*.pid");
$directories = array_unique(array_filter([
$this->scheduleDirectory(),
sys_get_temp_dir(),
]));

foreach ($pidFiles as $pidFile) {
$processInfo = json_decode(file_get_contents($pidFile), true);
foreach ($directories as $directory) {
foreach (glob($directory . '/doppar_cron_lock_*.pid') ?: [] as $pidFile) {
$processInfo = json_decode((string) @file_get_contents($pidFile), true);

if (!is_array($processInfo) || ($processInfo['finish_id'] ?? null) !== $finishId) {
continue;
}

if ($processInfo['finish_id'] === $finishId) {
if ($shouldReleaseLock) {
$lockFile = str_replace('.pid', '', $pidFile);
$lockFile = substr($pidFile, 0, -strlen('.pid'));

if (file_exists($lockFile)) {
unlink($lockFile);
@unlink($lockFile);
}
}

unlink($pidFile);
break;
@unlink($pidFile);

break 2;
}
}

Expand All @@ -57,4 +66,18 @@ public function handle(): int
return $exitCode;
}
}

/**
* Get the directory that holds the scheduler's lock files
*
* @return string|null
*/
protected function scheduleDirectory(): ?string
{
try {
return storage_path('schedule');
} catch (\Throwable) {
return null;
}
}
}
Loading
Loading