def set_start_time(self): if self.t() is not None: return d = os.path.abspath(os.path.join(self.root, "start-time")) create(d) start, t = time.time(), iso() while time.time() - start <= 1.0: try: p = os.path.join(d, t) os.symlink("../mesos/" + self.mesos_id, p) self._writef("t", t) self.timestamp = t return except OSError as e: if e.errno != errno.EEXIST: raise time.sleep(random.uniform(0.005, 0.025)) t = iso()
def lock(self, name, flags, seconds=60): fmt_time = "indefinite" if seconds is None else "%ds" % seconds fmt_flags = deimos.flock.format_lock_flags(flags) flags, seconds = deimos.flock.nb_seconds(flags, seconds) log.info("request // %s %s (%s)", name, fmt_flags, fmt_time) p = self.resolve(os.path.join("lock", name), mkdir=True) lk = deimos.flock.LK(p, flags, seconds) try: lk.lock() except deimos.flock.Err: log.error("failure // %s %s (%s)", name, fmt_flags, fmt_time) raise if (flags & LOCK_EX) != 0: lk.handle.write(iso() + "\n") log.info("success // %s %s (%s)", name, fmt_flags, fmt_time) return lk
def dirs(self, before=time.time(), exited=True): """ Provider a generator of container state directories. If exited is None, all are returned. If it is False, unexited containers are returned. If it is True, only exited containers are returned. """ timestamp = iso(before) root = os.path.join(self.root, "start-time") os.chdir(root) by_t = ( d for d in glob.iglob("????-??-??T*.*Z") if d < timestamp ) if exited is None: def predicate(directory): return True else: def predicate(directory): exit = os.path.join(directory, "exit") return os.path.exists(exit) is exited return ( os.path.join(root, d) for d in by_t if predicate(d) )
def dirs(self, before=time.time(), exited=True): """ Provider a generator of container state directories. If exited is None, all are returned. If it is False, unexited containers are returned. If it is True, only exited containers are returned. """ timestamp = iso(before) root = os.path.join(self.root, "start-time") os.chdir(root) by_t = (d for d in glob.iglob("????-??-??T*.*Z") if d < timestamp) if exited is None: def predicate(directory): return True else: def predicate(directory): exit = os.path.join(directory, "exit") return os.path.exists(exit) is exited return (os.path.join(root, d) for d in by_t if predicate(d))