In this episode, we're focusing on how to allow users to manually stop a running batch operation—specifically, how to cancel a server while it's being provisioned and clean up everything related to it.
First, we add a "Destroy Server" button that shows up when a server is in the provisioning state. Clicking this will trigger a destroyServer
method on our component. Behind the scenes, this method finds the batch job associated with the server (by its batch ID), cancels the batch, and then deletes the server itself. We make sure to handle the situation where there might not be a batch (like if provisioning already finished or failed).
Next, we see that canceling a batch doesn't actually stop running jobs automatically. To prevent canceled jobs from running, we first add an if
statement to every job, but that's a bit clunky. So instead, we implement middleware (like SkipIfBatchCancelled
) on our jobs, which elegantly skips job execution if the batch was canceled.
We also talk about using the batch's finally
callback to check if the batch was canceled (but not because of failures) and, in that case, to delete the server. This way, everything—servers and tasks—gets cleaned up from the database. We also redirect the user away from the server details page after cancellation, so they're not stuck on a page for something that no longer exists.
By the end, we've got a smooth workflow: canceling the batch stops further jobs, the server and related tasks are completely removed, and the user is taken back to a sensible place in the UI. Everything gets nicely tidied up, and the process feels robust and user-friendly.