|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Workflow; |
| 6 | + |
| 7 | +use Illuminate\Bus\Queueable; |
| 8 | +use Illuminate\Bus\UniqueLock; |
| 9 | +use Illuminate\Contracts\Bus\Dispatcher; |
| 10 | +use Illuminate\Contracts\Queue\ShouldBeEncrypted; |
| 11 | +use Illuminate\Contracts\Queue\ShouldQueue; |
| 12 | +use Illuminate\Queue\InteractsWithQueue; |
| 13 | +use Illuminate\Support\Carbon; |
| 14 | +use Illuminate\Support\Facades\Cache; |
| 15 | +use Illuminate\Support\Facades\DB; |
| 16 | +use Workflow\Models\StoredWorkflow; |
| 17 | +use Workflow\States\WorkflowPendingStatus; |
| 18 | + |
| 19 | +class Watchdog implements ShouldBeEncrypted, ShouldQueue |
| 20 | +{ |
| 21 | + use InteractsWithQueue; |
| 22 | + use Queueable; |
| 23 | + |
| 24 | + public const DEFAULT_TIMEOUT = 300; |
| 25 | + |
| 26 | + private const CACHE_KEY = 'workflow:watchdog'; |
| 27 | + |
| 28 | + private const LOOP_THROTTLE_KEY = 'workflow:watchdog:looping'; |
| 29 | + |
| 30 | + private const RECOVERY_LOCK_PREFIX = 'workflow:watchdog:recovering:'; |
| 31 | + |
| 32 | + public int $tries = 0; |
| 33 | + |
| 34 | + public int $maxExceptions = 0; |
| 35 | + |
| 36 | + public $timeout = 0; |
| 37 | + |
| 38 | + public static function wake(string $connection, ?string $queue = null): void |
| 39 | + { |
| 40 | + $timeout = self::timeout(); |
| 41 | + |
| 42 | + $queue = self::normalizeQueue($queue); |
| 43 | + |
| 44 | + DB::afterCommit(static function () use ($connection, $queue, $timeout): void { |
| 45 | + if (Cache::has(self::CACHE_KEY)) { |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + if (! Cache::add(self::LOOP_THROTTLE_KEY, true, 60)) { |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + if (! self::hasRecoverablePendingWorkflows($timeout)) { |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + if (! Cache::add(self::CACHE_KEY, true, $timeout)) { |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + $watchdog = (new self()) |
| 62 | + ->onConnection($connection); |
| 63 | + |
| 64 | + if ($queue !== null) { |
| 65 | + $watchdog->onQueue($queue); |
| 66 | + } |
| 67 | + |
| 68 | + try { |
| 69 | + app(Dispatcher::class)->dispatch($watchdog); |
| 70 | + } catch (\Throwable $exception) { |
| 71 | + Cache::forget(self::CACHE_KEY); |
| 72 | + Cache::forget(self::LOOP_THROTTLE_KEY); |
| 73 | + |
| 74 | + throw $exception; |
| 75 | + } |
| 76 | + }); |
| 77 | + } |
| 78 | + |
| 79 | + public function handle(): void |
| 80 | + { |
| 81 | + $timeout = self::timeout(); |
| 82 | + |
| 83 | + Cache::put(self::CACHE_KEY, true, $timeout); |
| 84 | + |
| 85 | + $model = config('workflows.stored_workflow_model', StoredWorkflow::class); |
| 86 | + |
| 87 | + $model::where('status', WorkflowPendingStatus::$name) |
| 88 | + ->where('updated_at', '<=', Carbon::now()->subSeconds($timeout)) |
| 89 | + ->whereNotNull('arguments') |
| 90 | + ->each(static function (StoredWorkflow $storedWorkflow) use ($timeout): void { |
| 91 | + self::recover($storedWorkflow, $timeout); |
| 92 | + }); |
| 93 | + |
| 94 | + if ($this->job !== null) { |
| 95 | + $this->release($timeout); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + private static function recover(StoredWorkflow $storedWorkflow, int $timeout): bool |
| 100 | + { |
| 101 | + $claimTtl = self::bootstrapWindow($timeout); |
| 102 | + |
| 103 | + return (bool) (Cache::lock(self::RECOVERY_LOCK_PREFIX . $storedWorkflow->id, $claimTtl) |
| 104 | + ->get(static function () use ($storedWorkflow): bool { |
| 105 | + $storedWorkflow->refresh(); |
| 106 | + |
| 107 | + if ($storedWorkflow->status::class !== WorkflowPendingStatus::class) { |
| 108 | + return false; |
| 109 | + } |
| 110 | + |
| 111 | + $workflowStub = $storedWorkflow->toWorkflow(); |
| 112 | + $workflowClass = $storedWorkflow->class; |
| 113 | + $workflowJob = new $workflowClass($storedWorkflow, ...$storedWorkflow->workflowArguments()); |
| 114 | + |
| 115 | + $storedWorkflow->touch(); |
| 116 | + |
| 117 | + (new UniqueLock(Cache::driver()))->release($workflowJob); |
| 118 | + |
| 119 | + $workflowStub->resume(); |
| 120 | + |
| 121 | + return true; |
| 122 | + }) ?? false); |
| 123 | + } |
| 124 | + |
| 125 | + private static function timeout(): int |
| 126 | + { |
| 127 | + return self::DEFAULT_TIMEOUT; |
| 128 | + } |
| 129 | + |
| 130 | + private static function hasRecoverablePendingWorkflows(int $timeout): bool |
| 131 | + { |
| 132 | + $model = config('workflows.stored_workflow_model', StoredWorkflow::class); |
| 133 | + |
| 134 | + return $model::where('status', WorkflowPendingStatus::$name) |
| 135 | + ->where('updated_at', '<=', Carbon::now()->subSeconds($timeout)) |
| 136 | + ->whereNotNull('arguments') |
| 137 | + ->exists(); |
| 138 | + } |
| 139 | + |
| 140 | + private static function bootstrapWindow(int $timeout): int |
| 141 | + { |
| 142 | + return max(1, min($timeout, 60)); |
| 143 | + } |
| 144 | + |
| 145 | + private static function normalizeQueue(?string $queue): ?string |
| 146 | + { |
| 147 | + if ($queue === null) { |
| 148 | + return null; |
| 149 | + } |
| 150 | + |
| 151 | + foreach (explode(',', $queue) as $candidate) { |
| 152 | + $candidate = trim($candidate); |
| 153 | + |
| 154 | + if ($candidate !== '') { |
| 155 | + return $candidate; |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + return null; |
| 160 | + } |
| 161 | +} |
0 commit comments