Skip to content
Snippets Groups Projects
Commit fc74c9dc authored by Anael Beutot's avatar Anael Beutot
Browse files

Rewrite binary.

parent 5a57e4d2
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/env python
import sys
import signal
import atexit
import time
import logging
import logging.config
from optparse import OptionParser
from os.path import isfile, abspath
from daemon import DaemonContext
from ccnode import __version__
from ccnode.node import Node
from ccnode.utils import signal_
from ccnode.config import NodeConfigParser
logger = logging.getLogger('ccnode')
from ccnode.node import MainLoop
DEFAULT_CONFIG_FILE = '/etc/cc-node.conf'
......@@ -33,6 +24,7 @@ oparser.add_option('-c', '--config', metavar='FILE',
oparser.add_option('-p', '--pidfile', metavar='FILE',
help=u'pid file path for daemon mode')
options, args = oparser.parse_args()
# check argument configuration
if options.daemonize and not options.pidfile:
sys.exit(u'Please supply a pid file...')
......@@ -42,59 +34,6 @@ if not isfile(options.config):
sys.exit(u'Please supply a valid path to configuration file...')
# globals
need_reload = False
node = None
config_file = NodeConfigParser(options.config)
# configure logging
logging.config.fileConfig(options.config)
@signal_(signal.SIGUSR1)
def reload_config(signum, frame):
global need_reload
need_reload = True
@signal_(signal.SIGTERM)
def exit_node(signum=None, frame=None):
# clean all current jobs
# TODO
if node is not None:
# clean node
node.shutdown()
# exit
logger.info(u'Exiting node...')
sys.exit()
def run_node():
global need_reload, node
try:
node = Node(
server_host=config_file.server_host,
server_port=config_file.server_port,
user_name=config_file.server_user,
user_passwd=config_file.server_passwd,
)
node.start()
while True:
if need_reload:
logger.info(u'Reloading logging configuration...')
logging.config.fileConfig(options.config)
need_reload = False
signal.pause()
except KeyboardInterrupt:
exit_node()
except Exception:
logger.exception(u'Unknown error:')
# take care of pid file if daemon
if options.daemonize:
pidfile = open(options.pidfile)
......@@ -102,13 +41,10 @@ if options.daemonize:
else:
files_preserve = None
with DaemonContext(detach_process=options.daemonize,
files_preserve=files_preserve,
stderr=sys.stderr,
stdout=sys.stdout):
# reload log config
logging.config.fileConfig(options.config)
# take care of pidfile
if options.daemonize:
......@@ -121,9 +57,4 @@ with DaemonContext(detach_process=options.daemonize,
pidfile.truncate()
pidfile.flush()
logger.debug(u'Starting node')
while True:
run_node()
logger.critical(u'Restarting node...')
time.sleep(2)
MainLoop(options.config).start()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment