Class: Drippings::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/drippings/client.rb

Defined Under Namespace

Classes: Drip

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



5
6
7
# File 'lib/drippings/client.rb', line 5

def initialize
  @drips = {}
end

Instance Method Details

#kickoffObject



9
10
11
12
13
# File 'lib/drippings/client.rb', line 9

def kickoff
  @drips.each do |name, _|
    ScheduleJob.perform_later(name)
  end
end

#register(name, job, scope, wait_until: nil, time_zone: nil, options: {}) ⇒ Object

Raises:

  • (ArgumentError)


31
32
33
34
35
36
37
38
39
# File 'lib/drippings/client.rb', line 31

def register(name, job, scope, wait_until: nil, time_zone: nil, options: {})
  raise ArgumentError, "A drip has already been registered for #{name}" if @drips[name].present?
  raise ArgumentError, 'Job must be a subclass of Drippings::ProcessJob' unless job < Drippings::ProcessJob
  if wait_until.present? && time_zone.nil?
    raise ArgumentError, 'time_zone must be defined if providing a wait_until'
  end

  @drips[name] = Drip.new(job, scope, wait_until, time_zone, options)
end

#schedule(name) ⇒ Object



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

def schedule(name)
  drip = @drips[name]
  scope = drip.scope.call
  job = drip.job
  raw_wait_until = drip.wait_until
  time_zone = drip.time_zone

  Scheduling.dedup(scope, name).find_in_batches do |batch|
    batch.each do |resource|
      scheduling = Drippings::Scheduling.create!(name: name, resource: resource)
      wait_until = raw_wait_until.present? ? wait_until(resource, raw_wait_until, time_zone) : nil
      job.set(wait_until: wait_until).perform_later(scheduling, **drip.options)
    end
  end
end