Skip to content

Commit 10b1fa0

Browse files
committed
Validate job on batch propose
1 parent 456fa3e commit 10b1fa0

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed

api/jobs/handlers.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,16 @@ def post_with_jobs(self):
707707
payload = self.request.json
708708
jobs_ = payload.get('jobs', [])
709709

710+
uid = None
711+
if not self.superuser_request:
712+
uid = self.uid
713+
714+
for job_number, job_ in enumerate(jobs_):
715+
try:
716+
Queue.validate_job(job_, self.origin, create_job=False, perm_check_uid=uid)
717+
except InputValidationException as e:
718+
raise InputValidationException("Job {}: {}".format(job_number, str(e)))
719+
710720
batch_proposal = {
711721
'proposal': {
712722
'preconstructed_jobs': jobs_

api/jobs/queue.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,10 @@ def retry(job, force=False):
128128

129129
return new_id
130130

131-
132131
@staticmethod
133-
def enqueue_job(job_map, origin, perm_check_uid=None):
132+
def validate_job(job_map, origin, create_job=False, perm_check_uid=None):
134133
"""
135-
Using a payload for a proposed job, creates and returns (but does not insert)
134+
Using a payload for a proposed job, creates and returns(if create_job is True) (but does not insert)
136135
a Job object. This preperation includes:
137136
- confirms gear exists
138137
- validates config against gear manifest
@@ -251,8 +250,17 @@ def enqueue_job(job_map, origin, perm_check_uid=None):
251250

252251
if gear_name not in tags:
253252
tags.append(gear_name)
253+
if create_job:
254+
job = Job(str(gear['_id']), inputs, destination=destination, tags=tags, config_=config_, now=now_flag, attempt=attempt_n, previous_job_id=previous_job_id, origin=origin, batch=batch)
255+
return job
256+
return True
254257

255-
job = Job(str(gear['_id']), inputs, destination=destination, tags=tags, config_=config_, now=now_flag, attempt=attempt_n, previous_job_id=previous_job_id, origin=origin, batch=batch)
258+
@staticmethod
259+
def enqueue_job(job_map, origin, perm_check_uid=None):
260+
"""
261+
Validates, Creates, Inserts, and Returns job
262+
"""
263+
job = Queue.validate_job(job_map, origin, create_job=True, perm_check_uid=perm_check_uid)
256264
job.insert()
257265
return job
258266

tests/integration_tests/python/test_batch.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,30 @@ def test_batch(data_builder, as_user, as_admin, as_root):
8484
assert r.ok
8585
analysis_batch_id = r.json()['_id']
8686

87+
# try to create a batch with invalid preconstructed jobs
88+
r = as_admin.post('/batch/jobs', json={
89+
'jobs': [
90+
{
91+
'gear_id': gear,
92+
'inputs': {
93+
'dicom': {
94+
'type': 'acquisition',
95+
'id': acquisition,
96+
'name': 'test.zip'
97+
}
98+
},
99+
'config': { 'two-digit multiple of ten': 20 },
100+
'destination': {
101+
'type': 'acquisition',
102+
'id': acquisition
103+
},
104+
'tags': [ 'test-tag' ]
105+
}
106+
]
107+
})
108+
assert r.status_code == 400
109+
assert "Job 0" in r.json().get('message')
110+
87111
# create a batch with preconstructed jobs
88112
r = as_admin.post('/batch/jobs', json={
89113
'jobs': [

0 commit comments

Comments
 (0)