Module: DelayedCron::Scheduling

Included in:
DelayedCron
Defined in:
lib/delayed_cron/scheduling.rb

Instance Method Summary collapse

Instance Method Details

#add_interval(options) ⇒ Object



29
30
31
32
33
# File 'lib/delayed_cron/scheduling.rb', line 29

def add_interval(options)
  date = beginning_of_day(options[:interval].to_i)
  options[:interval] = adjust_interval(date, options[:at])
  options
end

#adjust_interval(date, time_string) ⇒ Object



45
46
47
# File 'lib/delayed_cron/scheduling.rb', line 45

def adjust_interval(date, time_string)
  adjusted_date(date, time_string).to_i - Time.now.to_i
end

#adjusted_date(date, time_string) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/delayed_cron/scheduling.rb', line 49

def adjusted_date(date, time_string)
  time = parse_time(time_string.split(/:|\ /).map(&:to_i))
  DateTime.civil(
    date.year,
    date.month,
    date.day,
    time[:hours],
    time[:mins],
    time[:secs],
    Rational(time[:tz], 2400)
  )
end

#beginning_of_day(seconds) ⇒ Object



41
42
43
# File 'lib/delayed_cron/scheduling.rb', line 41

def beginning_of_day(seconds)
  (Time.now + seconds).beginning_of_day
end

#parse_options(options) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/delayed_cron/scheduling.rb', line 15

def parse_options(options)
  original_options = options
  if at = options[:at]
    options = if at.is_a?(Array)
      at.map do |at_option|
        add_interval(original_options.merge(at: at_option))
      end
    else
      add_interval(options)
    end
  end
  options
end

#parse_time(time_array) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/delayed_cron/scheduling.rb', line 62

def parse_time(time_array)
  {
    hours: time_array[0],
    mins:  time_array[1],
    secs:  time_array[2] || 0,
    tz:    time_array[3] || Time.now.strftime("%z").to_i
  }
end

#schedule(klass, method_name, options) ⇒ Object



4
5
6
7
8
9
10
11
12
13
# File 'lib/delayed_cron/scheduling.rb', line 4

def schedule(klass, method_name, options)
  parsed_options = parse_options(options)
  if parsed_options.is_a?(Array)
    parsed_options.each do |opts|
      processor.enqueue_delayed_cron(klass, method_name, opts)
    end
  else
    processor.enqueue_delayed_cron(klass, method_name, parsed_options)
  end
end

#timing_opts(interval, options_at) ⇒ Object



35
36
37
38
39
# File 'lib/delayed_cron/scheduling.rb', line 35

def timing_opts(interval, options_at)
  timing_opts = { interval: interval }
  timing_opts.merge!(at: options_at) if options_at.present?
  timing_opts
end