-
Notifications
You must be signed in to change notification settings - Fork 31
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
Update terminate logic to handle scheduled orchestrations #276
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -469,45 +469,70 @@ BEGIN | |
|
||
DECLARE @TaskHub varchar(50) = __SchemaNamePlaceholder__.CurrentTaskHub() | ||
|
||
DECLARE @existingStatus varchar(30) = ( | ||
SELECT TOP 1 existing.[RuntimeStatus] | ||
FROM Instances existing WITH (HOLDLOCK) | ||
WHERE [TaskHub] = @TaskHub AND [InstanceID] = @InstanceID | ||
) | ||
DECLARE @existingStatus varchar(30) | ||
DECLARE @existingLockExpiration datetime2(7) | ||
|
||
-- Get the status of an existing orchestration | ||
SELECT TOP 1 | ||
@existingStatus = existing.[RuntimeStatus], | ||
@existingLockExpiration = existing.[LockExpiration] | ||
FROM Instances existing WITH (HOLDLOCK) | ||
WHERE [TaskHub] = @TaskHub AND [InstanceID] = @InstanceID | ||
|
||
IF @existingStatus IS NULL | ||
BEGIN | ||
ROLLBACK TRANSACTION; | ||
THROW 50000, 'The instance does not exist.', 1; | ||
END | ||
-- If the instance is already completed, no need to terminate it. | ||
IF @existingStatus IN ('Pending', 'Running') | ||
|
||
DECLARE @now datetime2(7) = SYSUTCDATETIME() | ||
|
||
IF @existingStatus IN ('Running', 'Pending') | ||
BEGIN | ||
IF NOT EXISTS ( | ||
SELECT TOP (1) 1 FROM NewEvents | ||
WHERE [TaskHub] = @TaskHub AND [InstanceID] = @InstanceID AND [EventType] = 'ExecutionTerminated' | ||
) | ||
-- Create a payload to store the reason, if any | ||
DECLARE @PayloadID uniqueidentifier = NULL | ||
IF @Reason IS NOT NULL | ||
BEGIN | ||
-- Payloads are stored separately from the events | ||
DECLARE @PayloadID uniqueidentifier = NULL | ||
IF @Reason IS NOT NULL | ||
-- Note that we don't use the Reason column for the Reason with terminate events | ||
SET @PayloadID = NEWID() | ||
INSERT INTO Payloads ([TaskHub], [InstanceID], [PayloadID], [Text]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The comment in line#468 states that the table order for this sproc should be Instances --> (NewEvents --> Payloads --> NewEvents) but this PR changes the table order to Instances --> (Payloads --> Instances --> NewEvents). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm...good observation. Let me take a closer look at this. |
||
VALUES (@TaskHub, @InstanceID, @PayloadID, @Reason) | ||
END | ||
|
||
-- Check the status of the orchestration to determine which termination path to take | ||
IF @existingStatus = 'Pending' AND (@existingLockExpiration IS NULL OR @existingLockExpiration <= @now) | ||
BEGIN | ||
-- The orchestration hasn't started yet - transition it directly to the Terminated state and delete | ||
-- any pending messages | ||
UPDATE Instances SET | ||
[RuntimeStatus] = 'Terminated', | ||
[LastUpdatedTime] = @now, | ||
[CompletedTime] = @now, | ||
[OutputPayloadID] = @PayloadID, | ||
[LockExpiration] = NULL -- release the lock, if any | ||
WHERE [TaskHub] = @TaskHub AND [InstanceID] = @InstanceID | ||
|
||
DELETE FROM NewEvents WHERE [TaskHub] = @TaskHub AND [InstanceID] = @InstanceID | ||
andystaples marked this conversation as resolved.
Show resolved
Hide resolved
|
||
END | ||
ELSE | ||
BEGIN | ||
-- The orchestration has actually started running in this case | ||
IF NOT EXISTS ( | ||
SELECT TOP (1) 1 FROM NewEvents | ||
WHERE [TaskHub] = @TaskHub AND [InstanceID] = @InstanceID AND [EventType] = 'ExecutionTerminated' | ||
) | ||
BEGIN | ||
-- Note that we don't use the Reason column for the Reason with terminate events | ||
SET @PayloadID = NEWID() | ||
INSERT INTO Payloads ([TaskHub], [InstanceID], [PayloadID], [Text]) | ||
VALUES (@TaskHub, @InstanceID, @PayloadID, @Reason) | ||
INSERT INTO NewEvents ( | ||
[TaskHub], | ||
[InstanceID], | ||
[EventType], | ||
[PayloadID] | ||
) VALUES ( | ||
@TaskHub, | ||
@InstanceID, | ||
'ExecutionTerminated', | ||
@PayloadID) | ||
END | ||
|
||
INSERT INTO NewEvents ( | ||
[TaskHub], | ||
[InstanceID], | ||
[EventType], | ||
[PayloadID] | ||
) VALUES ( | ||
@TaskHub, | ||
@InstanceID, | ||
'ExecutionTerminated', | ||
@PayloadID) | ||
END | ||
END | ||
|
||
|
@@ -1444,7 +1469,7 @@ BEGIN | |
-- Instance IDs can be overwritten only if the orchestration is in a terminal state | ||
IF @existingStatus NOT IN ('Failed') | ||
BEGIN | ||
DECLARE @msg nvarchar(4000) = FORMATMESSAGE('Cannot rewing instance with ID ''%s'' because it is not in a ''Failed'' state, but in ''%s'' state.', @InstanceID, @existingStatus); | ||
DECLARE @msg nvarchar(4000) = FORMATMESSAGE('Cannot rewind instance with ID ''%s'' because it is not in a ''Failed'' state, but in ''%s'' state.', @InstanceID, @existingStatus); | ||
THROW 50001, @msg, 1; | ||
END | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an unrelated fix that I happened to notice independently. I'm just adding it to this PR as a convenience since it's a small change.