def _lock(self, job): self.lock_file = self._create_self_lock_file(job) lock_files = self._get_lock_files() lock_files.remove(self.lock_file) for path in lock_files: lock_pid = int(open(path, 'r').read()) if pid_exists(lock_pid): msg = 'File "%s" acquired by PID %u. ' % (path, lock_pid) raise OtherProcessHoldsLockError(msg) else: try: os.unlink(path) except OSError: self.log.warn("Unable to remove stale lock: %s", path)
def _lock(self, job): self.lock_file = self._create_self_lock_file(job) lock_files = self._get_lock_files() lock_files.remove(self.lock_file) for path in lock_files: try: lock_pid = int(open(path, 'r').read()) except Exception: msg = 'Cannot read PID from "%s".' % path raise LockCreationError(msg) else: if pid_exists(lock_pid): msg = 'File "%s" acquired by PID %u. ' % (path, lock_pid) raise OtherProcessHoldsLockError(msg) else: try: os.unlink(path) except OSError: self.log.warn("Unable to remove stale lock: %s", path)
def __enter__(self): flags = os.O_CREAT | os.O_EXCL | os.O_WRONLY | os.O_SYNC timelimit = time.monotonic() + self.timeout while True: try: fd = os.open(self.filename, flags) os.write(fd, self.pid) os.close(fd) self.locked = True return self except Exception: # pylint: disable=W0703 try: # Read the file to realize what's happening. with open(self.filename, 'r', encoding='utf-8') as f: content = f.read() existing_lock_pid = int(content) if existing_lock_pid != self.pid: # If there's no process with the PID in lockfile, # let's try to remove the lockfile to acquire the # lock in the next iteration. if not pid_exists(existing_lock_pid): os.remove(self.filename) continue except Exception: # pylint: disable=W0703 # If we cannot read the lock file, let's just # go on. Maybe in next iteration (if we have time) # we have a better luck. pass # If we get to this point, the lock file is there, it belongs # to a running process and we are just waiting for the lock # to be released. if self.timeout <= 0: raise AlreadyLocked('File is already locked.') elif time.monotonic() > timelimit: raise AlreadyLocked('Timeout waiting for the lock.') else: time.sleep(0.1)
s_ping, o_ping = utils_test.ping(vm_ip, count=ping_count, timeout=ping_timeout, output_func=logging.debug, session=server_session) logging.info(o_ping) if s_ping != 0: server_session.close() raise error.TestError("%s did not respond after %d sec." % (vm.name, ping_timeout)) server_session.close() if graphics_server: logging.info("To check the process running '%s'.", remote_viewer_executable) if process.pid_exists(int(remote_viewer_pid)) is False: raise error.TestFail( "PID '%s' for process '%s'" " does not exist" % (remote_viewer_pid, remote_viewer_executable)) else: logging.info( "PID '%s' for process '%s' still exists" " as expected.", remote_viewer_pid, remote_viewer_executable) logging.debug("Kill the PID '%s' running '%s'", remote_viewer_pid, remote_viewer_executable) process.kill_process_tree(int(remote_viewer_pid)) # Check unsafe result and may do migration again in right mode check_unsafe_result = True
logging.info("Check VM network connectivity after migrating") s_ping, o_ping = utils_test.ping(vm_ip, count=ping_count, timeout=ping_timeout, output_func=logging.debug, session=server_session) logging.info(o_ping) if s_ping != 0: server_session.close() raise error.TestError("%s did not respond after %d sec." % (vm.name, ping_timeout)) server_session.close() if graphics_server: logging.info("To check the process running '%s'.", remote_viewer_executable) if process.pid_exists(int(remote_viewer_pid)) is False: raise error.TestFail("PID '%s' for process '%s'" " does not exist" % (remote_viewer_pid, remote_viewer_executable)) else: logging.info("PID '%s' for process '%s' still exists" " as expected.", remote_viewer_pid, remote_viewer_executable) logging.debug("Kill the PID '%s' running '%s'", remote_viewer_pid, remote_viewer_executable) process.kill_process_tree(int(remote_viewer_pid)) # Check unsafe result and may do migration again in right mode