Skip to content

Commit

Permalink
Warm shutdown when Ctrl+C is pressed
Browse files Browse the repository at this point in the history
  • Loading branch information
fajran committed Feb 27, 2011
1 parent f0f3c87 commit ac9f839
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions irgsh_node/uploader.py
@@ -1,5 +1,7 @@
import os
import logging
import signal
import sys
import time
import tempfile
from cStringIO import StringIO
Expand All @@ -21,12 +23,27 @@ def __init__(self):
self.log = logging.getLogger('irgsh_node.uploader')
self.queue = Queue(settings.LOCAL_DATABASE)

self.force_stop = False

def stop(self):
self.stopped = True

def start(self):
signal.signal(signal.SIGINT, self.sigint_handler)

self.force_stop = False
self.run()

def sigint_handler(self, sig, frame):
self.stopped = True
if not self.force_stop:
self.log.info('Ctrl+C was pressed, finishing current job..')
self.log.info('Press Ctrl+C again to force stop the uploader.')
self.force_stop = True
else:
self.log.info('Stopping the uploader by force.')
sys.exit(1)

def run(self):
delay = 0.1

Expand Down Expand Up @@ -129,11 +146,6 @@ def main():

# TODO
# - add ability to launch multiple uploaders
# - catch SIGTERM/Ctrl+C and perform warm shutdown
# (tell all workers to finish ongoing upload but do not proceed
# to the next item in the queue)
# - if SIGTERM is received again, force all workers to stop


if __name__ == '__main__':
main()
Expand Down

0 comments on commit ac9f839

Please sign in to comment.