Class: ElasticWhenever::Schedule

Inherits:
Object
  • Object
show all
Defined in:
lib/elastic_whenever/schedule.rb

Defined Under Namespace

Modules: WheneverNumeric Classes: UnsupportedFrequencyException

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, verbose, variables) ⇒ Schedule

Returns a new instance of Schedule.



50
51
52
53
54
55
56
57
58
59
# File 'lib/elastic_whenever/schedule.rb', line 50

def initialize(file, verbose, variables)
  @environment = "production"
  @verbose = verbose
  @tasks = []
  @chronic_options = {}
  @bundle_command = "bundle exec"

  variables.each { |var| set(var[:key], var[:value]) }
  instance_eval(File.read(file), file)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



150
151
152
# File 'lib/elastic_whenever/schedule.rb', line 150

def method_missing(name, *args)
  Logger.instance.warn("Skipping unsupported method: #{name}")
end

Instance Attribute Details

#bundle_commandObject (readonly)

Returns the value of attribute bundle_command.



5
6
7
# File 'lib/elastic_whenever/schedule.rb', line 5

def bundle_command
  @bundle_command
end

#chronic_optionsObject (readonly)

Returns the value of attribute chronic_options.



4
5
6
# File 'lib/elastic_whenever/schedule.rb', line 4

def chronic_options
  @chronic_options
end

#environmentObject (readonly)

Returns the value of attribute environment.



6
7
8
# File 'lib/elastic_whenever/schedule.rb', line 6

def environment
  @environment
end

#tasksObject (readonly)

Returns the value of attribute tasks.



3
4
5
# File 'lib/elastic_whenever/schedule.rb', line 3

def tasks
  @tasks
end

Instance Method Details

#every(frequency, options = {}, &block) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/elastic_whenever/schedule.rb', line 65

def every(frequency, options = {}, &block)
  @tasks << Task.new(@environment, @verbose, @bundle_command, schedule_expression(frequency, options)).tap do |task|
    task.instance_eval(&block)
  end
rescue UnsupportedFrequencyException => exn
  Logger.instance.warn(exn.message)
end

#schedule_expression(frequency, options) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/elastic_whenever/schedule.rb', line 73

def schedule_expression(frequency, options)
  opts =  { :now => Time.new(2017, 12, 1, 0, 0, 0) }.merge(@chronic_options)
  time = Chronic.parse(options[:at], opts) || Time.new(2017, 12, 1, 0, 0, 0)

  case frequency
  when 1.minute
    "cron(* * * * ? *)"
  when :hour, 1.hour
    "cron(#{time.min} * * * ? *)"
  when :day, 1.day
    "cron(#{time.min} #{time.hour} * * ? *)"
  when :month, 1.month
    "cron(#{time.min} #{time.hour} #{time.day} * ? *)"
  when :year, 1.year
    "cron(#{time.min} #{time.hour} #{time.day} #{time.month} ? *)"
  when :sunday
    "cron(#{time.min} #{time.hour} ? * 1 *)"
  when :monday
    "cron(#{time.min} #{time.hour} ? * 2 *)"
  when :tuesday
    "cron(#{time.min} #{time.hour} ? * 3 *)"
  when :wednesday
    "cron(#{time.min} #{time.hour} ? * 4 *)"
  when :thursday
    "cron(#{time.min} #{time.hour} ? * 5 *)"
  when :friday
    "cron(#{time.min} #{time.hour} ? * 6 *)"
  when :saturday
    "cron(#{time.min} #{time.hour} ? * 7 *)"
  when :weekend
    "cron(#{time.min} #{time.hour} ? * 1,7 *)"
  when :weekday
    "cron(#{time.min} #{time.hour} ? * 2-6 *)"
  when 1.second...1.minute
    raise UnsupportedFrequencyException.new("Time must be in minutes or higher. Ignore this task.")
  when 1.minute...1.hour
    step = (frequency / 60).round
    min = []
    (60 % step == 0 ? 0 : step).step(59, step) { |i| min << i }
    "cron(#{min.join(",")} * * * ? *)"
  when 1.hour...1.day
    step = (frequency / 60 / 60).round
    hour = []
    (24 % step == 0 ? 0 : step).step(23, step) { |i| hour << i }
    "cron(#{time.min} #{hour.join(",")} * * ? *)"
  when 1.day...1.month
    step = (frequency / 24 / 60 / 60).round
    day = []
    (step <= 16 ? 1 : step).step(30, step) { |i| day << i }
    "cron(#{time.min} #{time.hour} #{day.join(",")} * ? *)"
  when 1.month...12.months
    step = (frequency / 30 / 24 / 60 / 60).round
    month = []
    (step <= 6 ? 1 : step).step(12, step) { |i| month << i }
    "cron(#{time.min} #{time.hour} #{time.day} #{month.join(",")} ? *)"
  when 12.months...Float::INFINITY
    raise UnsupportedFrequencyException.new("Time must be in months or lower. Ignore this task.")
  # cron syntax
  when /^((\*?[\d\/,\-]*)\s*){5}$/
    min, hour, day, mon, week, year = frequency.split(" ")
    # You can't specify the Day-of-month and Day-of-week fields in the same Cron expression.
    # If you specify a value in one of the fields, you must use a ? (question mark) in the other.
    week.gsub!("*", "?") if day != "?"
    day.gsub!("*", "?") if week != "?"
    # cron syntax:          sunday -> 0
    # scheduled expression: sunday -> 1
    week.gsub!(/(\d)/) { Integer($1) + 1 }
    year = year || "*"
    "cron(#{min} #{hour} #{day} #{mon} #{week} #{year})"
  # schedule expression syntax
  when /^((\*?\??L?W?[\d\/,\-]*)\s*){6}$/
    "cron(#{frequency})"
  else
    raise UnsupportedFrequencyException.new("`#{frequency}` is not supported option. Ignore this task.")
  end
end

#set(key, value) ⇒ Object



61
62
63
# File 'lib/elastic_whenever/schedule.rb', line 61

def set(key, value)
  instance_variable_set("@#{key}", value) unless key == 'tasks'
end