def start(args): if args.directory is None: directory = os.path.expanduser("~/.saxo") else: directory = args.directory if not args.foreground: # Save PEP 3122! if "." in __name__: from . import daemon else: import daemon if args.output is None: output = open(os.devnull, "w") elif args.output in {"-", "/dev/stdout"}: output = sys.stdout else: output = open(args.output, "w") pidfile = os.path.join(directory, "pid") daemon.start(pidfile, output) # Save PEP 3122! if "." in __name__: from . import client else: import client client.start(directory) return 0
def startup(config_path=DEFAULT_SETTINGS_FILE): #init logging config = ConfigParser.ConfigParser() config.read(config_path) setup_logging(config) #load the config file and start the listener, daemon logging.debug('reading setting from: %s' % config_path) #Load the plugin manager to get a handle to the plugins. _plugin_manager = PluginManager(config) locator = _plugin_manager.get_resource_locator() datastore = _plugin_manager.get_datastore() driver = _plugin_manager.get_driver() _registrar = Registrar(datastore, driver) #should the listener be started? start_server = config.getboolean('DEFAULT', 'start_server') if start_server: server.set_registrar(registrar) Thread.start(server.start()) #start looking for backends and updating the driver #THIS CALL WILL NOT RETURN daemon.start(_registrar, locator, config)
def startup(config_path=DEFAULT_SETTINGS_FILE): #init logging setup_logging() #load the config file and start the listener, daemon logging.debug("init starting up") config = ConfigParser.ConfigParser() logging.debug('reading setting from: %s' % config_path) config.read(config_path) #Load the plugin manager to get a handle to the plugins. _plugin_manager = PluginManager(config) locator = _plugin_manager.get_resource_locator() datastore = _plugin_manager.get_datastore() driver = _plugin_manager.get_driver() _registrar = Registrar(datastore, driver) #should the listener be started? start_server = config.getboolean('DEFAULT', 'start_server') if start_server: server.set_registrar(registrar) Thread.start(server.start()) #start looking for backends and updating the driver #THIS CALL WILL NOT RETURN daemon.start(_registrar, locator, config)
def main(): config = Config.instance() config.read("%s.ini" % path.splitext(__file__)[0]) daemon = ProxyDaemon() if config.daemon and hasattr(os, "fork"): daemon.start() else: daemon.run()
def minister(): parser = get_parser() options, args = parser.parse_args() if len(args) != 1: parser.print_usage() sys.exit(0) # Address ip, _, port = options.socket.partition(':') if port: port = int(port) address = (ip, port) # User if options.user: address = Manager.listen(address) set_process_owner(options.user) # Path path = args[0] if not os.path.exists(path): os.makedirs(path) # Handle Daemon Stoppping pidfile = os.path.join(path, 'minister.pid') if options.stop: if daemon.stop(pidfile): print "Minister stopped." sys.exit(0) else: sys.exit(1) elif options.restart: if daemon.stop(pidfile): print "Minister stopped." # Config config = get_config( os.path.join(path, 'config.json') ) config['path'] = path if options.debug: config['debug'] = True # Logging setup_logger( echo = options.verbose, path = os.path.join(path, 'logs/minister.log') ) # Daemon Start if options.start or options.restart: print "Minister daemon starting..." daemon.start(pidfile) # Start 'er up. manager = Manager(**config) atexit.register(manager.close) manager.serve(address)
def main(): foreground = False for arg in sys.argv: if "-d" in arg: debug.enable(registry.process) if "-l" in arg: debug.open_log(registry.process, registry.logfile) if "-f" in arg: foreground = True daemon.start(run, foreground)
def main(): if len(sys.argv) != 2: _print_usage_and_exit() action = sys.argv[1] if action == 'start': daemon.start() elif action == 'stop': daemon.stop() elif action == 'status': daemon.status() else: _print_usage_and_exit()
def main_thread(conf): LEVELS = { 'debug': logging.DEBUG, 'info': logging.INFO, 'warning': logging.WARNING, 'error': logging.ERROR, 'critical': logging.CRITICAL } logger = logging.getLogger('default') formatter = logging.Formatter( "%(asctime)s - %(name)s - %(levelname)s - %(message)s") # Add the log message handler to the logger file = logging.handlers.RotatingFileHandler(conf.logging.destination, maxBytes=10485760, backupCount=5) file.setFormatter(formatter) console = logging.StreamHandler() console.setFormatter(formatter) # logger.addHandler(file) logger.addHandler(console) logger.setLevel(LEVELS.get('debug')) logger.info("pgherd staring up...") try: event.set() signal.signal(signal.SIGTERM, handle_event) signal.signal(signal.SIGINT, handle_event) daemon.start(conf) while event.is_set(): logger.info('startup done. wating for signal') signal.pause() except KeyboardInterrupt: logger.info('^C catched - stopping server') raise
def main_thread(conf): LEVELS = { 'debug': logging.DEBUG, 'info': logging.INFO, 'warning': logging.WARNING, 'error': logging.ERROR, 'critical': logging.CRITICAL} logger = logging.getLogger('default') formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s") # Add the log message handler to the logger file = logging.handlers.RotatingFileHandler(conf.logging.destination, maxBytes=10485760, backupCount=5) file.setFormatter(formatter) console = logging.StreamHandler() console.setFormatter(formatter) # logger.addHandler(file) logger.addHandler(console) logger.setLevel(LEVELS.get('debug')) logger.info("pgherd staring up...") try: event.set() signal.signal(signal.SIGTERM, handle_event) signal.signal(signal.SIGINT, handle_event) daemon.start(conf) while event.is_set(): logger.info('startup done. wating for signal') signal.pause() except KeyboardInterrupt: logger.info('^C catched - stopping server') raise
def startup(config_path=DEFAULT_SETTINGS_FILE): ''' Load the config file and start the listener, daemon ''' logging.debug('Init starting up') config = ConfigParser.ConfigParser() logging.debug('Reading setting from: %s' % config_path) config.read(config_path) ''' Load the plugin manager to get a handle to the plugins. ''' plugin_manager = PluginManager(config) _locator = plugin_manager.get_resource_locator() _datastore = plugin_manager.get_datastore() _driver = plugin_manager.get_driver() _registrar = Registrar(_datastore, _driver) ''' Should the listener be started? ''' start_server = config.getboolean('DEFAULT', 'start_server') if start_server: server.set_registrar(registrar) thread.start(server.start()) ''' Start looking for backends and updating the driver ''' ''' THIS CALL WILL NOT RETURN ''' daemon.start(_registrar, _locator, config)
def wrapper(*args, **kwargs): # npm start if not start(num_node): print("[FAIL] npm start") killall() sys.exit(1) # function results = func(*args, **kwargs) # Killall npm if not killall(): print("[FAIL] killall npm") sys.exit(1) return results
def start(args): import json # Quit any previously connected instances if "." in __name__: from .saxo import client, data else: from saxo import client, data base = base_option(args) try: client("quit", base=base) except FileNotFoundError as err: ... except ConnectionRefusedError as err: ... except PermissionError as err: debug("Error: Unable to connect to internal socket", file=sys.stderr) debug("Check permissions on the config dir", file=sys.stderr) sys.stderr.flush() sys.exit() else: debug("Warning: Client may already have been running!") sys.stdout.flush() pidfile = os.path.join(base, "pid") if not args.foreground: # Save PEP 3122! if "." in __name__: from . import daemon else: import daemon if args.log: log = os.path.join(base, "log") if os.path.exists(log): import shutil modified = time.gmtime(os.path.getmtime("saxo")) log2 = time.strftime("log-%Y%m%d-%H%M%S.txt", modified) shutil.move(log, os.path.join(base, log2)) output = open(log, "w") else: output = open(os.devnull, "w") daemon.start(pidfile, output) else: # This is duplicated variatim from daemon.py import atexit with open(pidfile, "w") as f: f.write(str(os.getpid()) + "\n") def delete_pidfile(): if os.path.isfile(pidfile): os.remove(pidfile) atexit.register(delete_pidfile) if args.action == "start": # Otherwise you get recursion if args.action == "restart" os.environ["SAXO_BASE"] = base sys_args = json.dumps(args.original) data("args", sys_args, command="script.py", check=False) # Save PEP 3122! if "." in __name__: from . import irc else: import irc irc.start(base) return 0
filter_list.disable_doh, ] connection.sendall(json.dumps(response).encode('utf-8')) if __name__ == "__main__": daemon = FilterDaemon(socket_path=SOCKET_PATH, filters_dir=FILTERS_DIR, pidfile=PID_FILE) if len(sys.argv) != 2: print("Unknown command") sys.exit(2) if 'start' == sys.argv[1]: daemon.start() elif 'stop' == sys.argv[1]: daemon.stop() delete_socket_path(SOCKET_PATH) elif 'restart' == sys.argv[1]: daemon.stop() delete_socket_path(SOCKET_PATH) daemon.start() elif 'status' == sys.argv[1]: try: pf = file(PID_FILE, 'r') pid = int(pf.read().strip()) pf.close()
if not line.startswith( "data:" ): file.write( "F+ " + line + " E " + url + "\n" ) file.close() except Exception as e: sys.stderr.write( "ERROR: " + str( e ) + "\n" ) class ReceiveDaemon( Daemon ): def run( self ): connection = pika.BlockingConnection( pika.ConnectionParameters( HOST ) ) channel = connection.channel() channel.queue_declare( queue=QUEUE ) channel.basic_consume( callback, queue=QUEUE, no_ack=True ) channel.start_consuming() if __name__ == "__main__": daemon = ReceiveDaemon( "/tmp/daemon-example.pid" ) if len( sys.argv ) == 2: if "start" == sys.argv[ 1 ]: daemon.start() elif "stop" == sys.argv[ 1 ]: daemon.stop() elif "restart" == sys.argv[ 1 ]: daemon.restart() else: print "Unknown command" sys.exit( 2 ) sys.exit( 0 ) else: print "usage: %s start|stop|restart" % sys.argv[ 0 ] sys.exit( 2 )
def handle(self, *args, **options): # pylint: disable=too-many-locals # gevent monkey patch # Note: after patch, gevent-fastcgi will spawn new greenlet for each request, # which means django models will create new connection for each request. # it is suggested to use gunicorn instead of gevent-fastcgi in gevent mode. if options['monkey_patch']: names = filter(None, map(str.strip, options['monkey_patch'].split(','))) if names: module = __import__('gevent.monkey', fromlist=['*']) for name in names: if name not in MONKEY_PATCH_NAMES: raise CommandError( 'Unknown name "{0}" in --monkey-patch option'. format(name)) patch_func = getattr(module, 'patch_{0}'.format(name)) patch_func() # replace MySQLdb with pymysql for gevent import pymysql pymysql.install_as_MySQLdb() import os import sys from os.path import dirname, isdir # patch python2.6 logging module if sys.version_info < (2, 7): import logging class NullHandler(logging.Handler): def emit(self, record): pass logging.NullHandler = NullHandler # patch gevent fastcgi for werkzeug / flask from gevent_fastcgi.base import InputStream def readline(self, size=-1): self._eof_received.wait() return self._file.readline(size) InputStream.readline = readline from gevent_fastcgi.wsgi import WSGIRequest from gevent_fastcgi.interfaces import IRequestHandler from zope.interface import implements from gevent_fastcgi.server import FastCGIServer class WSGIRequestHandler(object): implements(IRequestHandler) def __init__(self, app): self.app = app def __call__(self, fastcgi_request): # pylint: disable=protected-access request = WSGIRequest(fastcgi_request) try: app_iter = self.app(request._environ, request.start_response) request.finish(app_iter) if hasattr(app_iter, 'close'): app_iter.close() except: try: from logger import log logging.exception('handle_http_request_exception') except: pass request.start_response('500 Internal Server Error', [ ('Content-type', 'text/plain'), ]) request.finish(['Internal Server Error (500)']) # subclass gevent fastcgi so each spawned process has distinctive random seeds class GFastCGIServer(FastCGIServer): def start_accepting(self): import random random.seed() return super(GFastCGIServer, self).start_accepting() if not args: raise CommandError('Please specify binding address') if len(args) > 1: raise CommandError('Unexpected arguments: %s' % ' '.join(args[1:])) bind_address = args[0] try: host, port = bind_address.split(':', 1) port = int(port) except ValueError: socket_dir = dirname(bind_address) if not isdir(socket_dir): raise CommandError( 'Please create directory for socket file first %r' % dirname(socket_dir)) else: if options['socket_mode'] is not None: raise CommandError('--socket-mode option can only be used ' 'with Unix domain sockets. Either use ' 'socket file path as address or do not ' 'specify --socket-mode option') bind_address = (host, port) pid_file = options.get('daemon') if pid_file: import daemon daemon = daemon.Daemon(None, pid_file, options.get('out_log', '/dev/null')) daemon.start() kwargs = dict(((name, value) for name, value in options.iteritems() if name in ('num_workers', 'max_conns', 'buffer_size', 'socket_mode'))) os.chdir(options['our_home_dir']) if options['our_home_dir'] not in sys.path: sys.path.append(options['our_home_dir']) wsgi_args = options['wsgi'].split(':') wsgi_module = wsgi_args[0] if len(wsgi_args) > 1: wsgi_object = wsgi_args[1] else: wsgi_object = 'app' app_module = __import__(wsgi_module, fromlist=[wsgi_object]) app = getattr(app_module, wsgi_object) request_handler = WSGIRequestHandler(app) server = GFastCGIServer(bind_address, request_handler, **kwargs) server.serve_forever()
def cmd_daemon(metasync, args, opts): "invoke a daemon (and wait) - currently disabled" if (opts.debug): daemon.start(metasync, args, opts) else: dbg.err("Currently daemon is supported only for debug mode.")
def cmd_daemon(metasync, args, opts): "invoke a daemon (and wait) - currently disabled" if(opts.debug): daemon.start(metasync, args, opts) else: dbg.err("Currently daemon is supported only for debug mode.")
def main(): global config config = config_loader.load_config() daemon = InputDaemon(pidfile=config['pidfile'], stdout=config['stdout'], stderr=config['stderr']) daemon.start() return 0