def runcpserver(*args): # Get the options options = CPSERVER_OPTIONS.copy() for arg in args: if '=' in arg: k, v = arg.split('=', 1) options[k] = v if "help" in args: print CPSERVER_HELP return if "stop" in args: stop_server(options['pidfile']) return True if not options['pidfile']: options['pidfile'] = '/var/run/django/cpserver_%s.pid' % options['port'] #stop_server(options['pidfile']) if options['daemonize'] == '1': from django.utils.daemonize import become_daemon if options['workdir']: become_daemon(our_home_dir=options['workdir']) else: become_daemon() # write the pidfile fp = open(options['pidfile'], 'w') fp.write("%d\n" % os.getpid()) fp.close() # Start the webserver start_server(options)
def handle(self, *app_labels, **options): verbose = options["verbose"] make_daemon = options["make_daemon"] loop = options["loop"] timeout = options["timeout"] rebuild_index = options["rebuild_index"] per_page = options["per_page"] commit_each = options["commit_each"] utils.load_indexes() if make_daemon: become_daemon() if app_labels: try: app_list = [models.get_app(app_label) for app_label in app_labels] except (ImproperlyConfigured, ImportError), e: raise CommandError("%s. Are you sure your INSTALLED_APPS setting is correct?" % e) for app in app_list: app_models = models.get_models(app, include_auto_created=True) if rebuild_index: rebuild(verbose, per_page, commit_each, app_models) else: update_changes(verbose, timeout, not (loop or make_daemon), per_page, commit_each, app_models)
def handle( self, *args, **options ): # Run as daemon, ie. fork the process if options['daemon']: from django.utils.daemonize import become_daemon become_daemon() # Running as daemon now. PID is fpid pid_file = file(options['pidfile'], "w") pid_file.write(str(os.getpid())) pid_file.close() try: # Specify polling frequency either on the command-line or inside settings if args and args[0].strip(): time_wait = float(args[0]) else: time_wait = getattr(settings, "CRONSERVER_FREQUENCY", 60) except: raise CommandError("Invalid wait time: %s is not a number." % args[0]) try: sys.stdout.write("Starting cronserver. Jobs will run every %d seconds.\n" % time_wait) if not options['daemon']: sys.stdout.write("Quit the server with CONTROL-C.\n") # Run server until killed while True: thread = CronThread(gc=options.get("gc", False), mp=options.get("prof", False)) thread.start() sleep(time_wait) except KeyboardInterrupt: logging.info("Exiting...\n") sys.exit()
def runserver(self, *args, **options): #if "help" in options: # print CPSERVER_HELP # return if not options['pidfile']: options['pidfile'] = os.path.join(PID_ROOT, '%s.pid'%self.get_command_name()) if options['stop']: self.stop_server(options['pidfile']) return True if options['daemon']: self.stop_server(options['pidfile']) from django.utils.daemonize import become_daemon become_daemon() fp = open(options['pidfile'], 'w') fp.write("%d\n" % os.getpid()) fp.close() # Start the server #pool = Pool(workers=options.get('workers', 0)) #pool.put_task(self.start_server, *[options]) #pool.start() #pool.stop() self.start_server(options)
def runserver(argset=[], **kwargs): # Get the options options = SERVER_OPTIONS.copy() options.update(kwargs) from net.aircable.utils import getLogger logger = getLogger(__name__) if options['daemonize']: if not options['pidfile']: options['pidfile'] = '/var/run/openproximity_rpc_%s.pid' % \ options['port'] logger.info("storing pid file in %s" % options['pidfile']) from django.utils.daemonize import become_daemon logger.info("daemonizing") become_daemon() logger.info("daemonized") fp = open(options['pidfile'], 'w') fp.write("%d\n" % os.getpid()) fp.close() # Start the rpc server logger.debug('starting server with options %s' % options) start_server(options)
def handle(self, *args, **options): daemonize.become_daemon() #fp = open('/tmp/jabber-bot.pid', "w") #fp.write("%d\n" % os.getpid()) #fp.close() bot = JabberNotificationBot(JABBER_ID,JABBER_PWD) bot.serve_forever()
def handle(self, *args, **options): """Handle the management command.""" daemonize = options.get('daemonize') pidfile = options.get('pidfile') stop = options.get('stop') # stop existing celeryd stopped = stop_celery_process(pidfile) if stop: if not stopped: print "No existing celeryd process" quit() print "Starting celeryd (%s)" % getpid() # safely turn this process into a daemon if daemonize: become_daemon() # write the pidfile if pidfile: with open(pidfile,'w+') as f: print "writing pidfile:",pidfile f.write(str(getpid())) run_worker(**options)
def handle(self, *app_labels, **options): self.verbosity = int(options['verbosity']) jobfile = os.path.normpath(os.path.join(settings.PROJECT_ROOT, '!job')) if not os.path.isfile(jobfile): f = open(jobfile, 'w+') else: if self.verbosity>=1: print "Video convertor already runing" return is_daemonize=options.get('daemonize', "False")=="True" if is_daemonize: if self.verbosity>=1: print "Daemonize this process" become_daemon() else: if self.verbosity>=1: print "\nStart video_converto in console mode, use --daemonize=True for Daemonize this process =)" print "Timeout per step is '%s' sec." % TIMEOUT print "Press Ctrl+C for exit\n\n" try: self.go() finally: os.unlink(jobfile)
def run_cherrypy_server(host="127.0.0.1", port=8008, threads=50, daemonize=False, pidfile=None, autoreload=False): if daemonize: if not pidfile: pidfile = '~/cpwsgi_%d.pid' % port stop_server(pidfile) from django.utils.daemonize import become_daemon become_daemon() fp = open(pidfile, 'w') fp.write("%d\n" % os.getpid()) fp.close() cherrypy.config.update({ 'server.socket_host': host, 'server.socket_port': int(port), 'server.thread_pool': int(threads), 'checker.on': False, }) DjangoAppPlugin(cherrypy.engine).subscribe() if not autoreload: # cherrypyserver automatically reloads if any modules change # Switch-off that functionality here to save cpu cycles # http://docs.cherrypy.org/stable/appendix/faq.html cherrypy.engine.autoreload.unsubscribe() cherrypy.quickstart() if pidfile: stop_server(pidfile)
def run_cherrypy_server(host="127.0.0.1", port=8008, threads=50, daemonize=False, pidfile=None): if daemonize: if not pidfile: pidfile = '~/cpwsgi_%d.pid' % port stop_server(pidfile) from django.utils.daemonize import become_daemon become_daemon() fp = open(pidfile, 'w') fp.write("%d\n" % os.getpid()) fp.close() cherrypy.config.update({ 'server.socket_host': host, 'server.socket_port': int(port), 'server.thread_pool': int(threads), 'checker.on': False, }) DjangoAppPlugin(cherrypy.engine).subscribe() cherrypy.quickstart() if pidfile: stop_server(pidfile)
def handle(self, *args, **options): from os.path import abspath, dirname, isdir from gevent_fastcgi.server import WSGIServer from django.core.handlers.wsgi import WSGIHandler if not args: raise CommandError('bind address is not specified') if len(args) > 1: raise CommandError('unexpected arguments: %s', ' '.join(args[1:])) try: host, port = args[0].split(':', 1) except ValueError: address = abspath(args[0]) if not isdir(dirname(address)): raise CommandError('directory %s does not exist', dirname(address)) else: try: address = (host, int(port)) except ValueError: raise CommandError('port must be an integer value') if options['daemonize']: from django.utils.daemonize import become_daemon daemon_opts = dict((key, value) for key, value in options.items() if key in ('our_home_dir', 'out_log', 'err_log', 'umask')) become_daemon(**daemon_opts) app = WSGIHandler() server = WSGIServer(address, app, max_conns=options['max_conns'], max_reqs=options['max_reqs']) server.serve_forever()
def handle(self, *args, **options): from os.path import dirname, isdir from gevent_fastcgi.server import FastCGIServer from gevent_fastcgi.wsgi import WSGIRequestHandler from django.core.handlers.wsgi import WSGIHandler 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) 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() if options['daemonize']: from django.utils.daemonize import become_daemon daemon_opts = dict( (key, value) for key, value in options.items() if key in ( 'our_home_dir', 'out_log', 'err_log', 'umask')) become_daemon(**daemon_opts) kwargs = dict(( (name, value) for name, value in options.iteritems() if name in ( 'num_workers', 'max_conns', 'buffer_size', 'socket_mode'))) app = WSGIHandler() request_handler = WSGIRequestHandler(app) server = FastCGIServer(bind_address, request_handler, **kwargs) server.serve_forever()
def handle(self, *args, **options): self._setup_logger(options["logfile"], options["loglevel"]) replay_failed = options["replay_failed"] use_reloader = options["use_reloader"] self.pidfile = options["pidfile"] self.workers = options["workers"] if self.workers: self.workers = int(self.workers) else: # avoid a value of 0 self.workers = None self.stop_check_interval = 1 stop_requested = options["stop_requested"] if stop_requested: self._request_stop() return signal.signal(signal.SIGTERM, signal_handler) self._handle_pidfile() self.daemonize = options["daemonize"] if self.daemonize: become_daemon() # close the db connection before spawning the pool workers so each gets a new one db.close_connection() global pool pool = multiprocessing.Pool(self.workers) if use_reloader: autoreload.main(lambda: self._handle(use_reloader, replay_failed)) else: self._handle(use_reloader, replay_failed)
def handle(self, qname=None, start_stop='start', *args, **options): if args or start_stop not in ('start', 'stop', 'purge') or not qname: raise CommandError('Usage is %s' % self.args) mod, qname = qname.split('.') mod = '%s.amqp.queues' % mod queues = __import__(mod, globals(), locals(), ['']) qclass = getattr(queues, qname) try: if start_stop == 'start': print "%s started" % qname daemonize = options.get('daemonize', False) daemonize = daemonize in (True, 1, 'TRUE', 'True', 'true', 'y', 'Y', 'yes', 'Yes', 'YES') if daemonize: from django.utils.daemonize import become_daemon workdir = options.get('workdir', '.') become_daemon(our_home_dir=workdir) if options.get("pidfile", False): fp = open(options["pidfile"], "w") fp.write("%d\n" % os.getpid()) fp.close() q = qclass().listen() elif start_stop == 'purge': q = qclass(bind_queue=False).purge() time.sleep(1) # give the message a chance to transmit print "Queue %s purged" % qname else: q = qclass(bind_queue=False).stop() time.sleep(1) # give the message a chance to transmit print "%s stop message sent" % qname except (KeyboardInterrupt, SystemExit): pass except Exception, e: print >> sys.stderr, "ERROR: %s" % e _x.exception(e)
def daemonize(self): sys.stdout.flush() sys.stderr.flush() become_daemon() atexit.register(self._del_pid) self._set_pid()
def run(self): try: time.sleep(1) # wait output become_daemon(our_home_dir=settings.PROJECT_ROOT) ret = commands.getoutput(self.restart_file) write_log("command return '%s'" % ret) except Exception, e: write_log("Exception: %s" % e)
def execute(self, daemon_args, daemon_options, command, command_args, command_options): out_log = daemon_options.pop('logfile') pid_file = daemon_options.pop('pidfile') daemonize.become_daemon(out_log=out_log, err_log=out_log) self.check_pid(pid_file) self.write_pid_to_pid_file(pid_file) signal.signal(signal.SIGTERM, lambda: self.remove_pid_file(pid_file)) command_instance = self.get_command(command) command_instance.execute(*command_args, **command_options) self.remove_pid_file(pid_file)
def handle(self, *queue_names, **options): self.validate() if not queue_names: queue_names = django_sqs.queues.keys() if options.get("daemonize", False): from django.utils.daemonize import become_daemon become_daemon( out_log=options.get("stdout_log", "/dev/null"), err_log=options.get("stderr_log", "/dev/null") ) if options.get("pid_file", ""): f = open(options["pid_file"], "w") try: f.write("%d\n" % os.getpid()) finally: f.close() if len(queue_names) == 1: self.receive(queue_names[0]) else: # fork a group of processes. Quick hack, to be replaced # ASAP with something decent. _log = logging.getLogger("django_sqs.runreceiver.master") _log.addHandler(_NullHandler()) # Close the DB connection now and let Django reopen it when it # is needed again. The goal is to make sure that every # process gets its own connection from django.db import connection connection.close() os.setpgrp() children = {} # queue name -> pid for queue_name in queue_names: pid = self.fork_child(queue_name) children[pid] = queue_name _log.info("Forked %s for %s" % (pid, queue_name)) while children: pid, status = os.wait() queue_name = children[pid] _log.error("Child %d (%s) exited: %s" % (pid, children[pid], _status_string(status))) del children[pid] pid = self.fork_child(queue_name) children[pid] = queue_name _log.info("Respawned %s for %s" % (pid, queue_name))
def runwsgiserver(self, argset=[], **kwargs): # Get the options options = CPWSGI_OPTIONS.copy() options.update(kwargs) for x in argset: if "=" in x: k, v = x.split("=", 1) else: k, v = x, True if v == "False" or v == "false": v = False # print "found false", v options[k.lower()] = v self.options = options if "help" in options: print CPWSGI_HELP return if "stop" in options: stop_server(options["pidfile"]) return True if options["daemonize"]: options["autoreload"] = False # daemonize and autoreaload are not compatible currently if not options["pidfile"]: options["pidfile"] = "/var/run/django_wsgi_%s.pid" % options["port"] stop_server(options["pidfile"]) from django.utils.daemonize import become_daemon if options["workdir"]: become_daemon(our_home_dir=options["workdir"]) else: become_daemon() fp = open(options["pidfile"], "w") fp.write("%d\n" % os.getpid()) fp.close() # autoreload if options["autoreload"]: import django.utils.autoreload django.utils.autoreload.main(self.start_server_servestatic, (options,)) else: # Start the webserver logging.info("starting server with options:\n%s" % pformat(options)) self.start_server_servestatic(options)
def runcpserver(argset=[], **kwargs): from django.conf import settings # Get the options options = CPSERVER_OPTIONS.copy() options.update(kwargs) for x in argset: if "=" in x: k, v = x.split('=', 1) else: k, v = x, True options[k.lower()] = v if "help" in options: print CPSERVER_HELP return if "stop" in options: stop_server(options['pidfile']) return True if options['daemonize']: if not options['pidfile']: options['pidfile'] = '/var/run/cpserver_%s.pid' % options['port'] stop_server(options['pidfile']) from django.utils.daemonize import become_daemon if options['workdir']: become_daemon(our_home_dir=options['workdir']) else: become_daemon() fp = open(options['pidfile'], 'w') fp.write("%d\n" % os.getpid()) fp.close() # Load up static files if exists: if not options['static']: # No option was passed for static files # Is it in settings.py? try: options['static'] = settings.STATIC_ROOT except AttributeError: # No static found in settings.STATIC_ROOT pass # Start the webserver print 'starting server with options %s' % options start_server(options)
def daemonize(self): sys.stdout.flush() sys.stderr.flush() # TODO: give feedback to the user, on whether the daemon was started successfully, # i.e. verify that the daemon was started correctly. # This needs to be done in the *parent* process: # it probably requires patching become_daemon, since we would have # to add some processing *before* the sys.exit() for the parent. # This processing (a callback function ?) would likely wait for a few seconds while checking for the creation of the pidfile, # then wait another few seconds that the pid actually continues existing: if it doesn't continue existing, # it would simply print an error. become_daemon() atexit.register(self._delpid) self._setpid()
def handle(self, verbose=False, make_daemon=False, timeout=10, rebuild_index=False, per_page=1000, commit_each=False, *args, **options): utils.load_indexes() if make_daemon: become_daemon() if rebuild_index: rebuild(verbose, per_page, commit_each) else: update_changes(verbose, timeout, not make_daemon, per_page, commit_each) if verbose: print '\n'
def start_server (options): if options['daemonize']: if options['server_user'] and options['server_group']: change_uid_gid(options['server_user'], options['server_group']) if options['workdir']: become_daemon(our_home_dir=options['workdir']) else: become_daemon() fp = open(options['pidfile'], 'w') fp.write("%d\n" % os.getpid()) fp.close() d = WSGIPathInfoDispatcher({'/static': static, '/': WSGIHandler()}) SERVER = Server( (options['host'], int(options['port'])), d, numthreads=int(options['threads']), max=int(options['threads']), server_name=options['server_name'], shutdown_timeout = int(options['shutdown_timeout']), request_queue_size = int(options['request_queue_size']) ) if options['ssl_certificate'].lower() == 'none' or options['ssl_private_key'].lower() == 'none': pass else: if not os.path.exists(options['ssl_certificate']) or not os.path.exists(options['ssl_private_key']): if options['ssl_certificate'] == settings.SERVER_CERT and options['ssl_private_key'] == settings.SERVER_KEY: generate_cert() else: raise Exception('Invalid Certificate or Key Path') SERVER.ssl_certificate = options['ssl_certificate'] SERVER.ssl_private_key = options['ssl_private_key'] try: SERVER.start() except KeyboardInterrupt: SERVER.stop()
def runwsgiserver(argset=[], **kwargs): # Get the options options = CPWSGI_OPTIONS.copy() options.update(kwargs) for x in argset: if "=" in x: k, v = x.split('=', 1) else: k, v = x, True if v=='False' or v=='false': v = False # print "found false", v options[k.lower()] = v if "help" in options: print CPWSGI_HELP return if "stop" in options: stop_server(options['pidfile']) return True if options['daemonize']: if not options['pidfile']: options['pidfile'] = '/var/run/cpwsgi_%s.pid' % options['port'] stop_server(options['pidfile']) from django.utils.daemonize import become_daemon if options['workdir']: become_daemon(our_home_dir=options['workdir']) else: become_daemon() fp = open(options['pidfile'], 'w') fp.write("%d\n" % os.getpid()) fp.close() if options['autoreload']: from django.utils import autoreload # Start the webserver if options['autoreload']: autoreload.main(start_server_servestatic, (), options) else: start_server_servestatic(options)
def runcpserver(argset=[], **kwargs): # Get the options options = CPSERVER_OPTIONS.copy() options.update(kwargs) for x in argset: if "=" in x: k, v = x.split('=', 1) else: k, v = x, True options[k.lower()] = v if "help" in options: print CPSERVER_HELP return if "stop" in options: stop_server(options['pidfile']) return True if options['daemonize']: if not options['pidfile']: options['pidfile'] = '/var/run/cpserver_%s.pid' % options['port'] stop_server(options['pidfile']) from django.utils.daemonize import become_daemon if options['workdir']: become_daemon(our_home_dir=options['workdir']) else: become_daemon() fp = open(options['pidfile'], 'w') fp.write("%d\n" % os.getpid()) fp.close() # Set the logging level lev = LEVELS[int(options['verbosity'])] logger.setLevel(lev) logger.debug('Turned logging on') # Start the webserver print 'starting server with options %s' % options #webbrowser.open('http://localhost:9000/') start_server(options)
def runcherrypy(argset=[], options={}, **kwargs): for x in argset: if "=" in x: k, v = x.split('=', 1) else: k, v = x, True options[k.lower()] = v if "help" in options: print CPSERVER_HELP return if "stop" in options: stop_server(options['pidfile']) return True if options['daemonize']: if not options['pidfile']: options['pidfile'] = '/var/run/cpserver_%s.pid' % options['port'] stop_server(options['pidfile']) from django.utils.daemonize import become_daemon if options['workdir']: become_daemon(our_home_dir=options['workdir']) else: become_daemon() fp = open(options['pidfile'], 'w') fp.write("%d\n" % os.getpid()) fp.close() # handle admin media import django from django.conf import settings from django.utils import translation translation.activate(settings.LANGUAGE_CODE) quit_command = (sys.platform == 'win32') and 'CTRL-BREAK' or 'CONTROL-C' if options['autoreload']: from django.utils import autoreload autoreload.main(start_server, kwargs={'options' : options, 'settings' : settings}) start_server(options, settings)
def run_cherrypy_server(host="127.0.0.1", port=None, threads=None, daemonize=False, pidfile=None, autoreload=False, startuplock=None): port = port or getattr(settings, "PRODUCTION_PORT", 8008) threads = threads or getattr(settings, "CHERRYPY_THREAD_COUNT", 18) if daemonize: if not pidfile: pidfile = '~/cpwsgi_%d.pid' % port # benjaoming: stopping the server is an explicit logic that has already # been implemented other places. Killing some process related to a # possibly out-dated pidfile is not exactly best practice # stop_server(pidfile) from django.utils.daemonize import become_daemon kalite_home = os.environ.get("KALITE_HOME", None) logfile = os.path.join(kalite_home, "kalite.log") if (kalite_home and os.environ.get("NAIVE_LOGGING", False)) else None if logfile: become_daemon(out_log=logfile, err_log=logfile) else: become_daemon() with open(pidfile, 'w') as f: f.write("%d\n" % os.getpid()) f.write(port) cherrypy.config.update({ 'server.socket_host': host, 'server.socket_port': int(port), 'server.thread_pool': int(threads), 'checker.on': False, }) DjangoAppPlugin(cherrypy.engine).subscribe() if not autoreload: # cherrypyserver automatically reloads if any modules change # Switch-off that functionality here to save cpu cycles # http://docs.cherrypy.org/stable/appendix/faq.html cherrypy.engine.autoreload.unsubscribe() cherrypy.quickstart() if pidfile: stop_server(pidfile)
def become_daemon(*args, **kwargs): """become_daemon function wrapper In Django 1.9, 'become_daemon' is removed. It means compatibility. """ if django.VERSION >= (1, 9): from .daemonize import become_daemon else: from django.utils.daemonize import become_daemon return become_daemon(*args, **kwargs)
def handle(self, *args, **options): logger.info("Running task manager command") GlobalConfig.initialize() start = options['start'] and True or False stop = options['stop'] and True or False pid = None try: pid = int(file(getPidFile(), 'r').readline()) except Exception: pid = None if stop is True and pid is not None: try: logger.info('Stopping task manager. pid: {0}'.format(pid)) os.kill(pid, signal.SIGTERM) time.sleep(1) # Wait a bit before running new one os.unlink(getPidFile()) except Exception: logger.error("Could not stop task manager (maybe it's not runing?)") os.unlink(getPidFile()) if start is True: logger.info('Starting task manager.') become_daemon(settings.BASE_DIR, settings.LOGDIR + '/taskManagerStdout.log', settings.LOGDIR + '/taskManagerStderr.log') pid = six.text_type(os.getpid()) open(getPidFile(), 'w+').write("%s\n" % pid) manager = TaskManager() manager.run() if start is False and stop is False: if pid is not None: sys.stdout.write("Task manager found running (pid file exists: {0})\n".format(pid)) else: sys.stdout.write("Task manager not foud (pid file do not exits)\n")
def runclient(argset=[], **kwargs): # Get the options options = CLIENT_OPTIONS.copy() options.update(kwargs) from net.aircable.utils import getLogger logger = getLogger(__name__) if options['daemonize']: if not options['pidfile']: options['pidfile'] = '/var/run/openproximity_client_%s_%s.pid' % ( \ options['mode'], options['port'] ) logger.info("storing pid file in %s" % options['pidfile']) from django.utils.daemonize import become_daemon logger.info("daemonizing") become_daemon() logger.info("daemonized") fp = open(options['pidfile'], 'w') fp.write("%d\n" % os.getpid()) fp.close() # Start the rpc server logger.debug('starting client with options %s' % options) mode = options['mode'] host = options['host'] port = int(options['port']) valid_modes=['scanner', 'uploader', 'pairing'] from net.aircable.openproximity.pluginsystem import pluginsystem valid_modes.extend([ \ i.provides['serverxr_type'] for i in \ pluginsystem.get_plugins('serverxr')]) if mode not in valid_modes: print "not a valid mode, use any of", valid_modes sys.exit(1) from rpc_clients import manager manager.run(host, port, mode)
def start(debug=False, daemonize=True, args=[], skip_job_scheduler=False, port=None): """ Start the kalite server as a daemon :param args: List of options to parse to the django management command :param port: Non-default port to bind to. You cannot run kalite on multiple ports at the same time. :param daemonize: Default True, will run in foreground if False :param skip_job_scheduler: Skips running the job scheduler in a separate thread """ # TODO: Do we want to fail if running as root? port = int(port or DEFAULT_LISTEN_PORT) if not daemonize: sys.stderr.write("Running 'kalite start' in foreground...\n") else: sys.stderr.write("Running 'kalite start' as daemon (system service)\n") sys.stderr.write("\nStand by while the server loads its data...\n\n") if os.path.exists(STARTUP_LOCK): try: pid, __ = read_pid_file(STARTUP_LOCK) # Does the PID in there still exist? if pid_exists(pid): sys.stderr.write( "Refusing to start: Start up lock exists: {0:s}\n".format( STARTUP_LOCK)) sys.stderr.write("Remove the file and try again.\n") sys.exit(1) # Couldn't parse to int except TypeError: pass os.unlink(STARTUP_LOCK) try: if get_pid(): sys.stderr.write("Refusing to start: Already running\n") sys.stderr.write("Use 'kalite stop' to stop the instance.\n") sys.exit(1) except NotRunning: pass # Check that the port is available by creating a simple socket and see # if it succeeds... if it does, the port is occupied. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) connection_error = sock.connect_ex(('127.0.0.1', port)) if not connection_error: sys.stderr.write( "Port {0} is occupied. Please close the process that is using " "it.\n".format(port)) sys.exit(1) # Write current PID and optional port to a startup lock file with open(STARTUP_LOCK, "w") as f: f.write("%s\n%d" % (str(os.getpid()), port)) # Remove the startup lock at this point if STARTUP_LOCK: os.unlink(STARTUP_LOCK) # Daemonize at this point, no more user output is needed if daemonize: from django.utils.daemonize import become_daemon kwargs = {} # Truncate the file open(SERVER_LOG, "w").truncate() print("Going to daemon mode, logging to {0}".format(SERVER_LOG)) kwargs['out_log'] = SERVER_LOG kwargs['err_log'] = SERVER_LOG become_daemon(**kwargs) # Write the new PID with open(PID_FILE, 'w') as f: f.write("%d\n%d" % (os.getpid(), port)) manage('initialize_kalite') # Print output to user about where to find the server addresses = get_ip_addresses(include_loopback=False) sys.stdout.write( "To access KA Lite from another connected computer, try the following address(es):\n" ) for addr in addresses: sys.stdout.write("\thttp://%s:%s/\n" % (addr, port)) sys.stdout.write( "To access KA Lite from this machine, try the following address:\n") sys.stdout.write("\thttp://127.0.0.1:%s/\n" % port) for addr in get_urls_proxy(output_pipe=sys.stdout): sys.stdout.write("\t{}\n".format(addr)) sys.stdout.write("\n") # Start the job scheduler (not Celery yet...) cron_thread = None if not skip_job_scheduler: cron_thread = manage('cronserver_blocking', args=[], as_thread=True) # Start cherrypy service cherrypy.config.update({ 'server.socket_host': LISTEN_ADDRESS, 'server.socket_port': port, 'server.thread_pool': 18, 'checker.on': False, }) DjangoAppPlugin(cherrypy.engine).subscribe() # cherrypyserver automatically reloads if any modules change # Switch-off that functionality here to save cpu cycles # http://docs.cherrypy.org/stable/appendix/faq.html cherrypy.engine.autoreload.unsubscribe() try: cherrypy.quickstart() except KeyboardInterrupt: # Handled in cherrypy by waiting for all threads to join pass except SystemExit: print("KA Lite caught system exit signal, quitting.") print("FINISHED serving HTTP") if cron_thread: # Do not exit thread together with the main process, let it finish # cleanly print("Asking KA Lite job scheduler to terminate...") from fle_utils.chronograph.management.commands import cronserver_blocking cronserver_blocking.shutdown = True cron_thread.join() print("Job scheduler terminated.")
def main(): """ Main. """ signal.signal(signal.SIGTERM, on_stop_signal) signal.signal(signal.SIGINT, on_stop_signal) signal.signal(signal.SIGHUP, on_restart_signal) parser = optparse.OptionParser() parser.add_option('--daemonize', action='store_true', default=False) parser.add_option('--workdir') parser.add_option('--logfile') parser.add_option('--umask', type='int') parser.add_option('--pidfile') parser.add_option('--frequency', type='int') parser.add_option('--config', dest='config_file', default='/vol/offlineimap/offlineimap.conf') parser.add_option('--quiet', dest='verbosity', action='store_const', const='Quiet', default='Basic') options, _extras = parser.parse_args() if options.daemonize: from django.utils.daemonize import become_daemon daemon_kwargs = {} if options.workdir: daemon_kwargs['our_home_dir'] = options.workdir if options.logfile: daemon_kwargs['out_log'] = options.logfile daemon_kwargs['err_log'] = options.logfile if options.umask: daemon_kwargs['umask'] = options.umask become_daemon(**daemon_kwargs) if options.pidfile: with open(options.pidfile, 'wb') as pidfile: pidfile.write('%d\n' % os.getpid()) options.config_file = os.path.abspath(options.config_file) logging.info('Starting offlineimapd') while not must_shut_down(): try: os.system("'%s' '%s'" % (os.path.abspath( os.path.join(os.path.dirname(__file__), 'gen-offlineimap-conf')), options.config_file)) params = [ 'offlineimap', # will show up as the process name '-c', "from offlineimap import init; init.startup(\'6.2.0\')", '-c', options.config_file, ] child = subprocess.Popen(params, executable='/usr/bin/python') while not (must_shut_down() or must_restart()): if child.poll() is not None: break time.sleep(1) terminate(child) except Exception: logging.exception('An exception occurred!') if options.pidfile: os.remove(options.pidfile) logging.info("Stopped")
if options["daemonize"].lower() in ('true', 'yes', 't'): daemonize = True elif options["daemonize"].lower() in ('false', 'no', 'f'): daemonize = False else: return fastcgi_help( "ERROR: Invalid option for daemonize parameter.") daemon_kwargs = {} if options['outlog']: daemon_kwargs['out_log'] = options['outlog'] if options['errlog']: daemon_kwargs['err_log'] = options['errlog'] if options['umask']: daemon_kwargs['umask'] = int(options['umask'], 8) if daemonize: from django.utils.daemonize import become_daemon become_daemon(our_home_dir=options["workdir"], **daemon_kwargs) if options["pidfile"]: fp = open(options["pidfile"], "w") fp.write("%d\n" % os.getpid()) fp.close() WSGIServer(WSGIHandler(), **wsgi_opts).run() if __name__ == '__main__': runfastcgi(sys.argv[1:])