38 |
38 |
"""
|
39 |
39 |
from django.core.management import setup_environ
|
40 |
40 |
|
|
41 |
# Fix path to import synnefo settings
|
41 |
42 |
import sys
|
42 |
43 |
import os
|
43 |
44 |
path = os.path.normpath(os.path.join(os.getcwd(), '..'))
|
... | ... | |
46 |
47 |
from synnefo import settings
|
47 |
48 |
setup_environ(settings)
|
48 |
49 |
|
49 |
|
from amqplib import client_0_8 as amqp
|
50 |
|
from signal import signal, SIGINT, SIGTERM
|
51 |
|
|
52 |
50 |
import logging
|
53 |
51 |
import time
|
54 |
|
import socket
|
55 |
|
from daemon import daemon
|
56 |
52 |
|
57 |
|
from synnefo.lib.amqp import AMQPClient
|
|
53 |
import daemon.runner
|
|
54 |
import daemon.daemon
|
|
55 |
from lockfile import LockTimeout
|
|
56 |
from signal import signal, SIGINT, SIGTERM
|
58 |
57 |
|
59 |
58 |
# Take care of differences between python-daemon versions.
|
60 |
59 |
try:
|
61 |
|
from daemon import pidfile
|
|
60 |
from daemon import pidfile as pidlockfile
|
62 |
61 |
except:
|
63 |
62 |
from daemon import pidlockfile
|
64 |
63 |
|
|
64 |
from synnefo.lib.amqp import AMQPClient
|
65 |
65 |
from synnefo.logic import callbacks
|
66 |
66 |
from synnefo.util.dictconfig import dictConfig
|
67 |
67 |
|
... | ... | |
164 |
164 |
# Queue # Exchange # RouteKey # Handler
|
165 |
165 |
(QUEUE_GANETI_EVENTS_OP, settings.EXCHANGE_GANETI, DB_HANDLER_KEY_OP, 'update_db'),
|
166 |
166 |
(QUEUE_GANETI_EVENTS_NET, settings.EXCHANGE_GANETI, DB_HANDLER_KEY_NET, 'update_net'),
|
167 |
|
(QUEUE_GANETI_BUILD_PROGR,settings.EXCHANGE_GANETI, BUILD_MONITOR_HANDLER, 'update_build_progress'),
|
|
167 |
(QUEUE_GANETI_BUILD_PROGR, settings.EXCHANGE_GANETI, BUILD_MONITOR_HANDLER, 'update_build_progress'),
|
168 |
168 |
]
|
169 |
169 |
|
170 |
170 |
if settings.DEBUG is True:
|
... | ... | |
187 |
187 |
""""Catch exit signal in parent process and forward it to children."""
|
188 |
188 |
global children
|
189 |
189 |
log.info("Caught signal %d, sending SIGTERM to children %s",
|
190 |
|
signum, children)
|
|
190 |
signum, children)
|
191 |
191 |
[os.kill(pid, SIGTERM) for pid in children]
|
192 |
192 |
|
193 |
193 |
|
... | ... | |
205 |
205 |
def parse_arguments(args):
|
206 |
206 |
from optparse import OptionParser
|
207 |
207 |
|
208 |
|
default_pid_file = os.path.join("var","run","synnefo","dispatcher.pid")
|
|
208 |
default_pid_file = os.path.join("var", "run", "synnefo", "dispatcher.pid")
|
209 |
209 |
parser = OptionParser()
|
210 |
210 |
parser.add_option("-d", "--debug", action="store_true", default=False,
|
211 |
211 |
dest="debug", help="Enable debug mode")
|
... | ... | |
332 |
332 |
global children
|
333 |
333 |
|
334 |
334 |
# Create pidfile,
|
335 |
|
# take care of differences between python-daemon versions
|
336 |
|
try:
|
337 |
|
pidf = pidfile.TimeoutPIDLockFile(opts.pid_file, 10)
|
338 |
|
except:
|
339 |
|
pidf = pidlockfile.TimeoutPIDLockFile(opts.pid_file, 10)
|
|
335 |
pidf = pidlockfile.TimeoutPIDLockFile(opts.pid_file, 10)
|
340 |
336 |
|
341 |
|
pidf.acquire()
|
|
337 |
if daemon.runner.is_pidfile_stale(pidf):
|
|
338 |
log.warning("Removing stale PID lock file %s", pidf.path)
|
|
339 |
pidf.break_lock()
|
|
340 |
|
|
341 |
try:
|
|
342 |
pidf.acquire()
|
|
343 |
except (pidlockfile.AlreadyLocked, LockTimeout):
|
|
344 |
log.critical("Failed to lock pidfile %s, another instance running?",
|
|
345 |
pidf.path)
|
|
346 |
sys.exit(1)
|
342 |
347 |
|
343 |
348 |
log.info("Became a daemon")
|
344 |
349 |
|
... | ... | |
375 |
380 |
|
376 |
381 |
|
377 |
382 |
def main():
|
|
383 |
(opts, args) = parse_arguments(sys.argv[1:])
|
|
384 |
|
378 |
385 |
dictConfig(settings.DISPATCHER_LOGGING)
|
379 |
386 |
|
380 |
387 |
global log
|
381 |
388 |
|
382 |
|
(opts, args) = parse_arguments(sys.argv[1:])
|
383 |
|
|
384 |
389 |
# Init the global variables containing the queues
|
385 |
390 |
_init_queues()
|
386 |
391 |
|
... | ... | |
409 |
414 |
if stream and hasattr(stream, 'fileno'):
|
410 |
415 |
files_preserve.append(handler.stream)
|
411 |
416 |
|
412 |
|
daemon_context = daemon.DaemonContext(
|
|
417 |
daemon_context = daemon.daemon.DaemonContext(
|
413 |
418 |
files_preserve=files_preserve,
|
414 |
419 |
umask=022)
|
415 |
420 |
|