-
Notifications
You must be signed in to change notification settings - Fork 12
Fix #292: align core Task/TimerTask public shape (result/isCompleted/isFaulted, cancellable TimerTask) #293
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
Some comments aren't visible on the classic Files Changed page.
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
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,86 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| import { CompletableTask } from "./completable-task"; | ||
|
|
||
| /** | ||
| * A durable timer task returned by `OrchestrationContext.createTimer`. | ||
| * | ||
| * In addition to the normal {@link Task} surface, a `TimerTask` can be | ||
| * canceled. Canceling is the classic "timeout vs. work" pattern: when a racing | ||
| * task wins (e.g. via `whenAny`), the losing timeout timer is canceled so the | ||
| * orchestration is no longer waiting on it. | ||
| * | ||
| * `TimerTask` is decoupled from the orchestration context's internal | ||
| * bookkeeping: the context injects a cancel handler via {@link setCancelHandler} | ||
| * and this class knows nothing about pending actions or tasks. This mirrors the | ||
| * `CancellableTask.set_cancel_handler` pattern in the Python SDK. | ||
| * | ||
| * @example Cancel the loser of a race | ||
| * ```typescript | ||
| * import { whenAny } from "@microsoft/durabletask-js"; | ||
| * | ||
| * const timeoutTask = context.createTimer(expirationDate); | ||
| * const workTask = context.callActivity("DoWork"); | ||
| * const winner = yield whenAny([timeoutTask, workTask]); | ||
| * if (winner === workTask && !timeoutTask.isCompleted) { | ||
| * timeoutTask.cancel(); | ||
| * } | ||
| * ``` | ||
| */ | ||
| export class TimerTask extends CompletableTask<undefined> { | ||
| private _cancelHandler?: () => void; | ||
| private _isCanceled = false; | ||
|
|
||
| /** | ||
| * Whether this timer has been canceled via {@link cancel}. | ||
| */ | ||
| get isCanceled(): boolean { | ||
| return this._isCanceled; | ||
| } | ||
|
|
||
| /** | ||
| * Registers the handler invoked when this timer is first canceled. | ||
| * | ||
| * The orchestration context supplies a closure that removes the timer's | ||
| * pending `CreateTimer` action and pending-task entry, so this class needs no | ||
| * knowledge of the context's internals. | ||
| * | ||
| * @internal Invoked by the orchestration context when the timer is created. | ||
| * Not part of the public `TimerTask` surface; orchestrator code must not call it. | ||
| * @param handler - Called once, when {@link cancel} first transitions the timer | ||
| * to the canceled state. | ||
| */ | ||
| setCancelHandler(handler: () => void): void { | ||
| this._cancelHandler = handler; | ||
| } | ||
|
|
||
| /** | ||
| * Cancels this timer so the orchestration stops waiting on it. | ||
| * | ||
| * The actual bookkeeping is performed by the cancel handler injected via | ||
| * {@link setCancelHandler}. The orchestration context's handler: | ||
| * - Removes the timer's `CreateTimer` action if it has not yet been dispatched | ||
| * to the sidecar (i.e. it is still pending in the current turn), so the timer | ||
| * is never scheduled at all. | ||
| * - Otherwise drops the timer from the pending-task set so the orchestrator no | ||
| * longer waits on it; the backend timer is reaped when the orchestration | ||
| * completes, and a late `TimerFired` event is ignored because no pending task | ||
| * remains for it. | ||
| * | ||
| * This is deterministic and replay-safe: it consumes no sequence number and | ||
| * only runs the injected handler. Cancel does NOT mark the task complete | ||
| * (`isCompleted` stays false). Calling `cancel()` after the timer has already | ||
| * fired (completed) or after it was already canceled is a no-op, so the handler | ||
| * runs at most once. | ||
| */ | ||
| cancel(): void { | ||
| if (this._isComplete || this._isCanceled) { | ||
| // Already fired or already canceled — nothing to do. | ||
| return; | ||
| } | ||
|
|
||
| this._isCanceled = true; | ||
| this._cancelHandler?.(); | ||
| } | ||
| } | ||
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.