Skip to content

Instantly share code, notes, and snippets.

@davidlenz
Last active February 25, 2022 17:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidlenz/f68442f7889ac5388eab375f98f058cb to your computer and use it in GitHub Desktop.
Save davidlenz/f68442f7889ac5388eab375f98f058cb to your computer and use it in GitHub Desktop.
cancel all jobs in a job queue
import time
import boto3
job_queue = <'job-queue-name'>
client=boto3.client("batch")
states = ['RUNNABLE','SUBMITTED','PENDING','STARTING','RUNNING']
for state in states:
print(state)
response = client.list_jobs(
jobQueue=job_queue,
jobStatus=state
)
jobs = response['jobSummaryList']
while len(jobs)>1:
for i,job in enumerate(jobs):
id_ = job["jobId"]
client.cancel_job(jobId = id_, reason="Unexpected run")
if i % 10==0:
print(i)
response = client.list_jobs(
jobQueue=job_queue,
jobStatus=state
)
jobs = response['jobSummaryList']
if len(jobs)>0:
time.sleep(5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment