Skip to content
This repository has been archived by the owner on Jan 28, 2021. It is now read-only.

Commit

Permalink
A somewhat paranoid addition, to try to keep us from killing the wron…
Browse files Browse the repository at this point in the history
…g query
  • Loading branch information
vegitron committed Jun 2, 2015
1 parent 644c40a commit 187d5b4
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions sqlshare_rest/util/query_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,22 @@ def worker(q, process_id):
print("Finished query id %s." % oldest_query.pk)
if run_once:
keep_looping = False
else:
# This is designed to protect against this scenario:
# 1) user cancels running query
# 2) we fetch that query from the db, call kill_query in the
# main process
# 3) the query finishes, and the worker process selects a new
# query to run
# 4) we kill the wrong query.
#
# This should be very unlikely, but it would be a nasty bug to
# try to figure out, since it doesn't happen often, but
# potentially has very bad outcomes.
#
# On the plus side, the time between 2 and 3 should be very
# short, so hopefully this 1 second pause more than covers us
sleep(1)

def periodic_check():
"""
Expand Down Expand Up @@ -167,6 +183,10 @@ def kill_query(query, max_current_thread):
query.save()
if pid in process_lookup:
p = process_lookup[pid]
if verbose:
print("Killing pid: %s to "
"terminate query: %s" % (pid,
query.pk))
try:
p.terminate()
p.join()
Expand Down

0 comments on commit 187d5b4

Please sign in to comment.