Commit 003c0e99 authored by Jed Simson's avatar Jed Simson

Improve job dispatcher

parent 31936f46
......@@ -59,6 +59,17 @@ def new_job():
return jsonify({'error': str(e)}), 404
@api.route('/jobs/stop/', methods=['GET'])
def stop_jobs():
try:
job_id = dispatcher.stop_running_jobs()
return jsonify({'success': 'Stopped Job with ID: {}'
.format(job_id)})
except JobNotFoundError as e:
return jsonify({'status': str(e)}), 404
@api.route('/jobs/<job_id>')
def job_status(job_id):
try:
......@@ -98,6 +109,17 @@ def job_status(job_id):
if 'job_id' in session:
del session['job_id']
elif state == 'stopped':
response = {
'state': state,
'status': 'It works!'
}
status = 200
if 'job_id' in session:
del session['job_id']
else:
# something went wrong in the background job
......@@ -113,6 +135,9 @@ def job_status(job_id):
}
status = 500
if 'job_id' in session:
del session['job_id']
return jsonify(response), status
......
......@@ -43,13 +43,32 @@ class Dispatcher:
for key, queue in queues.items():
if job_id in queue.job_ids:
job = queue.fetch_job(job_id)
return job
return queue.fetch_job(job_id)
raise JobNotFoundError('Job with ID {} not found on any queue'
.format(job_id))
def stop_running_jobs(self):
if self.is_job_running():
job = self.get_running_job()
self.log('Cancelling Job<{}> and removing from session.'
.format(job.get_id()))
# RQ does not allow for actually killing the
# workhorse process responsible for the job,
# so the best we can do is to set the job's
# state to `finished`, cancel the job,
# and remove it from the queue.
job.set_status('stopped')
job.cancel()
job.save()
return job.get_id()
raise JobNotFoundError('No jobs are currently running.')
def get_running_job(self):
if self.is_job_running():
# There is already a job running so fetch information
......
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -24,6 +24,7 @@ if __name__ == '__main__':
worker.push_exc_handler(timeout_exception_handler)
workers = []
for i in range(5):
p = multiprocessing.Process(target=worker.work)
workers.append(p)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment