Class: ActiveJob::Status::Storage

Inherits:
Object
  • Object
show all
Defined in:
lib/activejob-status/storage.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Storage

Returns a new instance of Storage.



6
7
8
9
10
11
# File 'lib/activejob-status/storage.rb', line 6

def initialize(options = {})
  options.assert_valid_keys(:expires_in, :throttle_interval)

  @expires_in = options[:expires_in]
  @throttle = ActiveJob::Status::Throttle.new(options[:throttle_interval])
end

Instance Method Details

#delete(job) ⇒ Object



42
43
44
# File 'lib/activejob-status/storage.rb', line 42

def delete(job)
  store.delete(key(job))
end

#job_id(job) ⇒ Object



17
18
19
# File 'lib/activejob-status/storage.rb', line 17

def job_id(job)
  job.is_a?(String) ? job : job.job_id
end

#key(job) ⇒ Object



21
22
23
# File 'lib/activejob-status/storage.rb', line 21

def key(job)
  "activejob:status:#{job_id(job)}"
end

#read(job) ⇒ Object



25
26
27
# File 'lib/activejob-status/storage.rb', line 25

def read(job)
  store.read(key(job)) || {}
end

#storeObject



13
14
15
# File 'lib/activejob-status/storage.rb', line 13

def store
  @store ||= ActiveJob::Status.store
end

#update(job, message, force: false) ⇒ Object



35
36
37
38
39
40
# File 'lib/activejob-status/storage.rb', line 35

def update(job, message, force: false)
  @throttle.wrap(force: force) do
    message = read(job).merge(message)
    store.write(key(job), message, expires_in: @expires_in)
  end
end

#write(job, message, force: false) ⇒ Object



29
30
31
32
33
# File 'lib/activejob-status/storage.rb', line 29

def write(job, message, force: false)
  @throttle.wrap(force: force) do
    store.write(key(job), message, expires_in: @expires_in)
  end
end