Skip to content

Commit

Permalink
controllers/jobs: fix PUT method
Browse files Browse the repository at this point in the history
Due to a mismatch in name, post requests were getting sent to a function meant to handle only PUT requests.

Signed-off-by: Aishwarya Mathuria <[email protected]>
  • Loading branch information
amathuria committed Oct 18, 2023
1 parent e7506c7 commit 6bca229
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 35 deletions.
43 changes: 10 additions & 33 deletions paddles/controllers/jobs.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import logging
from sqlalchemy import Sequence
from sqlalchemy.orm import load_only

from pecan import expose, abort, request, conf
Expand Down Expand Up @@ -43,7 +42,7 @@ def index(self):

@index.when(method='PUT', template='json')
@retryOperation(attempts=100)
def index_post(self):
def index_put(self, job_id):
"""
We update a job here, it should obviously exist already but most likely
the data is empty.
Expand Down Expand Up @@ -126,46 +125,24 @@ def index_post(self):
data = request.json
if not data:
raise ValueError()
config = dict(conf.sqlalchemy)
if 'sqlite' in config['url']:
# Need this check since Sequence is not supported in SQLite
job = Session.query(Job).order_by(Job.id.desc()).first()
if job:
job_id = job.id + 1
else:
job_id = 1
else:
job_id = Session.execute(Sequence('jobs_id_seq'))
job_id = str(job_id)
data['job_id'] = job_id
data['id'] = int(job_id)
except ValueError:
rollback()
error('/errors/invalid/', 'could not decode JSON body')
# we allow empty data to be pushed
if not job_id:
error('/errors/invalid/', "could not find required key: 'job_id'")
self.run = self._find_run()
if not self.run: self._create_run()

job_id = data['job_id'] = str(job_id)

self._create_job(job_id, data)
return dict({'job_id':job_id})
job = self._create_job(data)
return dict({'job_id':job.job_id})

@retryOperation
def _create_job(self, job_id, data):
query = Job.query.options(load_only('id', 'job_id'))
query = query.filter_by(job_id=job_id, run=self.run)
if query.first():
error('/errors/invalid/',
"job with job_id %s already exists" % job_id)
else:
log.info("Creating job: %s/ Job ID: %s", data.get('name', '<no name!>'),
job_id)
self.job = Job(data, self.run)
Session.commit()
return self.job
def _create_job(self, data):
log.info("Creating job: %s", data.get('name', '<no name!>'))
self.job = Job(data, self.run)
self.job.job_id = self.job.id
Session.commit()
log.info("Job ID of created job is %s", self.job.job_id)
return self.job

@expose('json')
def _lookup(self, job_id, *remainder):
Expand Down
3 changes: 3 additions & 0 deletions paddles/controllers/queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ def index_put(self):
def pop_queue(self, queue):
queue_name = queue
queue = Queue.filter_by(queue=queue_name).first()
if queue is None:
log.info("%s queue is empty! No jobs to retrieve", queue_name)
return None
if queue.paused is True:
error('/errors/unavailable', "queue is paused, cannot retrieve job")
return
Expand Down
2 changes: 0 additions & 2 deletions paddles/models/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,6 @@ class Job(Base):
def __init__(self, json_data, run):
self.run = run
self.posted = datetime.utcnow()
self.id = json_data['id']
self.job_id = json_data['job_id']
self.set_or_update(json_data)

def set_or_update(self, json_data):
Expand Down

0 comments on commit 6bca229

Please sign in to comment.