Class: Jobler::JobScheduler

Inherits:
Object
  • Object
show all
Defined in:
lib/jobler/job_scheduler.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ JobScheduler

Returns a new instance of JobScheduler.



11
12
13
14
15
16
# File 'lib/jobler/job_scheduler.rb', line 11

def initialize(args)
  @controller = args[:controller]
  @jobler_type = args.fetch(:jobler_type)
  @job_args = args[:job_args]
  @locale = args[:locale]
end

Instance Attribute Details

#jobObject (readonly)

Returns the value of attribute job.



2
3
4
# File 'lib/jobler/job_scheduler.rb', line 2

def job
  @job
end

Class Method Details

.create!(args) ⇒ Object



4
5
6
7
8
9
# File 'lib/jobler/job_scheduler.rb', line 4

def self.create!(args)
  scheduler = Jobler::JobScheduler.new(args)
  scheduler.create_job
  scheduler.perform_job_later
  scheduler
end

Instance Method Details

#create_jobObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/jobler/job_scheduler.rb', line 18

def create_job
  @job = Jobler::Job.new(
    jobler_type: @jobler_type,
    locale: @locale.presence || I18n.locale,
    parameters: YAML.dump(@job_args)
  )

  if @controller
    @job.assign_attributes(
      host: @controller.request.host,
      port: @controller.request.port,
      protocol: @controller.request.protocol
    )
  end

  @job.save!
end

#perform_job_laterObject



36
37
38
# File 'lib/jobler/job_scheduler.rb', line 36

def perform_job_later
  Jobler::JobRunner.perform_later(job.id)
end