-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathrun.py
executable file
·66 lines (58 loc) · 2.14 KB
/
run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python
import os
import sys
import time
import logging
import optparse
import configobj
import traceback
from daemon import DaemonContext
from daemon.pidfile import TimeoutPIDLockFile
from hyclops.queue import MessageQueue
# parse command-line options
parser = optparse.OptionParser()
basedir = os.path.dirname(os.path.abspath(__file__))
parser.add_option('-c', dest="config_file", help="config file location", default=os.path.join(basedir, "hyclops.conf"))
parser.add_option('--pid', dest="pid_file", help="pid file location", default="/tmp/hyclops.pid")
(options, args) = parser.parse_args()
# load config file
try:
config = configobj.ConfigObj(options.config_file, file_error=True)
except IOError, e:
sys.exit(e)
if os.path.exists(options.pid_file):
sys.exit("pid file (%s) already exists" % options.pid_file)
# start daemon
with DaemonContext(pidfile=TimeoutPIDLockFile(options.pid_file, 1), stderr=sys.stderr):
# logging settings
log_level = config["logging"]["log_level"]
log_file = config["logging"]["log_file"]
log_format = '[%(asctime)s] %(name)s (%(threadName)s) %(levelname)s: %(message)s'
logging.basicConfig(filename=log_file, level=logging.WARNING, format=log_format)
logger = logging.getLogger('hyclops')
logger.setLevel(getattr(logging, log_level))
# add environments
for key, value in config["environments"].items():
os.environ[key] = value
# create queue
listen_address = config["hyclops"]["listen_address"]
listen_port = config["hyclops"]["listen_port"]
queue = MessageQueue(config)
try:
queue.bind(listen_address, listen_port)
logger.info("Message queue is opened")
except Exception, e:
err_msg = "Failed to bind ZeroMQ socket: %s" % str(e)
logger.error(err_msg)
sys.exit(err_msg)
# polling loop
while True:
try:
queue.poll()
time.sleep(3)
except (KeyboardInterrupt, SystemExit), e:
queue.close()
logger.info("Message queue is closed by %s" % e.__class__.__name__)
break
except Exception, e:
logger.error(traceback.format_exc())