Class: Cronin::CronTask

Inherits:
Object
  • Object
show all
Defined in:
lib/cronin/cron_task.rb

Instance Method Summary collapse

Constructor Details

#initialize(entry) ⇒ CronTask

Returns a new instance of CronTask.



6
7
8
# File 'lib/cronin/cron_task.rb', line 6

def initialize(entry)
  @parsed_entry = Parser.new(entry).parse
end

Instance Method Details

#daily_execution_hours(options = {}) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/cronin/cron_task.rb', line 79

def daily_execution_hours(options = {})
  options = {:start_hour => 0, :start_minute => 0, :end_hour => 23, :end_minute => 59}.merge(options)
  execution_hours = []
  hours   = @parsed_entry[:hours].timing
  hours   = hours.select {|hour| options[:start_hour] <= hour }
  hours   = hours.select {|hour| options[:end_hour] >= hour }
  minutes = @parsed_entry[:minutes].timing 
  hours.each do |hour|
    _minutes = if hour == options[:start_hour]
      minutes.select { |minute| options[:start_minute] <= minute }
    else
      minutes
    end
    _minutes = if hour == options[:end_hour]
      _minutes.select { |minute| options[:end_minute] >= minute }
    else
      _minutes
    end
    _minutes.each do |minute|
      execution_hours << Cronin::Hour.new(hour,minute)
    end
  end
  execution_hours
end

#execution_daysObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/cronin/cron_task.rb', line 47

def execution_days
  get_days.select do |day|
    wday_mday = if both_wday_and_mday_are_restricted?
      (@parsed_entry[:week_days].timing.include?(day.wday) ||
      @parsed_entry[:month_days].timing.include?(day.mday))
    else
      (@parsed_entry[:week_days].timing.include?(day.wday) &&
      @parsed_entry[:month_days].timing.include?(day.mday))
    end
    # maybe it is to long?
    # @parsed_entry[:week_days].timing
    wday_mday && @parsed_entry[:months].timing.include?(day.month)
  end
end

#executions(start_time, end_time) ⇒ Object



10
11
12
13
14
15
# File 'lib/cronin/cron_task.rb', line 10

def executions(start_time, end_time)
  raise 'end date can not be earlier then start date' if end_time < start_time
  @start_time = start_time
  @end_time   = end_time
  get_executions
end

#get_daysObject



69
70
71
72
73
74
75
76
77
# File 'lib/cronin/cron_task.rb', line 69

def get_days
  start_day = Date.parse(time_to_string(@start_time))
  end_day   = Date.parse(time_to_string(@end_time))
  if start_day == end_day
    return [start_day]   
  else
    return (start_day..end_day).to_a
  end
end

#get_executionsObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/cronin/cron_task.rb', line 17

def get_executions
  start_day  = Date.parse(time_to_string(@start_time))
  end_day    = Date.parse(time_to_string(@end_time))
  executions = []
  execution_days.each do |day|
    if start_day == day
      execution_hours = daily_execution_hours(:start_hour => @start_time.hour, :start_minute => @start_time.min)
    end
    if end_day == day
      execution_hours = daily_execution_hours(:end_hour => @end_time.hour, :end_minute => @end_time.min)
    end
    if end_day == start_day
      options = {
        :start_hour   => @start_time.hour,
        :start_minute => @start_time.min,
        :end_hour     => @end_time.hour, 
        :end_minute   => @end_time.min
      }
      execution_hours = daily_execution_hours(options)
    end
    if end_day != day && start_day != day
      execution_hours = daily_execution_hours
    end
    execution_hours.each do |hour|
      executions << Time.mktime(day.year,day.month,day.day,hour.hour,hour.minute) 
    end
  end 
  executions
end