Skip to main content

Cancel a Workflow

View Markdown

Cancel an Activity from a Workflow​

Canceling an Activity from within a Workflow requires that the Activity Execution sends Heartbeats and sets a Heartbeat Timeout. If the Heartbeat is not invoked, the Activity cannot receive a cancellation request. When any non-immediate Activity is executed, the Activity Execution should send Heartbeats and set a Heartbeat Timeout to ensure that the server knows it is still working.

To cancel an Activity from a Workflow, start the Activity inside Workflow::async() and call cancel() on the returned cancellation scope. Set the cancellation type to ActivityCancellationType::WaitCancellationCompleted and wait on the Activity's promise to have the Workflow wait until the Activity finishes its cleanup. A canceled Activity rejects its promise with an ActivityFailure whose previous exception is a CanceledFailure.

php-sandbox/workflows/cancellation/CancelActivityWorkflow.php

$activity = Workflow::newActivityStub(
MyActivityInterface::class,
ActivityOptions::new()
->withStartToCloseTimeout('1 minute')
// The Activity must heartbeat within this timeout to receive cancellation.
->withHeartbeatTimeout('5 seconds')
// Wait for the Activity to finish its cleanup before the Workflow continues.
->withCancellationType(ActivityCancellationType::WaitCancellationCompleted),
);

// Start the Activity in its own cancellation scope.
$scope = Workflow::async(function () use ($activity, &$promise) {
$promise = $activity->processItems(60);
return yield $promise;
});

// Let the Activity run for a while, then cancel it.
yield Workflow::timer(3);
$scope->cancel();

try {
// Wait on the Activity itself, not the scope. The scope rejects as soon as
// it's canceled, but the Activity's promise waits for the Activity to
// finish its cleanup.
return yield $promise;
} catch (ActivityFailure $e) {
if (!$e->getPrevious() instanceof CanceledFailure) {
throw $e;
}
return 'Activity canceled';
}

When an Activity is canceled, Activity::heartbeat() throws an ActivityCanceledException the next time the Activity heartbeats. If cleanup logic needs to be performed, it can be done in a finally clause or inside a caught cancel error. However, for the Activity to appear canceled the exception needs to be re-thrown.

php-sandbox/workflows/cancellation/MyActivity.php

<?php
declare(strict_types=1);

namespace App\Workflows\Cancellation;

use Temporal\Activity;
use Temporal\Exception\Client\ActivityCanceledException;

class MyActivity implements MyActivityInterface
{
public function processItems(int $count): string
{
for ($i = 0; $i < $count; $i++) {
// Simulate one unit of work.
\sleep(1);

try {
// Heartbeat after each item. This is where the Activity learns that
// it has been canceled.
Activity::heartbeat($i);
} catch (ActivityCanceledException $e) {
// Clean up, then rethrow so that the Activity appears canceled.
\error_log("processItems canceled after {$i} items, cleaning up");
throw $e;
}
}

return "Processed {$count} items";
}
}
info

Unlike regular Activities, Local Activities can be canceled if they don't send Heartbeats. Local Activities are handled locally, and all the information needed to handle the cancellation logic is available in the same Worker process.

Reset a Workflow Execution​

Resetting a Workflow Execution terminates the current Workflow Execution and starts a new Workflow Execution from a point you specify in its Event History. Use reset when a Workflow is blocked due to a non-deterministic error or other issues that prevent it from completing.

When you reset a Workflow, the Event History up to the reset point is copied to the new Workflow Execution, and the Workflow resumes from that point with the current code. Reset only works if you've fixed the underlying issue, such as removing non-deterministic code. Any progress made after the reset point will be discarded. Provide a reason when resetting, as it will be recorded in the Event History.

  1. Navigate to the Workflow Execution details page,
  2. Click the Reset button in the top right dropdown menu,
  3. Select the Event ID to reset to,
  4. Provide a reason for the reset,
  5. Confirm the reset.

The Web UI shows available reset points and creates a link to the new Workflow Execution after the reset completes.