Beispiel #1
0
def start(options, parser):

    # Perform common option checks
    commonOptionsCheck(options, parser)

    # Setup the loggers
    fileHandler = setupLogging(options)

    # Start daemon?
    if options.daemon:

        # Make sure the PID file will be created without problems
        pidDir = getDfmsPidDir()
        createDirIfMissing(pidDir)
        pidfile = os.path.join(pidDir, "dfms%s.pid" % (options.dmAcronym))

        with daemon.DaemonContext(pidfile=PIDLockFile(pidfile, 1),
                                  files_preserve=[fileHandler.stream]):
            launchServer(options)

    # Stop daemon?
    elif options.stop:
        pidDir = getDfmsPidDir()
        pidfile = os.path.join(pidDir, "dfms%s.pid" % (options.dmAcronym))
        pid = PIDLockFile(pidfile).read_pid()
        if pid is None:
            sys.stderr.write(
                'Cannot read PID file, is there an instance running?\n')
        else:
            os.kill(pid, signal.SIGTERM)

    # Start directly
    else:
        launchServer(options)
Beispiel #2
0
def start(options, parser):

    # Perform common option checks
    commonOptionsCheck(options, parser)

    # Setup the loggers
    fileHandler = setupLogging(options)

    # Start daemon?
    if options.daemon:

        # Make sure the PID file will be created without problems
        pidDir = utils.getDlgPidDir()
        utils.createDirIfMissing(pidDir)
        pidfile = os.path.join(pidDir, "dlg%s.pid" % (options.dmAcronym))

        working_dir = options.work_dir
        if not working_dir:
            if options.cwd:
                print(
                    'The --cwd option is deprecated, prefer -w/--work-dir, continuing anyway'
                )
                working_dir = '.'
            else:
                working_dir = '/'
        with daemon.DaemonContext(pidfile=PIDLockFile(pidfile, 1),
                                  files_preserve=[fileHandler.stream],
                                  working_directory=working_dir):
            launchServer(options)

    # Stop daemon?
    elif options.stop:
        pidDir = utils.getDlgPidDir()
        pidfile = os.path.join(pidDir, "dlg%s.pid" % (options.dmAcronym))
        pid = PIDLockFile(pidfile).read_pid()
        if pid is None:
            sys.stderr.write(
                'Cannot read PID file, is there an instance running?\n')
        else:
            try:
                os.kill(pid, signal.SIGTERM)
            except OSError as e:
                # Process is gone and file was left dangling,
                # let's clean it up ourselves
                if e.errno == errno.ESRCH:
                    sys.stderr.write(
                        'Process %d does not exist, removing PID file')
                    os.unlink(pidfile)

    # Check status
    elif options.status:
        socket_is_listening = utils.check_port(options.host, options.port,
                                               options.timeout)
        sys.exit(socket_is_listening is False)

    # Start directly
    else:
        working_dir = options.work_dir or '.'
        os.chdir(working_dir)
        launchServer(options)
Beispiel #3
0
def start_daemon():
    # 전역변수로 처리 필요
    config = configparser.ConfigParser()
    config.read('./config.cfg')
    cfg_server = config['SERVER_INFO']
    cfg_default = config['DEFAULT_INFO']
    # make logger instance
    logger = logging.getLogger("DA_daemonLog")

    # make formatter
    formatter = logging.Formatter(
        '[%(levelname)s|%(filename)s:%(lineno)s] %(asctime)s > %(message)s')

    # make handler to output Log for stream and file
    fileMaxByte = 1024 * 1024 * 100  #100MB
    fileHandler = logging.handlers.RotatingFileHandler(
        cfg_default['test_path'], maxBytes=fileMaxByte, backupCount=10)

    # fileHandler = logging.FileHandler(cfg_default['logging_path'])
    streamHandler = logging.StreamHandler()

    # specify formatter to each handler
    fileHandler.setFormatter(formatter)
    streamHandler.setFormatter(formatter)

    # attach stream and file handler to logger instance
    logger.addHandler(fileHandler)
    logger.addHandler(streamHandler)

    pidfile = PIDLockFile(cfg_default['test_pid_path'])
    try:
        pidfile.acquire()
    except AlreadyLocked:
        try:
            os.kill(pidfile.read_pid(), 0)
            print('Process already running!')
            exit(1)
        except OSError:  #No process with locked PID
            pidfile.break_lock()

    daemon_context = daemon.DaemonContext(
        working_directory='/home/Toven/da/elda',
        umask=0o002,
        pidfile=PIDLockFile('/home/Toven/da/elda_daemon.pid'),
    )

    print("Start daemon for EyeLink in python")

    with daemon_context:
        while True:
            logger.setLevel(logging.INFO)
            logger.info("==========================")
            logger.debug("Debug message")
            logger.info("Info message")
            logger.warn("Warning message")
            logger.error("Error message")
            logger.critical("critical debug message")
            logger.info("==========================")
