Class: SayWhen::Scheduler

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/say_when/scheduler.rb

Instance Method Summary collapse

Methods included from Utils

#load_strategy

Instance Method Details

#extract_data(job) ⇒ Object



81
82
83
# File 'lib/say_when/scheduler.rb', line 81

def extract_data(job)
  job[:data] if job && job.is_a?(Hash)
end

#extract_job_class(job) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/say_when/scheduler.rb', line 65

def extract_job_class(job)
  if job.is_a?(Hash)
    job[:class] || job[:job_class]
  elsif job.is_a?(Class)
    job.name
  elsif job.is_a?(String)
    job
  end
end

#extract_job_method(job) ⇒ Object



75
76
77
78
79
# File 'lib/say_when/scheduler.rb', line 75

def extract_job_method(job)
  if job.is_a?(Hash)
    job[:method] || job[:job_method]
  end || 'execute'
end

#extract_scheduled(job) ⇒ Object



61
62
63
# File 'lib/say_when/scheduler.rb', line 61

def extract_scheduled(job)
  job[:scheduled] if job.is_a?(Hash)
end

#job_options(job) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/say_when/scheduler.rb', line 48

def job_options(job)
  opts = {
    scheduled: extract_scheduled(job),
    job_class: extract_job_class(job),
    job_method: extract_job_method(job),
    data: extract_data(job)
  }
  unless opts[:scheduled] || opts[:job_class]
    raise "No job class or scheduled option: #{job.inspect}\nopts: #{opts.inspect}"
  end
  opts
end

#loggerObject



93
94
95
# File 'lib/say_when/scheduler.rb', line 93

def logger
  SayWhen.logger
end

#schedule(job) ⇒ Object

When passing in a job, can be a Hash, String, or Class Hash: { class: ‘<class name>’ } or { job_class: ‘<class name>’ } String: ‘<class name>’ Class: <job class>



13
14
15
# File 'lib/say_when/scheduler.rb', line 13

def schedule(job)
  storage.create(job)
end

#schedule_cron(expression, job) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/say_when/scheduler.rb', line 17

def schedule_cron(expression, job)
  time_zone = if job.is_a?(Hash)
    job.delete(:time_zone)
  end || 'UTC'
  options = job_options(job)
  options[:trigger_strategy] = :cron
  options[:trigger_options]  = { expression: expression, time_zone: time_zone }
  schedule(options)
end

#schedule_in(after, job = {}) ⇒ Object



41
42
43
44
45
46
# File 'lib/say_when/scheduler.rb', line 41

def schedule_in(after, job = {})
  options = job_options(job)
  options[:trigger_strategy] = 'once'
  options[:trigger_options]  = { at: (Time.now + after)}
  schedule(options)
end

#schedule_instance(next_at_method = 'next_fire_at', job = {}) ⇒ Object



27
28
29
30
31
32
# File 'lib/say_when/scheduler.rb', line 27

def schedule_instance(next_at_method = 'next_fire_at', job = {})
  options = job_options(job)
  options[:trigger_strategy] = 'instance'
  options[:trigger_options]  = { next_at_method: next_at_method }
  schedule(options)
end

#schedule_once(time, job = {}) ⇒ Object



34
35
36
37
38
39
# File 'lib/say_when/scheduler.rb', line 34

def schedule_once(time, job = {})
  options = job_options(job)
  options[:trigger_strategy] = 'once'
  options[:trigger_options]  = { at: time}
  schedule(options)
end

#storageObject



89
90
91
# File 'lib/say_when/scheduler.rb', line 89

def storage
  @storage ||= load_strategy(:storage, SayWhen.options[:storage_strategy])
end

#storage=(s) ⇒ Object



85
86
87
# File 'lib/say_when/scheduler.rb', line 85

def storage=(s)
  @storage = s
end