Skip to content

Commit

Permalink
Allow literal days of week and months in raw cron syntax
Browse files Browse the repository at this point in the history
Last two fields of cron syntax allow days of week and months to be
entered as string literals instead of numbers.

Make the cron regex a little more strict so it won't match single words
like 'SUN' or 'Jan' as cron and allow the list of month and day
names as found in vixie cron.
  • Loading branch information
gregoryp authored and benlangfeld committed Nov 19, 2017
1 parent b05a574 commit 1e127f5
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

* Allow setting the path within which Capistrano will execute whenever. [Samuel Johnson](https://github.com/javan/whenever/pull/619)

* Allow the use of string literals for month and day-of-week in raw cron syntax.. [Potamianos Gregory](https://github.com/javan/whenever/pull/711)

### 0.9.7 / June 14, 2016

* Restore compatibility with Capistrano v3; it has a bug which we have to work around [Ben Langfeld, Chris Gunther, Shohei Yamasaki]
Expand Down
9 changes: 5 additions & 4 deletions lib/whenever/cron.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
module Whenever
module Output
class Cron
DAYS = %w(sun mon tue wed thu fri sat)
MONTHS = %w(jan feb mar apr may jun jul aug sep oct nov dec)
KEYWORDS = [:reboot, :yearly, :annually, :monthly, :weekly, :daily, :midnight, :hourly]
REGEX = /^(@(#{KEYWORDS.join '|'})|((\*?[\d\/,\-]*)\s*){5})$/
REGEX = /^(@(#{KEYWORDS.join '|'})|((\*?[\d\/,\-]*)\s){3}(\*?([\d\/,\-]|(#{MONTHS.join '|'}))*\s)(\*?([\d\/,\-]|(#{DAYS.join '|'}))*))$/i

attr_accessor :time, :task

Expand Down Expand Up @@ -56,8 +58,7 @@ def time_in_cron_syntax

protected
def day_given?
months = %w(jan feb mar apr may jun jul aug sep oct nov dec)
@at_given.is_a?(String) && (months.any? { |m| @at_given.downcase.index(m) } || @at_given[/\d\/\d/])
@at_given.is_a?(String) && (MONTHS.any? { |m| @at_given.downcase.index(m) } || @at_given[/\d\/\d/])
end

def parse_symbol
Expand Down Expand Up @@ -146,7 +147,7 @@ def parse_as_string
return (timing << '1-5') * " " if string.downcase.index('weekday')
return (timing << '6,0') * " " if string.downcase.index('weekend')

%w(sun mon tue wed thu fri sat).each_with_index do |day, i|
DAYS.each_with_index do |day, i|
return (timing << i) * " " if string.downcase.index(day)
end

Expand Down
1 change: 1 addition & 0 deletions test/unit/cron_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ class CronParseRawTest < Whenever::TestCase
should "return the same cron sytax" do
crons = ['0 0 27-31 * *', '* * * * *', '2/3 1,9,22 11-26 1-6 *', '*/5 6-23 * * *',
"*\t*\t*\t*\t*",
'7 17 * * FRI', '7 17 * * Mon-Fri', '30 12 * Jun *', '30 12 * Jun-Aug *',
'@reboot', '@yearly', '@annually', '@monthly', '@weekly',
'@daily', '@midnight', '@hourly']
crons.each do |cron|
Expand Down

0 comments on commit 1e127f5

Please sign in to comment.