cancel_tapis_job()#
cancel_tapis_job(tapis_client, job_uuid)
** Cancel a Tapis Job with Robust Error Handling**
Here’s an enhanced and more robust version of the tapipy job cancellation snippet, including:
Error handling,
Status confirmation,
Optional retry messaging for common user mistakes (e.g., invalid UUID, already finished jobs),
And a clean function you can reuse in your codebase or Jupyter workflows.
Example Usage#
from tapipy.tapis import Tapis
# Assume you already have an authenticated client
# t = Tapis(base_url='https://tacc.tapis.io', access_token='your_token_here')
job_id = 'abc123-your-job-uuid'
cancel_tapis_job(t, job_id)
Tip: When Jobs Can’t Be Cancelled#
Some jobs may already be in a terminal state (FINISHED, FAILED, CANCELLED) and cannot be stopped. You can pre-check status like this:
status = t.jobs.getJob(jobId=job_id).status
if status in ['FINISHED', 'FAILED', 'CANCELLED']:
print(f"Job is already in a terminal state: {status}")
else:
cancel_tapis_job(t, job_id)
Files#
You can find these files in Community Data.
cancel_tapis_job.py
def cancel_tapis_job(tapis_client, job_uuid):
"""
Attempts to cancel a Tapis job by UUID, with basic error handling and status confirmation.
Parameters:
- tapis_client: Authenticated tapipy.Tapis client
- job_uuid (str): UUID of the job to cancel
Returns:
- status (str): Final reported job status (e.g., CANCELLED, FINISHED, FAILED)
"""
try:
# Attempt to cancel the job
tapis_client.jobs.cancelJob(jobId=job_uuid)
print(f"Job {job_uuid} cancellation requested...")
# Confirm the updated job status
job_status = tapis_client.jobs.getJob(jobId=job_uuid).status
print(f"✅ Current job status: {job_status}")
return job_status
except Exception as e:
# Handle known issues
error_message = str(e)
if "Job not found" in error_message:
print(f"## Error ##: Job {job_uuid} not found. Check the UUID.")
elif "Invalid job state" in error_message:
print(f"** Warning **: Job {job_uuid} may already be completed or cancelled.")
else:
print(f"Unexpected error cancelling job {job_uuid}: {error_message}")
return None