Пример #1
0
    def _try_lock(self, requirements, candidate):
        """ Function that tries to lock given candidate resource """
        resource_id = candidate.get("id")
        try:
            pid_file = f"{resource_id}.pid"
            self.logger.debug('Trying lock using: %s',
                              os.path.join(self._lock_folder, pid_file))

            _lockable = PidFile(pidname=pid_file, piddir=self._lock_folder)
            _lockable.create()
            self.logger.info('Allocated: %s, lockfile: %s', resource_id,
                             pid_file)

            def release():
                nonlocal self, resource_id, _lockable
                self.logger.info('Release resource: %s', resource_id)
                _lockable.close()
                del self._allocations[resource_id]

            return Allocation(requirements=requirements,
                              resource_info=candidate,
                              _release=release,
                              pid_file=_lockable.filename)
        except PidFileError as error:
            raise AssertionError('no success') from error
Пример #2
0
def acquire_lock(name):
    """
    Attempts to acquire a lock for the name provided.
    """
    lock = PidFile(
        pidname="%s.LOCK" % name,
        piddir=LOCK_ROOT,
        enforce_dotpid_postfix=False)
    lock.create()
class Index:
    """
    There's a file on disc called "local state file" and it says when was the last timestamp pulled from Slack.
    """
    def __init__(self, index_path: Path):
        self._index_path = index_path
        self._data = None
        self._pidfile = PidFile("index", piddir="/tmp", lock_pidfile=True)

    def __enter__(self):
        if self._data is not None:
            raise Exception("can only enter once")
        self._index_path.parent.mkdir(exist_ok=True, parents=True)
        self._pidfile.create()
        if not self._index_path.exists():
            self._index_path.write_text("{}")
        self._data = json.load(self._index_path.open("r"))
        return self

    def __exit__(self, exception_type, exception_value, traceback):
        if exception_type is None:
            json.dump(self._data, self._index_path.open("w"), indent=4)
        self._pidfile.close()
        self._data = None

    def ts(self, channel: Conversation) -> Optional[datetime.datetime]:
        serialized = self._timestamps.get(repr(channel))
        if serialized:
            return self._str_to_ts(serialized)

    def set_ts(self, channel: Conversation, ts: datetime.datetime) -> None:
        self._timestamps[repr(channel)] = self._ts_to_str(ts)

    def set_all(self, ts):
        for k in self._timestamps:
            self._timestamps[k] = self._ts_to_str(ts)

    @classmethod
    def _ts_to_str(cls, ts: datetime.datetime) -> str:
        return ts.strftime(cls.datetime_format)

    @classmethod
    def _str_to_ts(cls, str_datetime: str) -> datetime.datetime:
        return datetime.datetime.strptime(str_datetime, cls.datetime_format)

    @property
    def _timestamps(self):
        return self._data.setdefault('timestamps', {})

    datetime_format = "%Y-%m-%d_%H:%M:%S.%f_%z"
Пример #4
0
class Pid(object):
    def __init__(self):
        self.pid = PidFile(piddir=LOCKFILE_DIR)

    def __enter__(self):
        try:
            self.pid.__enter__()
        except PidFileAlreadyLockedError:
            logging.getLogger(__name__).warning('%s is locked',
                                                self.pid.filename)
            raise SystemExit()
        return self

    def __exit__(self, exc_type=None, exc_value=None, exc_tb=None):
        self.pid.__exit__()
Пример #5
0
def run(**kwargs):
    params = get_params(kwargs)
    configure_logging(params)

    try:
        with PidFile(get_pidfile_key(params['query_folder'])):
            logging.info('Starting execution.')

            current_exec_time = utcnow()

            config = load_config(params['config_path'])
            config['current_exec_time'] = current_exec_time
            config['query_folder'] = params['query_folder']
            config['output_folder'] = params['output_folder']
            config['reruns'], rerun_files = read_reruns(params['query_folder'])

            reader = Reader(config)
            selector = Selector(reader, config)
            executor = Executor(selector, config)
            writer = Writer(executor, config,
                            configure_graphite(config, params))
            writer.run()

            delete_reruns(
                rerun_files)  # delete rerun files that have been processed
            logging.info('Execution complete.')
    except PidFileError:
        logging.warning(
            'A job with folder {} is already running, exiting successfully.'.
            format(params['query_folder']))
