-
-
Notifications
You must be signed in to change notification settings - Fork 64
Watchdog #371
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Watchdog #371
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
692631f
Initial plan
Copilot ec20c67
Implement watchdog-based recovery for stranded workflows
Copilot 404857a
Fix watchdog recovery: prevent duplicates, fix namespace collision, r…
Copilot 813747f
Harden watchdog recovery bootstrap
durable-workflow-ops 7d75503
Cover watchdog loop early exits
durable-workflow-ops ce07663
Cover remaining watchdog branches
durable-workflow-ops 567f217
Harden watchdog recovery loop
durable-workflow-ops a661f6e
Simplify watchdog wake-up flow
durable-workflow-ops 41d262e
Simplify watchdog defaults and test flow
durable-workflow-ops 0ecb8d1
Cover watchdog wake race path
durable-workflow-ops aa62d1d
Harden watchdog wake flow
durable-workflow-ops d9e6a48
Scope callback overlap locks
durable-workflow-ops 82772e1
Keep callback jobs activity-scoped
durable-workflow-ops 844da8e
Move watchdog pending check under lock
durable-workflow-ops d590723
Clear watchdog wake throttle on dispatch failure
durable-workflow-ops 0b2f526
Address remaining watchdog review threads
durable-workflow-ops 053b1a3
Fix watchdog unique lock test key
durable-workflow-ops db4f639
Avoid newQuery override in watchdog test
durable-workflow-ops d935162
Harden watchdog after-commit wake path
durable-workflow-ops File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Workflow; | ||
|
|
||
| use Illuminate\Bus\Queueable; | ||
| use Illuminate\Bus\UniqueLock; | ||
| use Illuminate\Contracts\Bus\Dispatcher; | ||
| use Illuminate\Contracts\Queue\ShouldBeEncrypted; | ||
| use Illuminate\Contracts\Queue\ShouldQueue; | ||
| use Illuminate\Queue\InteractsWithQueue; | ||
| use Illuminate\Support\Carbon; | ||
| use Illuminate\Support\Facades\Cache; | ||
| use Illuminate\Support\Facades\DB; | ||
| use Workflow\Models\StoredWorkflow; | ||
| use Workflow\States\WorkflowPendingStatus; | ||
|
|
||
| class Watchdog implements ShouldBeEncrypted, ShouldQueue | ||
| { | ||
| use InteractsWithQueue; | ||
| use Queueable; | ||
|
|
||
| public const DEFAULT_TIMEOUT = 300; | ||
|
|
||
| private const CACHE_KEY = 'workflow:watchdog'; | ||
|
|
||
| private const LOOP_THROTTLE_KEY = 'workflow:watchdog:looping'; | ||
|
|
||
| private const RECOVERY_LOCK_PREFIX = 'workflow:watchdog:recovering:'; | ||
|
|
||
| public int $tries = 0; | ||
|
|
||
| public int $maxExceptions = 0; | ||
|
|
||
| public $timeout = 0; | ||
|
|
||
| public static function wake(string $connection, ?string $queue = null): void | ||
| { | ||
| $timeout = self::timeout(); | ||
|
|
||
| $queue = self::normalizeQueue($queue); | ||
|
|
||
| DB::afterCommit(static function () use ($connection, $queue, $timeout): void { | ||
| if (Cache::has(self::CACHE_KEY)) { | ||
| return; | ||
| } | ||
|
|
||
| if (! Cache::add(self::LOOP_THROTTLE_KEY, true, 60)) { | ||
| return; | ||
| } | ||
|
|
||
| if (! self::hasRecoverablePendingWorkflows($timeout)) { | ||
| return; | ||
| } | ||
|
|
||
| if (! Cache::add(self::CACHE_KEY, true, $timeout)) { | ||
| return; | ||
| } | ||
|
|
||
| $watchdog = (new self()) | ||
| ->onConnection($connection); | ||
|
|
||
| if ($queue !== null) { | ||
| $watchdog->onQueue($queue); | ||
| } | ||
|
|
||
| try { | ||
| app(Dispatcher::class)->dispatch($watchdog); | ||
| } catch (\Throwable $exception) { | ||
| Cache::forget(self::CACHE_KEY); | ||
| Cache::forget(self::LOOP_THROTTLE_KEY); | ||
|
|
||
| throw $exception; | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| public function handle(): void | ||
| { | ||
| $timeout = self::timeout(); | ||
|
|
||
| Cache::put(self::CACHE_KEY, true, $timeout); | ||
|
|
||
| $model = config('workflows.stored_workflow_model', StoredWorkflow::class); | ||
|
|
||
| $model::where('status', WorkflowPendingStatus::$name) | ||
| ->where('updated_at', '<=', Carbon::now()->subSeconds($timeout)) | ||
| ->whereNotNull('arguments') | ||
| ->each(static function (StoredWorkflow $storedWorkflow) use ($timeout): void { | ||
| self::recover($storedWorkflow, $timeout); | ||
| }); | ||
|
|
||
rmcdaniel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if ($this->job !== null) { | ||
| $this->release($timeout); | ||
| } | ||
| } | ||
|
|
||
| private static function recover(StoredWorkflow $storedWorkflow, int $timeout): bool | ||
| { | ||
| $claimTtl = self::bootstrapWindow($timeout); | ||
|
|
||
| return (bool) (Cache::lock(self::RECOVERY_LOCK_PREFIX . $storedWorkflow->id, $claimTtl) | ||
| ->get(static function () use ($storedWorkflow): bool { | ||
| $storedWorkflow->refresh(); | ||
|
|
||
| if ($storedWorkflow->status::class !== WorkflowPendingStatus::class) { | ||
| return false; | ||
| } | ||
|
|
||
| $workflowStub = $storedWorkflow->toWorkflow(); | ||
| $workflowClass = $storedWorkflow->class; | ||
| $workflowJob = new $workflowClass($storedWorkflow, ...$storedWorkflow->workflowArguments()); | ||
|
|
||
| $storedWorkflow->touch(); | ||
|
|
||
| (new UniqueLock(Cache::driver()))->release($workflowJob); | ||
rmcdaniel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| $workflowStub->resume(); | ||
|
|
||
| return true; | ||
| }) ?? false); | ||
| } | ||
|
|
||
| private static function timeout(): int | ||
| { | ||
| return self::DEFAULT_TIMEOUT; | ||
| } | ||
|
|
||
| private static function hasRecoverablePendingWorkflows(int $timeout): bool | ||
| { | ||
| $model = config('workflows.stored_workflow_model', StoredWorkflow::class); | ||
|
|
||
| return $model::where('status', WorkflowPendingStatus::$name) | ||
| ->where('updated_at', '<=', Carbon::now()->subSeconds($timeout)) | ||
| ->whereNotNull('arguments') | ||
| ->exists(); | ||
| } | ||
|
|
||
| private static function bootstrapWindow(int $timeout): int | ||
| { | ||
| return max(1, min($timeout, 60)); | ||
| } | ||
|
|
||
| private static function normalizeQueue(?string $queue): ?string | ||
| { | ||
| if ($queue === null) { | ||
| return null; | ||
| } | ||
|
|
||
| foreach (explode(',', $queue) as $candidate) { | ||
| $candidate = trim($candidate); | ||
|
|
||
| if ($candidate !== '') { | ||
| return $candidate; | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.