Commit c697dc2c authored by Petr Pudlak's avatar Petr Pudlak
Browse files

Cancel job outside the SIGTERM handler

.. because modifying the queue inside the handler can have unexpected

Since Python 2 doesn't have a nice way how to modify a variable from
an inner function, we have to use a list as a wrapper.
(Python 3 has the "nonlocal" keyword for it.)
Signed-off-by: default avatarPetr Pudlak <>
Reviewed-by: default avatarKlaus Aehlig <>
parent 58d29849
...@@ -69,9 +69,11 @@ def main(): ...@@ -69,9 +69,11 @@ def main():
logging.debug("Registering a SIGTERM handler") logging.debug("Registering a SIGTERM handler")
cancel = [False]
def _TermHandler(signum, _frame): def _TermHandler(signum, _frame):"Killed by signal %d", signum)"Killed by signal %d", signum)
context.jobqueue.CancelJob(job_id) cancel[0] = True
signal.signal(signal.SIGTERM, _TermHandler) signal.signal(signal.SIGTERM, _TermHandler)
logging.debug("Picking up job %d", job_id) logging.debug("Picking up job %d", job_id)
...@@ -80,6 +82,11 @@ def main(): ...@@ -80,6 +82,11 @@ def main():
# waiting for the job to finish # waiting for the job to finish
time.sleep(1) time.sleep(1)
while not context.jobqueue.HasJobBeenFinalized(job_id): while not context.jobqueue.HasJobBeenFinalized(job_id):
if cancel[0]:
logging.debug("Got cancel request, cancelling job %d", job_id)
r = context.jobqueue.CancelJob(job_id)
logging.debug("CancelJob result for job %d: %s", job_id, r)
cancel[0] = False
time.sleep(1) time.sleep(1)
# wait until the queue finishes # wait until the queue finishes
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