Open dhiaayachi opened 1 month ago
Thank you for your feature request.
Temporal currently does not have a built-in way to automatically clean up resources when a workflow fails or is canceled. However, you can implement this logic in your workflow using a combination of activity and signals.
Here's how:
You can also use the Workflow.Cancel
or Workflow.Terminate
API in the Temporal SDK to cancel or terminate a running workflow execution and invoke cleanup activities. Refer to Cancellation and Interrupt a Workflow for more details.
Thank you for the feature request!
Temporal doesn't currently have automatic cleanup of resources in the case of a workflow failure or cancellation. However, you can achieve this by using a finally
block in your workflow or activities, which always executes regardless of whether the step succeeded or failed, and then invoke a cleanup activity to free resources.
Example of the finally
block:
from temporalio import workflow, activity
@workflow.defn
class MyWorkflow:
@workflow.run
async def run(self) -> None:
try:
vm_id = await self.create_vm()
await self.wait_vm_running(vm_id)
public_ip = await self.create_public_ip()
await self.wait_public_ip_available(public_ip)
await self.bind_public_ip(vm_id, public_ip)
except Exception:
await self.cleanup(vm_id, public_ip)
raise
finally:
await self.cleanup(vm_id, public_ip)
@activity.defn
async def create_vm():
# Implement vm creation logic
# ...
@activity.defn
async def wait_vm_running(vm_id):
# Implement vm status wait logic
# ...
@activity.defn
async def create_public_ip():
# Implement public ip creation logic
# ...
@activity.defn
async def wait_public_ip_available(public_ip):
# Implement public ip status wait logic
# ...
@activity.defn
async def bind_public_ip(vm_id, public_ip):
# Implement binding logic
# ...
@activity.defn
async def cleanup(vm_id, public_ip):
# Implement cleanup logic
# ...
In this example, if any step fails, the cleanup
activity is called to free the created resources. This activity is also called in the finally
block, which ensures the cleanup happens even if the workflow is canceled. You can extend this pattern to handle other cleanup steps within the workflow.
Thank you for reaching out!
This is a great feature request. Temporal does not currently have the ability to automatically clean up resources when a workflow fails or is canceled. However, you can use a workaround to achieve similar functionality.
Workaround:
finally
block or a catch
block for any exception that indicates a failure. You can also use a Signal
to trigger cleanup.Example:
// Workflow Definition
async function createAndBindResource() {
try {
await createVm();
await waitForVmRunning();
await createPublicIp();
await waitForPublicIpAvailable();
await bindPublicIpToVm();
// Other steps
} catch (error) {
// Call cleanup activity on failure
await cleanupResources();
} finally {
// Call cleanup activity regardless of success or failure
await cleanupResources();
}
}
// Cleanup activity
async function cleanupResources() {
await deleteVm();
await deletePublicIp();
// Other cleanup steps
}
Let me know if you have any other questions.
in my case, i want to use temporal to create some resources on a public cloud. like creating a cloud vm with public ip.
i want