lib/openwfe/util/scheduler.rb in openwferu-scheduler-0.9.15.1128 vs lib/openwfe/util/scheduler.rb in openwferu-scheduler-0.9.15.1172

- old
+ new

@@ -141,10 +141,21 @@ # # The vanilla case for tags assume they are String instances, but nothing # prevents you from using anything else. The scheduler has no persistence # by itself, so no serialization issue. # + # + # Since OpenWFEru 0.9.16, a cron schedule can be set at the second level : + # + # scheduler.schedule "7 * * * * *" do + # puts "it's now the seventh second of the minute" + # end + # + # The OpenWFEru scheduler recognizes an optional first column for second + # scheduling. This column can, like for the other columns, specify a + # value ("7"), a list of values ("7,8,9,27") or a range ("7-12"). + # class Scheduler include MonitorMixin # # By default, the precision is 0.250, with means the scheduler @@ -176,11 +187,11 @@ end @exit_when_no_more_jobs = false @dont_reschedule_every = false - @last_cron_minute = -1 + @last_cron_second = -1 @stopped = true end # @@ -655,19 +666,19 @@ end @dont_reschedule_every = true if at_job_count < 1 end + # TODO : eventually consider running cron / pending + # job triggering in two different threads + # # cron jobs - if now.sec == 0 and now.min != @last_cron_minute - # - # only consider cron jobs at the second 0 of a - # minute + if now.sec != @last_cron_second - @last_cron_minute = now.min + @last_cron_second = now.sec #puts "step() @cron_jobs.size #{@cron_jobs.size}" @cron_jobs.each do |cron_id, cron_job| #puts "step() cron_id : #{cron_id}" @@ -843,10 +854,11 @@ # (man 5 crontab) file line. # class CronLine attr_reader \ + :seconds, :minutes, :hours, :days, :months, :weekdays @@ -855,66 +867,76 @@ super() items = line.split - if items.length != 5 + unless [ 5, 6 ].include?(items.length) raise \ - "cron '#{line}' string should hold 5 items, " + + "cron '#{line}' string should hold 5 or 6 items, " + "not #{items.length}" \ end - @minutes = parse_item(items[0], 0, 59) - @hours = parse_item(items[1], 0, 24) - @days = parse_item(items[2], 1, 31) - @months = parse_item(items[3], 1, 12) - @weekdays = parse_weekdays(items[4]) + offset = items.length - 5 - adjust_arrays() + @seconds = if offset == 1 + parse_item(items[0], 0, 59) + else + [ 0 ] + end + @minutes = parse_item(items[0+offset], 0, 59) + @hours = parse_item(items[1+offset], 0, 24) + @days = parse_item(items[2+offset], 1, 31) + @months = parse_item(items[3+offset], 1, 12) + @weekdays = parse_weekdays(items[4+offset]) + + #adjust_arrays() end def matches? (time) if time.kind_of?(Float) or time.kind_of?(Integer) time = Time.at(time) end + return false if no_match?(time.sec, @seconds) return false if no_match?(time.min, @minutes) return false if no_match?(time.hour, @hours) return false if no_match?(time.day, @days) return false if no_match?(time.month, @months) return false if no_match?(time.wday, @weekdays) true end # - # Returns an array of 5 arrays (minutes, hours, days, months, - # weekdays). + # Returns an array of 6 arrays (seconds, minutes, hours, days, + # months, weekdays). # This method is used by the cronline unit tests. # def to_array - [ @minutes, @hours, @days, @months, @weekdays ] + [ @seconds, @minutes, @hours, @days, @months, @weekdays ] end private # # adjust values to Ruby # - def adjust_arrays() - if @hours - @hours.each do |h| - h = 0 if h == 23 - end - end - if @weekdays - @weekdays.each do |wd| - wd = wd - 1 - end - end - end + #def adjust_arrays() + # @hours = @hours.collect { |h| + # if h == 24 + # 0 + # else + # h + # end + # } if @hours + # @weekdays = @weekdays.collect { |wd| + # wd - 1 + # } if @weekdays + #end + # + # dead code WDS = [ "mon", "tue", "wed", "thu", "fri", "sat", "sun" ] # # used by parse_weekday() @@ -945,10 +967,11 @@ [ i ] end def parse_list (item, min, max) + items = item.split(",") result = [] items.each do |i| i = Integer(i) i = min if i < min @@ -957,10 +980,11 @@ end result end def parse_range (item, min, max) + i = item.index("-") j = item.index("/") inc = 1 @@ -978,10 +1002,11 @@ else iend = Integer(item[i+1..-1]) end else # case */x + istart = min iend = max end istart = min if istart < min @@ -989,9 +1014,10 @@ result = [] value = istart while true + result << value value = value + inc break if value > iend end