def test_lock_exploding(self): # get the lock and explode in the middle (then ignore the blast) try: with filelock(self.test_path): raise ValueError("pumba") except ValueError: pass # get the lock again with filelock(self.test_path): pass
def clean_unused_venvs(self, max_days_to_keep): """Compact usage stats and remove venvs. This method loads the complete file usage in memory, for every venv compact all records in one (the lastest), updates this info for every env deleted and, finally, write the entire file to disk. If something failed during this steps, usage file remains unchanged and can contain some data about some deleted env. This is not a problem, the next time this function it's called, this records will be deleted. """ with filelock(self.stat_file_lock): now = datetime.utcnow() venvs_dict = self._get_compacted_dict_usage_from_file() for venv_uuid, usage_date in venvs_dict.copy().items(): usage_date = self._str_to_datetime(usage_date) if (now - usage_date).days > max_days_to_keep: # remove venv from usage dict del venvs_dict[venv_uuid] venv_meta = self.venvscache.get_venv(uuid=venv_uuid) if venv_meta is None: # if meta isn't found means that something had failed previously and # usage_file wasn't updated. continue env_path = venv_meta['env_path'] logger.info("Destroying virtualenv at: %s", env_path) # #256 destroy_venv(env_path, self.venvscache) self._write_compacted_dict_usage_to_file(venvs_dict)
def run(self): self.pre_lock = time.time() time.sleep(.01) with filelock(self.filepath): self.in_lock = time.time() self.middle_work.wait() time.sleep(.01) self.post_work = time.time()
def remove(self, env_path): """Remove metadata for a given virtualenv from cache.""" with filelock(self.lockpath): cache = self._read_cache() logger.debug("Removing virtualenv from cache: %s" % env_path) lines = [ line for line in cache if json.loads(line).get( 'metadata', {}).get('env_path') != env_path ] self._write_cache(lines)
def remove(self, env_path): """Remove metadata for a given virtualenv from cache.""" with filelock(self.lockpath): cache = self._read_cache() logger.debug("Removing virtualenv from cache: %s" % env_path) lines = [ line for line in cache if json.loads(line).get('metadata', {}).get('env_path') != env_path ] self._write_cache(lines)
def store(self, installed_stuff, metadata, interpreter, options): """Store the virtualenv metadata for the indicated installed_stuff.""" new_content = { 'timestamp': int(time.mktime(time.localtime())), 'installed': installed_stuff, 'metadata': metadata, 'interpreter': interpreter, 'options': options } logger.debug("Storing installed=%s metadata=%s interpreter=%s options=%s", installed_stuff, metadata, interpreter, options) with filelock(self.lockpath): self._write_cache([json.dumps(new_content)], append=True)
def run(self): self.pre_lock = time.time() with filelock(self.filepath): self.in_lock = time.time() self.middle_work.wait() self.post_work = time.time()
def run(self): with filelock(self.filepath): time.sleep(.01) self.in_lock = time.time() self.middle_work.wait() self.post_work = time.time()