Commits

Max Arnold  committed 2c37c51

Added wayback metod to return last match for each job

  • Participants
  • Parent commits 39c4f69

Comments (0)

Files changed (1)

         # Do something with task history!
         pass
 
+    def wayback(self, wbtime=86400, tnow=None):
+        """Look back in time (wbtime seconds maximum) and find last matching jobs
+
+        Useful right after instantiation if previous task should be executed again.
+        Does not affect internal state
+        """
+        wbtasks = set()
+        if tnow is None: tnow = int(time.mktime(datetime.today().timetuple()))
+        for j in self.jobs:
+            for ts in xrange(tnow, tnow-wbtime-1, -1):
+                if pycron.match_job(j[0], ts):
+                    wbtasks.add((j[1], ts))
+                    break
+
+        return tuple([t[0] for t in sorted(wbtasks, key=itemgetter(1, 0))])
+
     def get_matched_jobs(self, tnow=None):
         """Return tuple of matched task identifiers
         """
 if __name__ == '__main__':
     c = pycron()
     c.add_job('*/5 * * * * *', 'job_id1', 'job_info1')
+    c.add_job('* * 0 * * *', 'job_id2', 'job_info2')
+    print c.wayback()
+
     while True:
         time.sleep(1)
         print datetime.today(), c.get_matched_jobs()