Last active
February 25, 2022 17:41
-
-
Save davidlenz/f68442f7889ac5388eab375f98f058cb to your computer and use it in GitHub Desktop.
cancel all jobs in a job queue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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