Beispiel #4
0
def get_maestral_pid(config_name):
    """
    Returns Maestral's PID if the daemon is running and responsive, ``None``
    otherwise. If the daemon is unresponsive, it will be killed before returning.

    :param str config_name: The name of the Maestral configuration to use.
    :returns: The daemon's PID.
    :rtype: int
    """

    lockfile = PIDLockFile(pidpath_for_config(config_name))
    pid = lockfile.read_pid()

    if pid:
        try:
            if not is_pidfile_stale(lockfile):
                return pid
        except OSError:
            os.kill(pid, signal.SIGKILL)
            logger.debug(
                f"Daemon process with PID {pid} is not responsive. Killed.")
    else:
        logger.debug("Could not find PID file")

    lockfile.break_lock()
Beispiel #5
0
def check_if_pidfile_process_is_running(pid_file: str, process_name: str):
    """
    Checks if a pidfile already exists and process is still running.
    If process is dead then pidfile is removed.

    :param pid_file: path to the pidfile
    :param process_name: name used in exception if process is up and
        running
    """
    pid_lock_file = PIDLockFile(path=pid_file)
    # If file exists
    if pid_lock_file.is_locked():
        # Read the pid
        pid = pid_lock_file.read_pid()
        if pid is None:
            return
        try:
            # Check if process is still running
            proc = psutil.Process(pid)
            if proc.is_running():
                raise AirflowException(
                    f"The {process_name} is already running under PID {pid}.")
        except psutil.NoSuchProcess:
            # If process is dead remove the pidfile
            pid_lock_file.break_lock()
Beispiel #6
0
    def main(argv):
        nablogging.setup_logging("nabd")
        pidfilepath = "/run/nabd.pid"
        hardware_platform = hardware.device_model()
        matchObj = re.match(r"Raspberry Pi Zero", hardware_platform)
        if matchObj:
            # running on Pi Zero or Zero 2 hardware
            from .nabio_hw import NabIOHW

            nabiocls = NabIOHW
        else:
            # other hardware: go virtual
            from .nabio_virtual import NabIOVirtual

            nabiocls = NabIOVirtual
        usage = (
            f"nabd [options]\n"
            f" -h                  display this message\n"
            f" --pidfile=<pidfile> define pidfile (default = {pidfilepath})\n"
            " --nabio=<nabio> define nabio class "
            f"(default = {nabiocls.__module__}.{nabiocls.__name__})\n")
        try:
            opts, args = getopt.getopt(argv, "h", ["pidfile=", "nabio="])
        except getopt.GetoptError:
            print(usage)
            exit(2)
        for opt, arg in opts:
            if opt == "-h":
                print(usage)
                exit(0)
            elif opt == "--pidfile":
                pidfilepath = arg
            elif opt == "--nabio":
                from pydoc import locate

                nabiocls = locate(arg)
        pidfile = PIDLockFile(pidfilepath, timeout=-1)
        try:
            with pidfile:
                nabio = nabiocls()
                Nabd.leds_boot(nabio, 1)
                nabd = Nabd(nabio)
                logging.info(f"running on {hardware_platform}")
                nabd.run()
        except AlreadyLocked:
            error_msg = f"nabd already running? (pid={pidfile.read_pid()})"
            print(error_msg)
            logging.critical(error_msg)
            exit(1)
        except LockFailed:
            error_msg = (f"Cannot write pid file to {pidfilepath}, please fix "
                         f"permissions")
            print(error_msg)
            logging.critical(error_msg)
            exit(1)
        except Exception:
            error_msg = f"Unhandled error: {traceback.format_exc()}"
            print(error_msg)
            logging.critical(error_msg)
            exit(3)