Пример #6
0
def main():
    try:
        with PidFile("zapret_ipfile.py.pid"):
            __start()
            __gen_ipfile()
    except PidFileError:
        __edr.printt("Уже запущено обновление.")
Пример #7
0
def check_pod_status():
    """Background Task to update status/phase of known pods
    """

    from api.models.kubepod import KubePod

    try:
        with PidFile('pod_status') as p:
            print(p.pidname)

            ns = os.environ.get('MLBENCH_NAMESPACE')

            config.load_incluster_config()
            v1 = client.CoreV1Api()

            pods = pods = KubePod.objects.all()

            for pod in pods:
                ret = v1.read_namespaced_pod(pod.name, ns)
                phase = ret.status.phase

                if phase != pod.phase:
                    pod.phase = phase
                    pod.save()

    except PidFileError:
        return
Пример #8
0
def launch_app(name, pidname, appid, wxapp, **app_kwargs):
    print(f"Starting {name}")

    pid_dir = make_pid_dir()

    print('My PID is:', os.getpid())

    try:
        with PidFile(pidname=pidname, piddir=str(pid_dir)) as p:
            print("###")
            print(wx.Colour(wx.SYS_COLOUR_BACKGROUND))
            print(wx.Colour(240, 240, 240))
            print("###")

            if sys.platform == "win32":
                import ctypes

                ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(
                    appid)
                # wx.html2.WebView.MSWSetEmulationLevel(level=wx.html2.WEBVIEW_EMU_DEFAULT)

            App = wxapp(**app_kwargs)
            App.MainLoop()

            print("Goodbye :)")

            return 0

    except (BlockingIOError, PidFileAlreadyLockedError):
        switch_process(name, pid_dir / f"{pidname}.pid")
        return 1
Пример #9
0
def main():
    arguments = parse_arguments()
    logger = log(arguments.loglevel)

    logger.warning('Starting json2mqtt')

    try:
        with PidFile('json2mqtt', piddir='/var/tmp'):
            logger.info("Reading configuration file {}".format(
                arguments.filename))
            settings = Settings(filename=arguments.filename)

            logger.info("Reading schema files {}".format(arguments.filename))
            schemas = Schemas(logger=logger, schema_dir=settings.schema_dir)
            schemas.import_all()

            logger.info("Starting MQTT Listener server")
            server = MQTTListener(
                settings=settings,
                schemas=schemas,
                logger=logger,
            )
            server.run()

    except ConfigError as e:
        logger.error("Config file contains errors: {}".format(e))
        sys.exit(1)

    except PidFileAlreadyLockedError:
        logger.error("Another instance of this service is already running")
        sys.exit(1)
Пример #10
0
    def work(self):
        """run the sequence, emit the finish-line"""
        # print('I will now start to work!')
        # print('data from main:' ,zmqquery(self.zmq_sSeq, 'data'))
        try:
            with PidFile("zmqLogger"):
                msg = "zmqLogger is not running, no data available, aborting"
                self._logger.error(msg)
                self.sig_finished.emit(msg)
                return
        except PidFileError:
            pass

        try:
            with self.controlsLock:
                fin = self.running()
        except problemAbort as e:
            fin = f"Error occurred, aborting sequence! Error: {e}"
            self._logger.error(fin)

        finally:
            try:
                self.sig_finished.emit(fin)
            except NameError:
                self.sig_finished.emit(
                    "An Error occurred! Aborted sequence completely!")
                self._logger.error(
                    "An Error occurred! Aborted sequence completely!")
Пример #11
0
def run():
    conf = {
        '/': {
            'tools.sessions.on': True,
            'tools.caching.on': False,
            #            'tools.auth_basic.on': True,
            #            'tools.auth_basic.realm':'admin',
            #            'tools.auth_basic.checkpassword':encrypt_pwd,
            'tools.staticdir.root': config._ROOT_DIR_
        },
        '/static': {
            'tools.staticdir.on': True,
            'tools.staticdir.dir': config._STATIC_DIR_
        },
        '/bootstrap': {
            'tools.staticdir.on': True,
            'tools.staticdir.dir': config._STATIC_DIR_ + 'bootstrap/css/'
        },
        '/favicon.ico': {
            'tools.staticfile.on': True,
            'tools.staticfile.filename': favicon()
        }
    }

    with PidFile('server.pid') as spid:
        print(spid.pidname)
        print(spid.piddir)
        portNumber = config.port()
        cherrypy.config.update({
            'server.socket_host': '0.0.0.0',
            'server.socket_port': portNumber,
        })
        cherrypy.quickstart(Notes(), '/', conf)
