Module: DelayedCron

Defined in:
lib/delayed_cron.rb,
lib/delayed_cron/jobs.rb,
lib/delayed_cron/railtie.rb,
lib/delayed_cron/version.rb,
lib/delayed_cron/jobs/resque.rb,
lib/delayed_cron/jobs/sidekiq.rb,
lib/delayed_cron/jobs/delayed_job.rb,
lib/delayed_cron/jobs/sucker_punch.rb

Defined Under Namespace

Modules: ClassMethods, Glue, Jobs Classes: Railtie

Constant Summary collapse

VERSION =
"0.2.5"

Class Method Summary collapse

Class Method Details

.adjust_interval(date, time_string) ⇒ Object



60
61
62
# File 'lib/delayed_cron.rb', line 60

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

.adjusted_date(date, time_string) ⇒ Object



64
65
66
67
# File 'lib/delayed_cron.rb', line 64

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



56
57
58
# File 'lib/delayed_cron.rb', line 56

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

.define_cron_jobsObject



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

def define_cron_jobs
  return false unless cron_jobs.present?
  cron_jobs.each do |job|
    obj         = job
    job_is_hash = job.is_a?(Hash)
    job         = job_is_hash ? obj[:job] : job
    interval    = job_is_hash ? obj[:interval] : default_interval
    options_at  = job_is_hash ? obj[:at] : nil
    klass, name = job.split(".")
    # TODO: raise error if interval is not set
    options     = timing_opts(interval, options_at)
    DelayedCron.schedule(klass, name, options)
  end
end

.parse_time(time_array) ⇒ Object



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

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

.process_job(klass, method_name, options) ⇒ Object



50
51
52
53
54
# File 'lib/delayed_cron.rb', line 50

def process_job(klass, method_name, options)
  # TODO: add ability to send args to klass method
  klass.constantize.send(method_name)
  schedule(klass, method_name, options)
end

.processorObject



30
31
32
33
34
35
# File 'lib/delayed_cron.rb', line 30

def processor
  return DelayedCron::Jobs::DelayedJob  if defined? ::Delayed::Job
  return DelayedCron::Jobs::Resque      if defined? ::Resque
  return DelayedCron::Jobs::Sidekiq     if defined? ::Sidekiq
  return DelayedCron::Jobs::SuckerPunch if defined? ::SuckerPunch
end

.schedule(klass, method_name, options) ⇒ Object



37
38
39
40
41
42
# File 'lib/delayed_cron.rb', line 37

def schedule(klass, method_name, options)
  if options[:at]
    options[:interval] = adjust_interval(beginning_of_day(options[:interval].to_i), options[:at])
  end
  processor.enqueue_delayed_cron(klass, method_name, options)
end

.setup {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

  • _self (DelayedCron)

    the object that the method was called on



10
11
12
13
# File 'lib/delayed_cron.rb', line 10

def setup
  yield self
  define_cron_jobs
end

.timing_opts(interval, options_at) ⇒ Object



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

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