def _finalize(self, link_as=None, name_hint=None): log.debug(f'finalize hint={name_hint} link_as={link_as} {self._state}') self.log.disable_hint() if link_as and self._state == 'spun_down': self.hooks.save.in_reverse() temp_path = self.path self.path = temp.unique_dir(self._parent_path, hint=name_hint) log.debug(f'saving to temp {temp_path}') self._state = 'saving' self.expiration.depend_on_loaded_python_modules() del self.log with open(os.path.join(temp_path, 'machine.clpickle'), 'wb') as f: cloudpickle.dump(self, f) log.debug(f'moving {temp_path} to {self.path}') os.rename(temp_path, self.path) self._state == 'saved' link_this = self.path else: assert self._state in ('spun_down', 'loaded', 'dropped') log.info(f'discarding {self.path}') temp.remove(self.path) link_this = self._parent_path self._state = 'dropped' if (link_this and link_as and os.path.realpath(link_as) != os.path.realpath(link_this)): log.debug(f'linking {link_this} to {link_as}') if os.path.lexists(link_as): if os.path.exists(link_as) and not needs_a_rebuild(link_as): log.critical(f'Refusing to overwrite fresh {link_as}') raise RuntimeError(f'Not overriding fresh {link_as}') os.unlink(link_as) os.symlink(link_this, link_as) return link_as
def apply(self, step, *args, **kwargs): func, tag = step_loader.func_and_autotag(step, *args, **kwargs) log.debug(f'apply {self.path} {step} {func} {args} {kwargs}') if self._state == 'spun_up': log.debug(f'applying to unclean') return func(self, *args, **kwargs) elif self._state == 'loaded': log.debug(f'applying to clean') return self._cache_aware_apply(step, tag, func, args, kwargs) else: log.critical(f'apply to state={self._state}') raise RuntimeError(f'State machine error, apply to {self._state}')