def load_sub_devs_cache(self): with cmlock(timeout=5, delay=1, lockfile=self.sub_devs_name+".lock", intent="load"): with open(self.sub_devs_name, 'r') as f: try: return set(json.load(f)) except: raise ex.excError("corrupted sub devs cache file %s"%self.sub_devs_name)
def run(self): try: with lock.cmlock(lockfile=os.path.join(self.var_d, "run.lock"), timeout=0): self._run() except lock.LOCK_EXCEPTIONS: raise ex.excError("task is already running (maybe too long for the schedule)") finally: self.svc.notify_done("run", rids=[self.rid])
def test_no_run_x_acquired_fails(mocker, tmp_file, timeout): side_effects = [lock.LockAcquire({ "pid": 0, "intent": "" })] * (timeout + 1) mocker.patch('lock.os.getpid', return_value=-1) lock_nowait = mocker.patch('lock.lock_nowait', side_effect=side_effects) runs = [] with pytest.raises(lock.LockTimeout): with lock.cmlock(lockfile=tmp_file, timeout=timeout): runs.append(1) assert len(runs) == 0 assert lock_nowait.call_count == max(timeout, 1)
def _run(self): """ Acquire the osvcd lock, write the pid in a system-compatible pidfile, and start the daemon loop. """ try: with cmlock(lockfile=rcEnv.paths.daemon_lock, timeout=1, delay=1): if daemon_process_running(): self.log.error("a daemon process is already running") sys.exit(1) self.write_pid() except LockTimeout: self.log.error("a daemon is already running, and holding the " "daemon lock") sys.exit(1) self.loop_forever()
def start(self): lockfile = os.path.join(rcEnv.paths.pathlock, "disk.loop") if self.is_up(): self.log.info("%s is already up" % self.label) return try: with cmlock(timeout=30, delay=1, lockfile=lockfile): cmd = ['lofiadm', '-a', self.loopFile] ret, out, err = self.vcall(cmd) except Exception as exc: raise ex.excError(str(exc)) if ret != 0: raise ex.excError self.loop = file_to_loop(self.loopFile) if len(self.loop) == 0: raise ex.excError("loop device did not appear or disappeared") time.sleep(1) self.log.info("%s now loops to %s" % (self.loop, self.loopFile)) self.can_rollback = True
def test_try_x_times_to_get_lock_until_it_acquires_lock( mocker, tmp_file, timeout): runs = [] if timeout == 0: side_effects = [None] expected_lock_nowait = 1 else: side_effects = [lock.LockAcquire({ "pid": 0, "intent": "" })] * (timeout - 1) # noinspection PyTypeChecker side_effects.append(None) expected_lock_nowait = timeout # mocker.patch('lock.os.getpid', return_value=-1) lock_nowait = mocker.patch('lock.lock_nowait', side_effect=side_effects) with lock.cmlock(lockfile=tmp_file, timeout=timeout): runs.append(1) assert len(runs) == 1 assert lock_nowait.call_count == expected_lock_nowait
def write_sub_devs_cache(self, dl): with cmlock(timeout=5, delay=1, lockfile=self.sub_devs_name+".lock", intent="write"): with open(self.sub_devs_name, 'w') as f: f.write(json.dumps(list(dl)))