Пример #12
0
def __configure_daemon(detach: bool, camguard: Any) -> DaemonContext:
    """in case of running daemonized, this configures daemon as described in PEP 3143

    Args:
        detach (bool): set to true to detach from terminal 
        camguard (Any): the camguard instance to run 

    Returns:
        DaemonContext: daemon context which handles turning program into a daemon process
    """
    signal_map: Dict[Signals, Any] = {
        # lambda type couldn't be inferred
        SIGTERM: lambda sig, _: __shutdown(camguard, sig),  # type: ignore
        SIGINT: lambda sig, _: __shutdown(camguard, sig)  # type: ignore
    }

    # setup pid file context (/var/run/camguard.pid)
    pid_file: Optional[PidFile] = PidFile(
        pidname='camguard') if detach else None
    work_dir: str = '/' if detach else '.'

    return DaemonContext(detach_process=detach,
                         pidfile=pid_file,
                         signal_map=signal_map,
                         stderr=sys.stderr,
                         stdout=sys.stdout,
                         working_directory=work_dir)
Пример #13
0
def main():
    try:
        with PidFile("zapretdelete_duple.py.pid"):
            __start()
            __genereate()
    except PidFileError:
        __edr.printt("Уже запущено обновление.")
Пример #14
0
 async def fetch_list(self):
     '''Fetch & store the list'''
     if not self.fetcher:
         return
     set_running(f'{self.__class__.__name__}-{self.vendor}-{self.listname}')
     try:
         with PidFile(f'{self.listname}.pid', piddir=self.meta):
             if not await self.__newer():
                 unset_running(
                     f'{self.__class__.__name__}-{self.vendor}-{self.listname}'
                 )
                 return
             async with aiohttp.ClientSession() as session:
                 async with session.get(self.url) as r:
                     content = await r.content.read()
                     if self.__same_as_last(content):
                         return
                     self.logger.info('Got a new file \o/')
                     with (self.directory / '{}.txt'.format(
                             datetime.now().isoformat())).open('wb') as f:
                         f.write(content)
                     unset_running(
                         f'{self.__class__.__name__}-{self.vendor}-{self.listname}'
                     )
     except PidFileError:
         self.logger.info('Fetcher already running')
     finally:
         unset_running(
             f'{self.__class__.__name__}-{self.vendor}-{self.listname}')
Пример #15
0
def check_pod_status():
    """Background Task to update status/phase of known pods"""
    try:
        with PidFile("pod_status") as p:
            _check_and_update_pod_phase()

    except PidFileError:
        return
Пример #16
0
def main():
    args = parse_arguments()

    if args.pid is not None:
        with PidFile(args.pid):
            return start_sync(args)
    else:
        return start_sync(args)
Пример #17
0
def main(**kwargs):
    if __edr.str2bool(__edr.config('Main')['nginx']):
        __start()
        try:
            with PidFile("zapretnginx.py.pid"):
                __genereate(**kwargs)
        except PidFileError:
            __edr.printt("Идёт обновление базы, выполненние невозможно.")
            __edr.LogWrite("Идёт обновление базы, выполненние невозможно.")
Пример #18
0
def run_flask(debug):
    sys.stderr = sys.stdout

    pid_dir = pathlib.Path(user_config_dir("GunShotMatch")) / ".pid"
    if not pid_dir.exists():
        pid_dir.mkdir()

    with PidFile(pidname="DataViewer", piddir=str(pid_dir)) as p:
        app.run(debug=debug)
Пример #19
0
def main(args):
    if args.daemon:
        import daemon
        from pid import PidFile
        pidfile = PidFile(conf.serv_pidfile)
        with daemon.DaemonContext(pidfile=pidfile):
            start_server()
    else:
        start_server()
def main():
    settings_folder = r"/home/downloader_settings"
    set_logger(os.path.join(settings_folder, "uploader.log"))
    with PidFile():
        for file in os.listdir(settings_folder):
            settings_file = os.path.join(settings_folder, file)
            _, file_extension = os.path.splitext(settings_file)
            if file_extension == ".json" and "installation_history.json" not in settings_file:
                upload_to_sharepoint(settings_file)
Пример #21
0
 def get_context(self):
     return DaemonContext(uid=self.uid,
                          gid=self.gid,
                          files_preserve=[
                              self.log_handler.stream,
                          ],
                          pidfile=PidFile(self.pid_file),
                          stderr=self.log_handler.stream,
                          stdout=self.log_handler.stream,
                          signal_map=self.sig_map)
Пример #22
0
def check_new_pods():
    """Background task to look for new pods available in cluster.
    Creates corresponding `KubePod` objects in db.
    """
    try:
        with PidFile("new_pods") as p:
            _check_and_create_new_pods()

    except PidFileError:
        return
Пример #23
0
def main() -> None:
    try:
        with PidFile(
            'nice-bot',
            piddir='/var/lib/nice-bot'
        ):
            Service().start()
    except PidFileError:
        logging.error('Nice-bot is already running')
        raise
Пример #24
0
    def decorated_function(**values):
        site_id = values['site_id']

        local_tries, local_delay = tries, delay
        while local_tries > 0:
            try:
                with PidFile(piddir='/var/www/jekylledit',
                             pidname=site_id + '.lock'):
                    return f(**values)
            except PidFileAlreadyLockedError:
                local_tries -= 1
                local_delay *= 2
Пример #25
0
def payout_voters(ctx, network, cover_fees, max_weight, share, min_share,
                  message, password):
    """
    Calculate and transmit payments to the voters.
    """
    config = ctx.obj['config']
    printer = ctx.obj["printer"]
    Crypt().decrypt_config(config, password, printer)
    setting = config[network]

    if cover_fees is None:
        cover_fees = setting["cover_fees"]
    if min_share is None:
        min_share = setting["min_share"]
    if max_weight is None:
        max_weight = setting["max_weight"]
    if message is None:
        message = setting["message"]
    if share is None:
        share = setting["share"]

    with PidFile(piddir='/tmp/'):
        printer.log("starting payment run")
        payer = Payouts(
            db_name=setting["db_name"],
            db_host=setting["db_host"],
            db_pw=setting["db_password"],
            db_user=setting["db_user"],
            network=network,
            delegate_address=setting["delegate_address"],
            pubkey=setting["delegate_pubkey"],
            rewardswallet=setting["rewardswallet"],
            passphrase=setting["delegate_passphrase"][0],
            second_passphrase=setting["delegate_second_passphrase"][0]
            if setting["delegate_second_passphrase"][0] else None,
            printer=printer,
            arky_network_name=setting["arky"])
        max_weight *= setting["coin_in_sat"]

        printer.info("Calculating payments")
        payouts = payer.calculate_payouts(cover_fees=cover_fees,
                                          max_weight=max_weight,
                                          share=share)

        printer.info("transmitting payments")
        payer.transmit_payments(
            payouts,
            message=message,
            min_share=min_share,
        )
        printer.log("Payment run done!")
Пример #26
0
def check_running(pid_file):
    if not os.path.exists('/var/run/' + pid_file + '.pid'):
        return False
    else:
        print('pid file found: /var/run/{0}.pid'.format(pid_file))

        # try a lock, if succesful, it's a stale pid file, and we'll delete it
        try:
            with PidFile(pid_file) as p:
                print('Checking if we\'re already runnning')
                return False
        except:
            print('Already runnning! Stopping.')
            return True
