Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 59: Support crontab ranges 'x-y' and 'x/y' #47

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 28 additions & 9 deletions lib/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,8 @@ def _crontab(self, crontab):
if not next_event:
next_event = self._parse_month(crontab, offset=1) # next month
return next_event
except:
logger.error("Error parsing crontab: {}".format(crontab))
except Exception as e:
logger.error('Error parsing crontab "{}": {}'.format(crontab, e))
return datetime.datetime.now(tzutc()) + dateutil.relativedelta.relativedelta(years=+10)

def _parse_month(self, crontab, offset=0):
Expand All @@ -378,7 +378,7 @@ def _parse_month(self, crontab, offset=0):
day_range = day_range + self._range(day, 0o1, mdays)
else:
day_range = self._range(day, 0o1, mdays)
# combine the differnt ranges
# combine the different ranges
event_range = sorted([str(day) + '-' + str(hour) + '-' + str(minute) for minute in minute_range for hour in hour_range for day in day_range])
if offset: # next month
next_event = event_range[0]
Expand Down Expand Up @@ -472,16 +472,35 @@ def _sun(self, crontab):
def _range(self, entry, low, high):
result = []
item_range = []
if entry == '*':
item_range = list(range(low, high + 1))
else:

# Check for multiple items and process each item recursively
if ',' in entry:
for item in entry.split(','):
item = int(item)
result.extend(self._range(item, low, high))

# Check for intervals, e.g. "*/2", "9-17/2"
elif '/' in entry:
spec_range, interval = entry.split('/')
logger.error('Cron spec interval {} {}'.format(entry, interval))
result = self._range(spec_range, low, high)[::int(interval)]

# Check for numeric ranges, e.g. "9-17"
elif '-' in entry:
spec_low, spec_high = entry.split('-')
result = self._range('*', int(spec_low), int(spec_high))

# Process single item
else:
if entry == '*':
item_range = list(range(low, high + 1))
else:
item = int(entry)
if item > high: # entry above range
item = high # truncate value to highest possible
item_range.append(item)
for entry in item_range:
result.append('{:02d}'.format(entry))
for entry in item_range:
result.append('{:02d}'.format(entry))

return result

def _day_range(self, days):
Expand Down