Beispiel #7
0
def pull():
    """ Pull down tasks from forges and add them to your taskwarrior tasks.

    Relies on configuration in ~/.bugwarriorrc
    """
    twiggy.quickSetup()
    try:
        # Load our config file
        config = load_config()

        tw_config = TaskWarriorBase.load_config(get_taskrc_path(config))
        lockfile_path = os.path.join(
            os.path.expanduser(tw_config['data']['location']),
            'bugwarrior.lockfile')

        lockfile = PIDLockFile(lockfile_path)
        lockfile.acquire(timeout=10)
        try:
            # Get all the issues.  This can take a while.
            issue_generator = aggregate_issues(config)

            # Stuff them in the taskwarrior db as necessary
            synchronize(issue_generator, config)
        finally:
            lockfile.release()
    except LockTimeout:
        log.name('command').critical(
            'Your taskrc repository is currently locked. '
            'Remove the file at %s if you are sure no other '
            'bugwarrior processes are currently running.' % (lockfile_path))
    except:
        log.name('command').trace('error').critical('oh noes')
Beispiel #8
0
def main (interrupt=None):
  args = parser.parse_args()
  config = ConfigParser()
  dirname = os.path.dirname(os.path.realpath(sys.argv[0]))
  if args.config is None:
    configfile = None
    for afile in [ os.path.expanduser('~/.relaykeys.cfg'),
                    os.path.join(dirname, 'relaykeys.cfg') ]:
      if len(config.read([afile])) > 0:
        configfile = afile
        break
  else:
    if len(config.read([args.config])) == 0:
      raise ValueError("Could not read config file: {}".format(args.config))
    configfile = args.config
  if "server" not in config.sections():
    config["server"] = {}
  serverconfig = config["server"]
  if serverconfig.getboolean("rewritepasswordonce", False):
    serverconfig["password"] = mkpasswd(24, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
    del serverconfig["rewritepasswordonce"]
    if "client" in config.sections():
      config["client"]["password"] = serverconfig["password"]
    with open(configfile, "w") as f:
      config.write(f)
  isdaemon = args.daemon or serverconfig.getboolean("daemon", False) if os.name =='posix' else False
  if isdaemon:
    pidfile = os.path.realpath(args.pidfile or serverconfig.get("pidfile", None))
    with DaemonContext(working_directory=os.getcwd(),
                       pidfile=PIDLockFile(pidfile)):
      init_logger(dirname, True, args, serverconfig)
      return do_main(args, serverconfig, interrupt)
  else:
    init_logger(dirname, False, args, serverconfig)
    return do_main(args, serverconfig, interrupt)
Beispiel #9
0
def get_pid_lock_file(pidfile_path, logger, app_name):
    pidlock = PIDLockFile(pidfile_path)
    if pidlock.is_locked():
        old_pid = pidlock.read_pid()
        logger.info("Lock file exists for PID %d." % old_pid)
        if os.getpid() == old_pid:
            logger.info("Stale lock since we have the same PID.")
        else:
            try:
                old = psutil.Process(old_pid)
                if os.path.basename(__file__) in old.cmdline():
                    try:
                        logger.info("Trying to terminate old instance...")
                        old.terminate()
                        try:
                            old.wait(10)
                        except psutil.TimeoutExpired:
                            logger.info("Trying to kill old instance.")
                            old.kill()
                    except psutil.AccessDenied:
                        logger.error(
                            "The process seems to be %s, but "
                            "can not be stopped. Its command line: %s" %
                            app_name, old.cmdline())
                else:
                    logger.info(
                        "Process does not seem to be {}.".format(app_name))
            except psutil.NoSuchProcess:
                pass
                logger.info("No such process exist anymore.")
        logger.info("Breaking old lock.")
        pidlock.break_lock()
    return pidlock
Beispiel #10
0
def stop_maestral_daemon_thread(config_name='maestral', timeout=10):
    """Stops maestral's thread.

    This function tries to shut down Maestral gracefully. If it is not successful
    within the given timeout, a TimeoutError is raised.

    :param str config_name: The name of the Maestral configuration to use.
    :param float timeout: Number of sec to wait for daemon to shut down before killing it.
    :returns: ``Exit.Ok`` if successful,``Exit.NotRunning`` if the daemon was not running,
        ``Exit.Failed`` if it could not be stopped within  timeout.
    """

    logger.debug('Stopping thread')
    lockfile = PIDLockFile(pidpath_for_config(config_name))
    t = _threads[config_name]

    if not t.is_alive():
        lockfile.break_lock()
        return Exit.NotRunning

    # tell maestral daemon to shut down
    try:
        with MaestralProxy(config_name) as m:
            m.stop_sync()
            m.shutdown_pyro_daemon()
    except Pyro5.errors.CommunicationError:
        return Exit.Failed

    # wait for maestral to carry out shutdown
    t.join(timeout=timeout)
    if t.is_alive():
        return Exit.Failed
    else:
        return Exit.Ok
Beispiel #11
0
 def main(cls, argv):
     service_name = cls.__name__.lower()
     pidfilepath = '/var/run/{service_name}.pid'.format(
         service_name=service_name)
     usage = '{service_name} [options]\n'.format(service_name=service_name) \
      + ' -h                   display this message\n' \
      + ' --pidfile=<pidfile>  define pidfile (default = {pidfilepath})\n'.format(pidfilepath=pidfilepath)
     try:
         opts, args = getopt.getopt(argv, "h", ["pidfile="])
     except getopt.GetoptError:
         print(usage)
         exit(2)
     for opt, arg in opts:
         if opt == '-h':
             print(usage)
             exit(0)
         elif opt == '--pidfile':
             pidfilepath = arg
     pidfile = PIDLockFile(pidfilepath, timeout=-1)
     try:
         with pidfile:
             service = cls()
             service.run()
     except AlreadyLocked:
         print('{service_name} already running? (pid={pid})'.format(
             service_name=service_name, pid=pidfile.read_pid()))
         exit(1)
     except LockFailed:
         print(
             'Cannot write pid file to {pidfilepath}, please fix permissions'
             .format(pidfilepath=pidfilepath))
         exit(1)
Beispiel #12
0
def pull(dry_run, flavor, interactive, debug):
    """ Pull down tasks from forges and add them to your taskwarrior tasks.

    Relies on configuration in bugwarriorrc
    """

    try:
        main_section = _get_section_name(flavor)
        config = _try_load_config(main_section, interactive)

        lockfile_path = os.path.join(get_data_path(config, main_section),
                                     'bugwarrior.lockfile')
        lockfile = PIDLockFile(lockfile_path)
        lockfile.acquire(timeout=10)
        try:
            # Get all the issues.  This can take a while.
            issue_generator = aggregate_issues(config, main_section, debug)

            # Stuff them in the taskwarrior db as necessary
            synchronize(issue_generator, config, main_section, dry_run)
        finally:
            lockfile.release()
    except LockTimeout:
        log.critical('Your taskrc repository is currently locked. '
                     'Remove the file at %s if you are sure no other '
                     'bugwarrior processes are currently running.' %
                     (lockfile_path))
    except RuntimeError as e:
        log.exception("Aborted (%s)" % e)
Beispiel #13
0
def main():
    parser = argparse.ArgumentParser(
        description='A distributed cron-like daemon')
    parser.add_argument('-f', dest='foreground', action='store_const',
                        const=False, help='run in foreground')
    args = parser.parse_args()

    if os.geteuid() != 0:
        sys.exit('%s must be run as root.' % os.path.basename(sys.argv[0]))

    context = daemon.DaemonContext(
        pidfile=PIDLockFile('/var/run/megacron.pid'),
        detach_process=args.foreground,
    )
    context.signal_map = {
        signal.SIGINT: _signal_handler,
        signal.SIGTERM: _signal_handler
    }

    with context:
        worker.init_worker()

        events = sched.scheduler(time.time, time.sleep)

        scheduler.check_scheduler(events)

        worker.heartbeat(events)
        worker.update_schedules(events)

        events.run()
Beispiel #14
0
def run_with_lock(remove=False):
    lock = PIDLockFile(
        getattr(
            settings, "HYPERKITTY_JOBS_UPDATE_INDEX_LOCKFILE",
            os.path.join(gettempdir(), "hyperkitty-jobs-update-index.lock")))
    try:
        lock.acquire(timeout=-1)
    except AlreadyLocked:
        if check_pid(lock.read_pid()):
            logger.warning("The job 'update_index' is already running")
            return
        else:
            lock.break_lock()
            lock.acquire(timeout=-1)
    except LockFailed as e:
        logger.warning(
            "Could not obtain a lock for the 'update_index' "
            "job (%s)", e)
        return
    try:
        update_index(remove=remove)
    except Exception as e:
        logger.exception("Failed to update the fulltext index: %s", e)
    finally:
        lock.release()
Beispiel #15
0
    def main(argv):
        nablogging.setup_logging("nabd")
        pidfilepath = "/run/nabd.pid"
        usage = (
            f"nabd [options]\n"
            f" -h                  display this message\n"
            f" --pidfile=<pidfile> define pidfile (default = {pidfilepath})\n")
        try:
            opts, args = getopt.getopt(argv, "h", ["pidfile=", "nabio="])
        except getopt.GetoptError:
            print(usage)
            exit(2)
        for opt, arg in opts:
            if opt == "-h":
                print(usage)
                exit(0)
            elif opt == "--pidfile":
                pidfilepath = arg
        pidfile = PIDLockFile(pidfilepath, timeout=-1)
        try:
            with pidfile:
                from .nabio_hw import NabIOHW

                nabio = NabIOHW()
                Nabd.leds_boot(nabio, 1)
                nabd = Nabd(nabio)
                nabd.run()
        except AlreadyLocked:
            print(f"nabd already running? (pid={pidfile.read_pid()})")
            exit(1)
        except LockFailed:
            print(f"Cannot write pid file to {pidfilepath}, please fix "
                  f"permissions")
            exit(1)
Beispiel #16
0
 def start_proc(self, proc_name, proc_factory, chroot_dir=None,
                kill_old=False):
     if not proc_name.isalnum():
         raise Exception('Invalid name: Only Alpha-Numeric Characters Allowed.')
     procs = self._get_procs()
     pidfilename = os.path.join(
         self.lock_dir, f'procz_{proc_name}.pid')
     if proc_name in procs:
         if not kill_old:
             raise Exception('Process is already running!')
         self.kill_proc(proc_name)
         with TimedLoop(2) as l:
             l.run_til(lambda: os.path.isfile(pidfilename), lambda x: not x)
     pid = os.fork()
     if pid:
         return 'starting proc'
     else:
         try:
             with daemon.DaemonContext(pidfile=PIDLockFile(pidfilename),
                                       detach_process=True):
                 procd = proc_factory()
                 procd.run()
         except Exception as e:
             e_log = os.path.join(self.lock_dir, f'{proc_name}.error.log')
             with open(e_log, 'w') as f:
                 f.write(traceback.format_exc()+'\n')
             raise e
Beispiel #17
0
def run_maestral_daemon(config_name="maestral", run=True, log_to_stdout=False):
    """
    Wraps :class:`maestral.main.Maestral` as Pyro daemon object, creates a new instance
    and start Pyro's event loop to listen for requests on a unix domain socket. This call
    will block until the event loop shuts down.

    This command will return silently if the daemon is already running.

    :param str config_name: The name of the Maestral configuration to use.
    :param bool run: If ``True``, start syncing automatically. Defaults to ``True``.
    :param bool log_to_stdout: If ``True``, write logs to stdout. Defaults to ``False``.
    """

    from maestral.main import Maestral

    sock_name = sockpath_for_config(config_name)
    pid_name = pidpath_for_config(config_name)

    lockfile = PIDLockFile(pid_name)

    # acquire PID lock file

    try:
        lockfile.acquire(timeout=1)
    except AlreadyLocked:
        if is_pidfile_stale(lockfile):
            lockfile.break_lock()
        else:
            logger.debug(f"Maestral already running")
            return

    logger.debug(f"Starting Maestral daemon on socket '{sock_name}'")

    try:
        # clean up old socket, create new one
        try:
            os.remove(sock_name)
        except FileNotFoundError:
            pass

        daemon = Daemon(unixsocket=sock_name)

        # start Maestral as Pyro server
        ExposedMaestral = expose(Maestral)
        # mark stop_sync and shutdown_daemon as oneway methods
        # so that they don't block on call
        ExposedMaestral.stop_sync = oneway(ExposedMaestral.stop_sync)
        ExposedMaestral.shutdown_pyro_daemon = oneway(
            ExposedMaestral.shutdown_pyro_daemon)
        m = ExposedMaestral(config_name, run=run, log_to_stdout=log_to_stdout)

        daemon.register(m, f"maestral.{config_name}")
        daemon.requestLoop(loopCondition=m._loop_condition)
        daemon.close()
    except Exception:
        traceback.print_exc()
    finally:
        # remove PID lock
        lockfile.release()
Beispiel #18
0
def check_daemon_options(daemon_config):
    """Returns the pidfile object and non-default daemon settings;
    dies if there are any illegal settings."""
    check_for_illegal_daemon_options(daemon_config)
    daemon_options = {k: v for k, v in daemon_config.items() if v is not None}
    pidfile_path = daemon_options.pop('pidfile')
    pidfile = PIDLockFile(pidfile_path)
    return pidfile, daemon_options
Beispiel #19
0
def main():

    # using CaresResolver as DNS resolver
    # see also: http://www.tornadoweb.org/en/branch3.0/caresresolver.html
    Resolver.configure('tornado.platform.caresresolver.CaresResolver')
    # CurlAsyncHTTPClient to be used as httpclient subclass
    tornado.httpclient.AsyncHTTPClient.configure(
        "tornado.curl_httpclient.CurlAsyncHTTPClient")

    define("port", default=8080, help="run on the given port", type=int)
    # define("address", default=get_listening_address(), help="run on the given address", type=str)
    define("daemon", default=settings.daemon, help="run as daemon", type=bool)
    define("webgate",
           default=settings.webgate,
           help="run on web gate mode",
           type=bool)
    define("log_to_file", default=False, help="log to file", type=bool)
    define("game_host",
           default=settings.game_servers['development']['host'],
           help="bind address",
           type=str)
    define("game_port",
           default=settings.game_servers['development']['port'],
           help="run on the given port",
           type=int)
    define("mode",
           default="development",
           help="default run in development mode",
           type=str)
    if '--daemon' not in sys.argv:
        parse_command_line(sys.argv + ['--log_to_stderr'])
    else:
        parse_command_line(final=False)

    game_server = settings.game_servers[options.mode]
    assert (game_server)
    if options.daemon:
        from lockfile.pidlockfile import PIDLockFile
        import daemon
        main_log = open(settings.main_log_file, "a+")
        pid_file = os.path.join(settings.ROOT, "pids",
                                "%s-%s.pid" % (settings.APPNAME, options.port))
        if not daemon_running(pid_file):
            ctx = daemon.DaemonContext(
                stdout=main_log,
                stderr=main_log,
                pidfile=PIDLockFile(pid_file, threaded=False),
                working_directory=settings.ROOT,
            )
            ctx.open()
            settings.daemon = options.daemon
            options.log_to_file = True
            options.log_file_prefix = settings.tornado_log_prefix % options.port
            parse_command_line(['--log_file_prefix', options.log_file_prefix])

    start()
Beispiel #20
0
 def _start():
     init_db(configuration)
     with open(LOG_FILE, "w+") as log_file, \
             daemon.DaemonContext(pidfile=PIDLockFile(PID_FILE),
                                  detach_process=True,
                                  stdout=log_file, stderr=log_file):
         if no_wsgi:
             API.run("0.0.0.0", configuration.server_port)
         else:
             WSGIServer(API, port=configuration.server_port).start()
Beispiel #21
0
def set(key, value):
    with PIDLockFile(LOCKFILE):
        try:
            with open(DATAFILE, 'r+') as jsondata:
                data = json.load(jsondata)
                data[key] = value
                json.dump(data, jsondata)
        except IOError:  # File does not exist.
            with open(DATAFILE, 'w+') as jsondata:
                json.dump({key: value}, jsondata)
Beispiel #22
0
 def exec(self):
     try:
         dc = DaemonContext(
             working_directory=self.work_dir,
             pidfile=PIDLockFile("/tmp/{}.pid".format(self.basename)),
             stderr=open("{}.err".format(self.basename), "a+"))
         with dc:
             self.__do_process()
     except Exception as e:
         raise
Beispiel #23
0
    def run(self):
        """Startup the processing and go!"""
        self.terminate = threading.Event()
        atexit.register(self.shutdown)
        self.context = daemon.DaemonContext(detach_process=True)
        iter = 0

        if Bcfg2.Options.setup.daemon:
            self.logger.debug("Daemonizing")
            try:
                self.context.pidfile = PIDLockFile(Bcfg2.Options.setup.daemon)
                self.context.open()
            except LockFailed:
                self.logger.error("Failed to daemonize: %s" %
                                  sys.exc_info()[1])
                self.shutdown()
                return
            except LockTimeout:
                self.logger.error("Failed to daemonize: "
                                  "Failed to acquire lock on %s" %
                                  self.setup['daemon'])
                self.shutdown()
                return
            except PIDFileError:
                self.logger.error("Error writing pid file: %s" %
                                  sys.exc_info()[1])
                self.shutdown()
                return
            self.logger.info("Starting daemon")

        self.transport.start_monitor(self)

        while not self.terminate.isSet():
            try:
                interaction = self.transport.fetch()
                if not interaction:
                    continue
                store_thread = ReportingStoreThread(interaction, self.storage)
                store_thread.start()
                self.children.append(store_thread)

                iter += 1
                if iter >= self.cleanup_threshold:
                    self.reap_children()
                    iter = 0

            except (SystemExit, KeyboardInterrupt):
                self.logger.info("Shutting down")
                self.shutdown()
            except:
                self.logger.error("Unhandled exception in main loop %s" %
                                  sys.exc_info()[1])
Beispiel #24
0
def main():
    serverCfg = piccolo.PiccoloServerConfig()

    # start logging
    handler = piccoloLogging(logfile=serverCfg.cfg['logging']['logfile'],
                             debug=serverCfg.cfg['logging']['debug'])
    log = logging.getLogger("piccolo.server")

    if serverCfg.cfg['daemon']['daemon']:
        import daemon
        try:
            import lockfile
        except ImportError:
            print(
                "The 'lockfile' Python module is required to run Piccolo Server. Ensure that version 0.12 or later of lockfile is installed."
            )
            sys.exit(1)
        try:
            from lockfile.pidlockfile import PIDLockFile
        except ImportError:
            print(
                "An outdated version of the 'lockfile' Python module is installed. Piccolo Server requires at least version 0.12 or later of lockfile."
            )
            sys.exit(1)
        from lockfile import AlreadyLocked, NotLocked

        # create a pid file and tidy up if required
        pidfile = PIDLockFile(serverCfg.cfg['daemon']['pid_file'], timeout=-1)
        try:
            pidfile.acquire()
        except AlreadyLocked:
            try:
                os.kill(pidfile.read_pid(), 0)
                print('Process already running!')
                exit(1)
            except OSError:  #No process with locked PID
                print('PID file exists but process is dead')
                pidfile.break_lock()
        try:
            pidfile.release()
        except NotLocked:
            pass

        pstd = open(serverCfg.cfg['daemon']['logfile'], 'w')
        with daemon.DaemonContext(pidfile=pidfile,
                                  files_preserve=[handler.stream],
                                  stderr=pstd):
            # start piccolo
            piccolo_server(serverCfg)
    else:
        # start piccolo
        piccolo_server(serverCfg)
Beispiel #25
0
    def set(self, key, value):
        with PIDLockFile(self.lockfile):
            try:
                data = self.get_data()
            except IOError:  # File does not exist.
                with open(self.datafile, 'w') as jsondata:
                    json.dump({key: value}, jsondata)
            else:
                with open(self.datafile, 'w') as jsondata:
                    data[key] = value
                    json.dump(data, jsondata)

            os.chmod(self.datafile, 0o600)
Beispiel #26
0
def stop_maestral_daemon_process(config_name="maestral", timeout=10):
    """Stops maestral by finding its PID and shutting it down.

    This function first tries to shut down Maestral gracefully. If this fails, it will
    send SIGTERM. If that fails as well, it will send SIGKILL.

    :param str config_name: The name of the Maestral configuration to use.
    :param float timeout: Number of sec to wait for daemon to shut down before killing it.
    :returns: ``Exit.Ok`` if successful, ``Exit.Killed`` if killed and ``Exit.NotRunning``
        if the daemon was not running.
    """

    logger.debug("Stopping daemon")
    lockfile = PIDLockFile(pidpath_for_config(config_name))
    pid = lockfile.read_pid()

    if not pid or is_pidfile_stale(lockfile):
        lockfile.break_lock()
        return Exit.NotRunning

    try:
        # tell maestral daemon to shut down
        with MaestralProxy(config_name) as m:
            m.stop_sync()
            m.shutdown_pyro_daemon()
    except Pyro5.errors.CommunicationError:
        logger.debug("Could not communicate with daemon")
        try:
            os.kill(pid, signal.SIGTERM)  # try to send SIGTERM to process
            logger.debug("Terminating daemon process")
        except ProcessLookupError:
            logger.debug("Daemon was not running")
            return Exit.NotRunning
    finally:
        # wait for maestral to carry out shutdown
        logger.debug("Waiting for shutdown")
        while timeout > 0:
            try:
                os.kill(pid, signal.SIG_DFL)  # query if still running
            except ProcessLookupError:
                logger.debug("Daemon shut down")
                return Exit.Ok  # return True if not running anymore
            else:
                time.sleep(0.2)  # wait for 0.2 sec and try again
                timeout -= 0.2

        # send SIGKILL after timeout, delete PID file and return False
        os.kill(pid, signal.SIGKILL)
        logger.debug("Daemon process killed")
        lockfile.break_lock()
        return Exit.Killed
Beispiel #27
0
def stop_maestral_daemon_process(config_name='maestral', timeout=10):
    """Stops maestral by finding its PID and shutting it down.

    This function first tries to shut down Maestral gracefully. If this fails, it will
    send SIGTERM. If that fails as well, it will send SIGKILL.

    :param str config_name: The name of the Maestral configuration to use.
    :param float timeout: Number of sec to wait for daemon to shut down before killing it.
    :returns: ``Exit.Ok`` if successful, ``Exit.Killed`` if killed and ``Exit.NotRunning``
        if the daemon was not running.
    """

    logger.debug('Stopping daemon')
    lockfile = PIDLockFile(pidpath_for_config(config_name))
    pid = lockfile.read_pid()

    try:
        if not pid or not _process_exists(pid):
            return Exit.NotRunning

        try:
            with MaestralProxy(config_name) as m:
                m.stop_sync()
                m.shutdown_pyro_daemon()
        except Pyro5.errors.CommunicationError:
            logger.debug('Could not communicate with daemon, sending SIGTERM')
            _send_term(pid)
        finally:
            logger.debug('Waiting for shutdown')
            while timeout > 0:
                if not _process_exists(pid):
                    logger.debug('Daemon shut down')
                    return Exit.Ok
                else:
                    time.sleep(0.2)
                    timeout -= 0.2

            # send SIGTERM after timeout and delete PID file
            _send_term(pid)

            time.sleep(1)

            if not _process_exists(pid):
                logger.debug('Daemon shut down')
                return Exit.Ok
            else:
                os.kill(pid, signal.SIGKILL)
                logger.debug('Daemon killed')
                return Exit.Killed
    finally:
        lockfile.break_lock()
Beispiel #28
0
def get_lock(workdir, force=False):
    from lockfile.pidlockfile import PIDLockFile
    from lockfile import AlreadyLocked

    pidfile = PIDLockFile(os.path.join(workdir, 'lobster.pid'), timeout=-1)
    try:
        pidfile.acquire()
    except AlreadyLocked:
        if not force:
            logger.error(
                "another instance of lobster is accessing {0}".format(workdir))
            raise
    pidfile.break_lock()
    return pidfile
Beispiel #29
0
def main():
    working_dir = os.path.abspath(os.path.join(os.path.dirname(__file__),
                                               ".."))

    if not os.path.exists("/tmp/pristella"):
        os.makedirs("/tmp/pristella")

    context = daemon.DaemonContext(
        working_directory=working_dir,
        pidfile=PIDLockFile("/tmp/pristella/daemon.pid"),
        stdout=open(os.path.join(working_dir, "log/log.txt"), "a+"),
        stderr=open(os.path.join(working_dir, "log/error.txt"), "a+"))

    with context:
        daemon_process()
Beispiel #30
0
    def __init__(self, mainOptions):
        self.pidfilename = mainOptions.get('pidfilename', None)  # the file we're using for locking
        self.parentpidvar = mainOptions.get('parentpidvar', None)  # environment variable holding parent pid
        self.parentpid = None  # parent pid which already has the lock
        self.ppath = None  # complete path to the lock file
        self.pidlockfile = None  # PIDLockFile object
        self.pidfilepid = None  # pid of the process which has the lock
        self.locktorelease = None  # PIDLockFile object we should release when done

        if self.parentpidvar is not None and self.parentpidvar in os.environ:
            self.parentpid = int(os.environ[self.parentpidvar])

        if self.pidfilename is not None:
            self.ppath = os.path.join(gp.get_masterdatadir(), self.pidfilename)
            self.pidlockfile = PIDLockFile(self.ppath)