Simple monitoring status for ActiveJob, independent of your queuing backend or cache storage.
gem '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.cache
defaults toActiveSupport::Cache::NullStore
which will result in empty status. Setting a cache store for ActiveJob::Status is therefore mandatory.
To set your own 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
Because ActiveJob::Status relies on cache store, all statuses come with an expiration item. It's set to 1 hour by default.
To set a longer expiration:
# config/initializers/activejob_status.rb
ActiveJob::Status.options = { expires_in: 30.days.to_i }
Include the ActiveJob::Status
module in your jobs.
class MyJob < ActiveJob::Base
include ActiveJob::Status
end
The module introduces two methods:
status
to directly read/update statusprogress
to 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 of 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
Within a controller
def status
status = ActiveJob::Status.get(params[:job])
render json: status.to_json
end
- Don't hesitate to submit your feature/idea/fix in issues
- Fork the repository
- Create your feature branch
- Create a pull request
Not yet provided.
Copyright (c) 2019 Savater Sebastien.
See LICENSE for further details.
Contributors:
- Valentin Ballestrino @vala
- Dennis van de Hoef @dennisvandehoef