def test_touch(self): datefile = Datefile('/tmp', TEST_FILE) actual = datefile.timestamp after = datetime.utcnow() datefile.touch() ok_(datefile.timestamp > actual) ok_(actual < after)
def test_should_run(self): # without last run file eq_(pid.should_run(self.pid_dir, self.job.rule), True) print 'hee --> %s %s' % (self.pid_dir,self.job.rule_name) self._make_temp_pid_file() # with last run file that's new d = Datefile(self.pid_dir, "%s.last_run" % self.job.rule_name, timestamp=datetime.utcnow()) print 'yo --> %s' % d.timestamp eq_(pid.should_run(self.pid_dir, self.job.rule), False) # with last run file that's old Datefile(self.pid_dir, "%s.last_run" % self.job.rule_name, timestamp=Datefile.EPOCH) eq_(pid.should_run(self.pid_dir, self.job.rule), False) os.remove('%s/%s.pid' % (self.pid_dir, self.job.rule_name)) # right date, with no pid Datefile(self.pid_dir, "%s.last_run" % self.job.rule_name, timestamp=Datefile.EPOCH) eq_(pid.should_run(self.pid_dir, self.job.rule), True)
def should_run(pid_dir, rule): if not os.path.exists(get_pid_path(pid_dir, rule)): last_run = Datefile(pid_dir, "%s.last_run" % rule.name) if last_run.is_older_than(rule.time_delta): return True else: log.debug('Skipping job: %s (last: %s)', rule.name, last_run) return False
def should_run(pid_dir, rule): last_run = Datefile(pid_dir, "%s.last_run" % rule.name) if not os.path.exists(get_pid_path(pid_dir, rule)) and \ last_run.is_older_than(rule.time_delta): return True else: log.debug('Skipping job: %s (last: %s)', rule.name, last_run) return False
def test_oclock_ran_yesterday(self): self.delete() now = datetime.utcnow() if now.hour == 23: now -= timedelta(hours=1) before = now - timedelta(hours=26) datefile = Datefile('/tmp', TEST_FILE, timestamp=before) ok_(datefile.is_older_than({'oclock': now.hour})) ok_(not datefile.is_older_than({'oclock': now.hour + 1}))
def should_run(pid_dir, rule): last_run = Datefile(pid_dir, "%s.last_run" % rule.name) if not os.path.exists(get_pid_path(pid_dir, rule)): if last_run.is_older_than(rule.time_delta): return True elif rule.retry: if os.path.exists(os.path.join(pid_dir, "%s.next_retry" % rule.name)): next_retry = Datefile(pid_dir, "%s.next_retry" % rule.name) now = datetime.utcnow() if now > datetime.strptime(str(next_retry), '%Y-%m-%d %H:%M:%S'): return True log.debug('Skipping job: %s (last: %s)', rule.name, last_run) return False
def should_run(pid_dir, rule): last_run = Datefile(pid_dir, "%s.last_run" % rule.name) if not os.path.exists(get_pid_path(pid_dir, rule)): if last_run.is_older_than(rule.time_delta): return True elif rule.retry: if os.path.exists( os.path.join(pid_dir, "%s.next_retry" % rule.name)): next_retry = Datefile(pid_dir, "%s.next_retry" % rule.name) now = datetime.utcnow() if now > datetime.strptime(str(next_retry), '%Y-%m-%d %H:%M:%S'): return True log.debug('Skipping job: %s (last: %s)', rule.name, last_run) return False
def test_is_older_than(self): self.delete() before = datetime.utcnow() datefile = Datefile('/tmp', TEST_FILE) ok_(datefile.is_older_than({'seconds': 2})) ok_(datefile.is_older_than({'days': 2})) # test will stop working on Jan 1, 2170 ok_(not datefile.is_older_than({'days': 73000})) datefile.touch(before) time.sleep(3) ok_(datefile.is_older_than({'seconds': 2})) ok_(not datefile.is_older_than({'minutes': 5}))
def create_next_retry(pid_dir, rule): # we should retry this rule in rule.retry_delay number of hours from datetime import timedelta Datefile(pid_dir, "%s.next_retry" % rule.name, timestamp=datetime.utcnow() + timedelta(0, 0, 0, 0, 0, int(rule.retry_delay), 0))
def test_creation(self): # ensure newly created Datefiles are 'old' self.delete() after = datetime.utcnow() datefile = Datefile('/tmp', TEST_FILE) actual = datefile.timestamp ok_(after > actual) ok_(os.path.exists(os.path.join('/tmp', TEST_FILE)))
def create_failed(pid_dir, rule): Datefile(pid_dir, "%s.failed" % rule.name, timestamp=datetime.utcnow()) next_retry = os.path.join(pid_dir, "%s.next_retry" % rule.name) if os.path.exists(next_retry): os.unlink(next_retry)
def create_last_run(pid_dir, rule): Datefile(pid_dir, "%s.last_run" % rule.name, timestamp=datetime.utcnow())
def test_reopening(self): # test overriding the timestamp before = datetime.utcnow() datefile = Datefile('/tmp', TEST_FILE, timestamp=datetime.utcnow()) actual = datefile.timestamp ok_(before < actual)