Implemented Terminate, Cancel and List jobs
This commit is contained in:
parent
ddd52a5a97
commit
e3024ae1ba
4 changed files with 249 additions and 3 deletions
|
|
@ -196,6 +196,7 @@ class Job(threading.Thread, BaseModel):
|
|||
self.job_started_at = datetime.datetime(1970, 1, 1)
|
||||
self.job_stopped_at = datetime.datetime(1970, 1, 1)
|
||||
self.job_stopped = False
|
||||
self.job_stopped_reason = None
|
||||
|
||||
self.stop = False
|
||||
|
||||
|
|
@ -230,6 +231,8 @@ class Job(threading.Thread, BaseModel):
|
|||
}
|
||||
if self.job_stopped:
|
||||
result['stoppedAt'] = datetime2int(self.job_stopped_at)
|
||||
if self.job_stopped_reason is not None:
|
||||
result['statusReason'] = self.job_stopped_reason
|
||||
return result
|
||||
|
||||
def run(self):
|
||||
|
|
@ -328,6 +331,11 @@ class Job(threading.Thread, BaseModel):
|
|||
self.job_stopped = True
|
||||
self.job_stopped_at = datetime.datetime.now()
|
||||
|
||||
def terminate(self, reason):
|
||||
if not self.stop:
|
||||
self.stop = True
|
||||
self.job_stopped_reason = reason
|
||||
|
||||
|
||||
class BatchBackend(BaseBackend):
|
||||
def __init__(self, region_name=None):
|
||||
|
|
@ -478,6 +486,20 @@ class BatchBackend(BaseBackend):
|
|||
|
||||
return result
|
||||
|
||||
def get_job_by_id(self, identifier):
|
||||
"""
|
||||
Get job by id
|
||||
:param identifier: Job ID
|
||||
:type identifier: str
|
||||
|
||||
:return: Job
|
||||
:rtype: Job
|
||||
"""
|
||||
try:
|
||||
return self._jobs[identifier]
|
||||
except KeyError:
|
||||
return None
|
||||
|
||||
def describe_compute_environments(self, environments=None, max_results=None, next_token=None):
|
||||
envs = set()
|
||||
if environments is not None:
|
||||
|
|
@ -916,6 +938,36 @@ class BatchBackend(BaseBackend):
|
|||
|
||||
return result
|
||||
|
||||
def list_jobs(self, job_queue, job_status=None, max_results=None, next_token=None):
|
||||
jobs = []
|
||||
|
||||
job_queue = self.get_job_queue(job_queue)
|
||||
if job_queue is None:
|
||||
raise ClientException('Job queue {0} does not exist'.format(job_queue))
|
||||
|
||||
if job_status is not None and job_status not in ('SUBMITTED', 'PENDING', 'RUNNABLE', 'STARTING', 'RUNNING', 'SUCCEEDED', 'FAILED'):
|
||||
raise ClientException('Job status is not one of SUBMITTED | PENDING | RUNNABLE | STARTING | RUNNING | SUCCEEDED | FAILED')
|
||||
|
||||
for job in job_queue.jobs:
|
||||
if job_status is not None and job.job_state != job_status:
|
||||
continue
|
||||
|
||||
jobs.append(job)
|
||||
|
||||
return jobs
|
||||
|
||||
def terminate_job(self, job_id, reason):
|
||||
if job_id is None:
|
||||
raise ClientException('Job ID does not exist')
|
||||
if reason is None:
|
||||
raise ClientException('Reason does not exist')
|
||||
|
||||
job = self.get_job_by_id(job_id)
|
||||
if job is None:
|
||||
raise ClientException('Job not found')
|
||||
|
||||
job.terminate(reason)
|
||||
|
||||
|
||||
available_regions = boto3.session.Session().get_available_regions("batch")
|
||||
batch_backends = {region: BatchBackend(region_name=region) for region in available_regions}
|
||||
|
|
|
|||
|
|
@ -263,3 +263,42 @@ class BatchResponse(BaseResponse):
|
|||
return json.dumps({'jobs': self.batch_backend.describe_jobs(jobs)})
|
||||
except AWSError as err:
|
||||
return err.response()
|
||||
|
||||
# ListJobs
|
||||
def listjobs(self):
|
||||
job_queue = self._get_param('jobQueue')
|
||||
job_status = self._get_param('jobStatus')
|
||||
max_results = self._get_param('maxResults')
|
||||
next_token = self._get_param('nextToken')
|
||||
|
||||
try:
|
||||
jobs = self.batch_backend.list_jobs(job_queue, job_status, max_results, next_token)
|
||||
except AWSError as err:
|
||||
return err.response()
|
||||
|
||||
result = {'jobSummaryList': [{'jobId': job.job_id, 'jobName': job.job_name} for job in jobs]}
|
||||
return json.dumps(result)
|
||||
|
||||
# TerminateJob
|
||||
def terminatejob(self):
|
||||
job_id = self._get_param('jobId')
|
||||
reason = self._get_param('reason')
|
||||
|
||||
try:
|
||||
self.batch_backend.terminate_job(job_id, reason)
|
||||
except AWSError as err:
|
||||
return err.response()
|
||||
|
||||
return ''
|
||||
|
||||
# CancelJob
|
||||
def canceljob(self): # Theres some AWS semantics on the differences but for us they're identical ;-)
|
||||
job_id = self._get_param('jobId')
|
||||
reason = self._get_param('reason')
|
||||
|
||||
try:
|
||||
self.batch_backend.terminate_job(job_id, reason)
|
||||
except AWSError as err:
|
||||
return err.response()
|
||||
|
||||
return ''
|
||||
|
|
|
|||
|
|
@ -18,5 +18,8 @@ url_paths = {
|
|||
'{0}/v1/deregisterjobdefinition': BatchResponse.dispatch,
|
||||
'{0}/v1/describejobdefinitions': BatchResponse.dispatch,
|
||||
'{0}/v1/submitjob': BatchResponse.dispatch,
|
||||
'{0}/v1/describejobs': BatchResponse.dispatch
|
||||
'{0}/v1/describejobs': BatchResponse.dispatch,
|
||||
'{0}/v1/listjobs': BatchResponse.dispatch,
|
||||
'{0}/v1/terminatejob': BatchResponse.dispatch,
|
||||
'{0}/v1/canceljob': BatchResponse.dispatch,
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue