def __init__(self, path): import json self._path = path try: with open(path) as f: old_data = json.load(f) except json.JSONDecodeError: logger.warning('Could not decode config') old_data = {} except OSError: logger.debug('No config file') old_data = {} for i in range(10): try: self._f = open(path, 'w+') fcntl.flock(self._f, fcntl.LOCK_EX | fcntl.LOCK_NB) self._locked = True break except BlockingIOError: import signal pid = old_data.get('pid') if pid: logger.info( f'Config file is locked (try {i}). Killing previous instance {pid}' ) os.kill(pid, signal.SIGTERM) time.sleep(.05) else: logger.error(f'Config file is locked and no pid to kill') assert self._locked
def dispatch(self, event): try: super().dispatch(event) except Exception as e: import traceback traceback.print_exc() logger.error(str(e))
def clean(): try: while True: for r in store.values(): if time() - r.t > r.to: r.release() sleep(1.) except: logger.error('error during clean') for r in store.values(): r.release()
def get_free(pool): for i in range(20): av = [p for p in pool if check_free(p)] logger.debug('Free ports' + str(av)) if av: break sleep(.1) if not av: logger.error('No ports available') return None else: return av[0]
def remote_install_dependency(address, module): user, host = address.split('@') rem = f"/home/{user}/.chi/cache" repo = join('/', *module.__file__.split('/')[:-2]) target = address + ':' + rem + repo logger.debug(f"Uploading {repo} to {target}") copydir(repo, target, with_src=False) cmd = f'pip3 install --user -e {rem+repo}' try: out = subprocess.check_output(['ssh', address, f'echo "{cmd}"; {cmd}'], universal_newlines=True) except subprocess.CalledProcessError as e: logger.error( f'Install failed with code {e.returncode} and output:\n{e.output}') raise e
def killer(): while time() - state['last_request'] < timeout: sleep(2) logger.error('timeout') os.kill(os.getpid(), signal.SIGINT) # kill self