Simple monitoring status for ActiveJob, independent of your queuing backend or cache storage.
gem 'activejob-status', github: 'inkstak/activejob-status'
By default, ActiveJob::Status use the Rails.cache to store data.
You can use any compatible ActiveSupport::Cache::Store you want (memory, memcache, redis, ..)
or any storage responding to read/write/delete
Note : In Rails 5,
Rails.cachedefaults toActiveSupport::Cache::NullStorewhich will result in empty status. Setting a cache store for ActiveJob::Status is therefore mandatory.
Set your store:
# config/initializers/activejob_status.rb
# By default
ActiveJob::Status.store = Rails.cache
# Set another storage
ActiveJob::Status.store = ActiveSupport::Cache::MemoryStore.new
# Use the ActiveSupport#lookup_store syntax
ActiveJob::Status.store = :redis_store
Include the ActiveJob::Status module in your jobs.
class MyJob < ActiveJob::Base
include ActiveJob::Status
end
The module introduces two methods:
statusto directly read/update statusprogressto implement a progress status
class MyJob < ActiveJob::Base
include ActiveJob::Status
def perform
status.update(foo: false)
progress.total = 1000
1000.time do |i|
progress.increment
end
status.update(foo: true)
end
end
Check the status a job
job = MyJob.perform_later
status = ActiveJob::Status.get(job)
# => { status: :queued }
You can also use the job_id
status = ActiveJob::Status.get('d11b64e6-8631-4118-ae76-e19376769171')
# => { status: :queued }
Follow the progression of your job
status
# => { status: :working, progress: 100, total: 1000, foo: false }
status.working?
# => true
status.progress
# => 0.1
status[:foo]
# => false
until it's completed
status
# => { status: :completed, progress: 1000, total: 1000, foo: true }
status.completed?
# => true
status.progress
# => 1
status[:foo]
# => true