Пример #27
0
def run_args(args):
    config_path = None  # Must be replaced by dynamic default value
    pidfile_path = None  # Must be replaced by dynamic default value
    is_debug = False

    while len(args) > 0:
        if args[0] == '--debug':
            if is_debug:
                print('Duplicate option --debug!', file=sys.stderr)
                exit(1)
            is_debug = True
            args = args[1:]
        elif args[0] == '--pidfile':
            if pidfile_path is not None:
                print('Duplicate option --pidfile!', file=sys.stderr)
                exit(1)
            if len(args) < 2:
                print('Missing argument to --pidfile!', file=sys.stderr)
                exit(1)
            pidfile_path = args[1]
            args = args[2:]
        elif args[0] == '--config':
            if config_path is not None:
                print('Duplicate option --config!', file=sys.stderr)
                exit(1)
            if len(args) < 2:
                print('Missing argument to --config!', file=sys.stderr)
                exit(1)
            config_path = args[1]
            args = args[2:]
        # TODO: How about '--help'?
        else:
            print('Unknown option {}!'.format(args[0]), file=sys.stderr)
            exit(1)

    if config_path is None:
        config_path = 'briar_rose.config'

    if pidfile_path is None:
        pidfile_path = default_pidfile_path()

    if is_debug:
        print('Would use pidfile at {}'.format(pidfile_path))
        print('Loading configuration from {}'.format(config_path))
        run_debug(config_path)
    else:
        with PidFile(pidname=basename(pidfile_path),
                     piddir=dirname(pidfile_path),
                     enforce_dotpid_postfix=False):
            run_daemon(config_path)
Пример #28
0
def main():
    description = """IRRd main process"""
    parser = argparse.ArgumentParser(description=description)
    parser.add_argument(
        '--config',
        dest='config_file_path',
        type=str,
        help=
        f'use a different IRRd config file (default: {CONFIG_PATH_DEFAULT})')
    parser.add_argument('--foreground',
                        dest='foreground',
                        action='store_true',
                        help=f"run IRRd in the foreground, don't detach")
    args = parser.parse_args()

    mirror_frequency = int(os.environ.get('IRRD_SCHEDULER_TIMER_OVERRIDE', 15))

    daemon_kwargs = {
        'umask': 0o022,
    }
    if args.foreground:
        daemon_kwargs['detach_process'] = False
        daemon_kwargs['stdout'] = sys.stdout
        daemon_kwargs['stderr'] = sys.stderr

    # config_init w/ commit may only be called within DaemonContext
    config_init(args.config_file_path, commit=False)

    with daemon.DaemonContext(**daemon_kwargs):
        config_init(args.config_file_path)
        piddir = get_setting('piddir')
        logger.info('IRRd attempting to secure PID')
        try:
            with PidFile(pidname='irrd', piddir=piddir):
                logger.info(
                    f'IRRd {__version__} starting, PID {os.getpid()}, PID file in {piddir}'
                )
                run_irrd(mirror_frequency)
        except PidFileError as pfe:
            logger.error(
                f'Failed to start IRRd, unable to lock PID file irrd.pid in {piddir}: {pfe}'
            )
        except Exception as e:
            logger.error(
                f'Error occurred in main process, terminating. Error follows:')
            logger.exception(e)
            os.kill(os.getpid(), signal.SIGTERM)
Пример #29
0
 def run_forever(self):
     with PidFile(pidname="crypto_com_market_data_worker",
                  piddir="./logs") as pidfile:
         try:
             loop = asyncio.get_event_loop()
             loop.run_until_complete(self.run())
         except KeyboardInterrupt:
             self.logger.info("Interrupted")
             pidfile.close(fh=pidfile.fh, cleanup=True)
             try:
                 sys.exit(0)
             except SystemExit:
                 os._exit(0)
         except Exception as e:
             self.logger.exception(e)
         finally:
             pidfile.close(fh=pidfile.fh, cleanup=True)
             self.logger.info("Bye bye!")
Пример #30
0
def main():
    arguments = parse_arguments()
    logger = get_logger(verbosity=arguments.verbosity, log_file=arguments.log_file)

    try:
        with PidFile('mqtt_alarm_server', piddir='/var/tmp'):
            logger.info("Reading configuration file {}".format(arguments.config))
            settings = Settings(filename=arguments.config)

            logger.info("Starting MQTT Listener server")
            mqtt_server = AlarmMQTTListener(settings=settings, logger=logger)
            mqtt_server.run()
    except ConfigError as e:
        logger.error("Config file contains errors: {}".format(e))
        sys.exit(1)
    except PidFileAlreadyLockedError:
        logger.error("Another instance of this service is already running")
        sys.exit(1)