Browse Source

crontab: Added support for using day names for day_of_week ("SUN", "sunday", "sun", etc)

Ask Solem 15 years ago
parent
commit
2e5af1fe7f
2 changed files with 13 additions and 1 deletions
  1. 12 0
      celery/task/base.py
  2. 1 1
      celery/tests/test_task.py

+ 12 - 0
celery/task/base.py

@@ -740,6 +740,9 @@ class crontab(schedule):
         represents the day of week that execution should occur.
 
     """
+    daynames = "sun", "mon", "tue", "wed", "thu", "fri", "sat"
+    weekdays = dict((name, dow) for name, dow in zip(daynames, range(7)))
+
     def __init__(self, minute=None, hour=None, day_of_week=None,
             nowfun=datetime.now):
         self.hour = hour                  # (0 - 23)
@@ -747,6 +750,15 @@ class crontab(schedule):
         self.day_of_week = day_of_week    # (0 - 6) (Sunday=0)
         self.nowfun = nowfun
 
+        if isinstance(self.day_of_week, basestring):
+            abbreviation = self.day_of_week[0:3].lower()
+            try:
+                self.day_of_week = self.weekdays[abbreviation]
+            except KeyError:
+                # Show original day name in exception, instead of abbr.
+                raise KeyError(self.day_of_week)
+
+
     def remaining_estimate(self, last_run_at):
         # remaining_estimate controls the frequency of scheduler
         # ticks. The scheduler needs to wake up every second in this case.

+ 1 - 1
celery/tests/test_task.py

@@ -483,7 +483,7 @@ class DailyPeriodic(task.PeriodicTask):
 
 
 class WeeklyPeriodic(task.PeriodicTask):
-    run_every = task.crontab(hour=7, minute=30, day_of_week=4)
+    run_every = task.crontab(hour=7, minute=30, day_of_week="thursday")
 
 
 def patch_crontab_nowfun(cls, retval):