Module: Marty::BackgroundJob::UpdateSchedule

Defined in:
app/services/marty/background_job/update_schedule.rb

Class Method Summary collapse

Class Method Details

.call(id:, job_class:) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
# File 'app/services/marty/background_job/update_schedule.rb', line 4

def self.call(id:, job_class:)
  model = Marty::BackgroundJob::Schedule.find_by(id: id)

  if model.blank? || model.off?
    return remove_schedule(
      schedule_id: id,
      job_class: job_class
    )
  end

  return schedule(schedule_obj: model) if model.on?
end

.remove_schedule(schedule_id:, job_class:) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'app/services/marty/background_job/update_schedule.rb', line 17

def self.remove_schedule(schedule_id:, job_class:)
  klass = job_class.constantize
  return true unless klass.respond_to?(:remove_schedule)

  klass.remove_schedule(Delayed::Job.find_by(schedule_id: schedule_id))

  true
rescue NameError
  false
end

.schedule(schedule_obj:) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'app/services/marty/background_job/update_schedule.rb', line 28

def self.schedule(schedule_obj:)
  klass = schedule_obj.job_class.constantize

  return false unless klass.respond_to?(:schedule)

  klass.schedule(schedule_obj: schedule_obj)

  true
rescue NameError
  false
end