|
| 1 | +_incomplete_job(job) = !(job["status"] in ["SUCCEEDED", "FAILED"]) |
| 2 | +_suffix_asterisk(prefix) = endswith(prefix, "*") ? prefix : string(prefix, "*") |
| 3 | + |
| 4 | +function _get_job_ids(job_queue, prefix) |
| 5 | + # Check if the prefix provided ends w/ '*", if not append it |
| 6 | + prefix = _suffix_asterisk(prefix) |
| 7 | + |
| 8 | + resp = @mock Batch.list_jobs( |
| 9 | + Dict( |
| 10 | + "jobQueue" => job_queue, |
| 11 | + "filters" => [Dict("name" => "JOB_NAME", "values" => [prefix])], |
| 12 | + ), |
| 13 | + ) |
| 14 | + jobs = resp["jobSummaryList"] |
| 15 | + |
| 16 | + while haskey(resp, "nextToken") |
| 17 | + resp = @mock Batch.list_jobs( |
| 18 | + Dict("jobQueue" => job_queue, "nextToken" => resp["nextToken"]) |
| 19 | + ) |
| 20 | + append!(jobs, resp["jobSummaryList"]) |
| 21 | + end |
| 22 | + |
| 23 | + filter!(j -> _incomplete_job(j), jobs) |
| 24 | + |
| 25 | + return [j["jobId"] for j in jobs] |
| 26 | +end |
| 27 | + |
| 28 | +""" |
| 29 | + terminate_jobs() |
| 30 | +
|
| 31 | +Terminate all Batch jobs with a given prefix. |
| 32 | +
|
| 33 | +# Arguments |
| 34 | +- `job_queue::JobQueue`: JobQueue where the jobs reside |
| 35 | +- `prefix::AbstractString`: Prefix for the jobs |
| 36 | +
|
| 37 | +# Keywords |
| 38 | +- `reason::AbstractString=""`: Reason to terminate the jobs |
| 39 | +
|
| 40 | +# Return |
| 41 | +- `Array{String}`: Terminated Job Ids |
| 42 | +""" |
| 43 | +function terminate_jobs( |
| 44 | + job_queue::AbstractString, prefix::AbstractString; reason::AbstractString="" |
| 45 | +) |
| 46 | + job_ids = _get_job_ids(job_queue, prefix) |
| 47 | + |
| 48 | + for job_id in job_ids |
| 49 | + @mock Batch.terminate_job(job_id, reason) |
| 50 | + end |
| 51 | + |
| 52 | + return job_ids |
| 53 | +end |
| 54 | + |
| 55 | +function terminate_jobs( |
| 56 | + job_queue::JobQueue, prefix::AbstractString; reason::AbstractString="" |
| 57 | +) |
| 58 | + return terminate_jobs(job_queue.arn, prefix; reason=reason) |
| 59 | +end |
0 commit comments