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():
job_id = dispatcher.stop_running_jobs()
return jsonify({'success': 'Stopped Job with ID: {}'
except JobNotFoundError as e:
return jsonify({'status': str(e)}), 404
def job_status(job_id):
......@@ -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']
# 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'
def stop_running_jobs(self):
if self.is_job_running():
job = self.get_running_job()
self.log('Cancelling Job<{}> and removing from session.'
# 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.
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 diff is collapsed.
......@@ -24,6 +24,7 @@ if __name__ == '__main__':
workers = []
for i in range(5):
p = multiprocessing.Process(
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