Batch:list_jobs() - extend list of return fields (#4727)

This commit is contained in:
Bert Blommers 2021-12-28 13:02:18 -01:00 committed by GitHub
parent 36d5e1d7df
commit e020b06016
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 15 deletions

View File

@ -415,6 +415,7 @@ class Job(threading.Thread, BaseModel, DockerModel):
self.all_jobs = all_jobs
self.stop = False
self.exit_code = None
self.daemon = True
self.name = "MOTO-BATCH-" + self.job_id
@ -422,18 +423,28 @@ class Job(threading.Thread, BaseModel, DockerModel):
self._log_backend = log_backend
self.log_stream_name = None
def describe(self):
def describe_short(self):
result = {
"jobDefinition": self.job_definition.arn,
"jobId": self.job_id,
"jobName": self.job_name,
"jobQueue": self.job_queue.arn,
"status": self.job_state,
"dependsOn": self.depends_on if self.depends_on else [],
"createdAt": datetime2int_milliseconds(self.job_created_at),
"status": self.job_state,
"jobDefinition": self.job_definition.arn,
}
if self.job_stopped_reason is not None:
result["statusReason"] = self.job_stopped_reason
if result["status"] not in ["SUBMITTED", "PENDING", "RUNNABLE", "STARTING"]:
result["startedAt"] = datetime2int_milliseconds(self.job_started_at)
if self.job_stopped:
result["stoppedAt"] = datetime2int_milliseconds(self.job_stopped_at)
if self.exit_code is not None:
result["container"] = {"exitCode": self.exit_code}
return result
def describe(self):
result = self.describe_short()
result["jobQueue"] = self.job_queue.arn
result["dependsOn"] = self.depends_on if self.depends_on else []
if self.job_stopped:
result["stoppedAt"] = datetime2int_milliseconds(self.job_stopped_at)
result["container"] = {}
@ -452,8 +463,6 @@ class Job(threading.Thread, BaseModel, DockerModel):
"environment", []
)
result["container"]["logStreamName"] = self.log_stream_name
if self.job_stopped_reason is not None:
result["statusReason"] = self.job_stopped_reason
if self.timeout:
result["timeout"] = self.timeout
return result
@ -630,7 +639,8 @@ class Job(threading.Thread, BaseModel, DockerModel):
self._log_backend.put_log_events(log_group, stream_name, logs, None)
result = container.wait() or {}
job_failed = self.stop or result.get("StatusCode", 0) > 0
self.exit_code = result.get("StatusCode", 0)
job_failed = self.stop or self.exit_code > 0
self._mark_stopped(success=not job_failed)
except Exception as err:

View File

@ -276,11 +276,7 @@ class BatchResponse(BaseResponse):
except AWSError as err:
return err.response()
result = {
"jobSummaryList": [
{"jobId": job.job_id, "jobName": job.job_name} for job in jobs
]
}
result = {"jobSummaryList": [job.describe_short() for job in jobs]}
return json.dumps(result)
# TerminateJob

View File

@ -160,6 +160,14 @@ def test_list_jobs():
)
job_id2 = resp["jobId"]
all_jobs = batch_client.list_jobs(jobQueue=queue_arn)["jobSummaryList"]
all_jobs.should.have.length_of(2)
for job in all_jobs:
job.should.have.key("createdAt")
job.should.have.key("jobDefinition")
job.should.have.key("jobName")
job.should.have.key("status").equals("STARTING")
batch_client.list_jobs(jobQueue=queue_arn, jobStatus="SUCCEEDED")[
"jobSummaryList"
].should.have.length_of(0)
@ -168,9 +176,17 @@ def test_list_jobs():
for job_id in [job_id1, job_id2]:
_wait_for_job_status(batch_client, job_id, "SUCCEEDED")
batch_client.list_jobs(jobQueue=queue_arn, jobStatus="SUCCEEDED")[
succeeded_jobs = batch_client.list_jobs(jobQueue=queue_arn, jobStatus="SUCCEEDED")[
"jobSummaryList"
].should.have.length_of(2)
]
succeeded_jobs.should.have.length_of(2)
for job in succeeded_jobs:
job.should.have.key("createdAt")
job.should.have.key("jobDefinition")
job.should.have.key("jobName")
job.should.have.key("status").equals("SUCCEEDED")
job.should.have.key("stoppedAt")
job.should.have.key("container").should.have.key("exitCode").equals(0)
@mock_logs