对 Sidekiq 消息处理进行扩展,以跟踪您的作业
This gem is now maintained at https://github.com/kenaniah/sidekiq-status as of version 2.0.0.
Versions 1.1.4 and prior were maintained at https://github.com/utgarda/sidekiq-status/, which still exists for historical purposes.
An extension to Sidekiq message processing to track your jobs. Inspired by resque-status and mostly copying its features, using Sidekiq's middleware.
Fully compatible with ActiveJob.
Supports the latest versions of Sidekiq and all the way back to 3.x.
Add this line to your application's Gemfile:
gem 'sidekiq-status'And then execute:
$ bundleOr install it yourself as:
gem install sidekiq-statusTo get started:
To use, add sidekiq-status to the middleware chains. See Middleware usage on the Sidekiq wiki for more info.
require 'sidekiq'
require 'sidekiq-status'
Sidekiq.configure_client do |config|
# accepts :expiration (optional)
Sidekiq::Status.configure_client_middleware config, expiration: 30.minutes
end
Sidekiq.configure_server do |config|
# accepts :expiration (optional)
Sidekiq::Status.configure_server_middleware config, expiration: 30.minutes
# accepts :expiration (optional)
Sidekiq::Status.configure_client_middleware config, expiration: 30.minutes
endNote: This method of configuration is new as of version 0.8.0.
After that you can use your jobs as usual. You need to also include the Sidekiq::Status::Worker module in your jobs if you want the additional functionality of tracking progress and storing / retrieving job data.
class MyJob
include Sidekiq::Worker
include Sidekiq::Status::Worker # enables job status tracking
def perform(*args)
# your code goes here
end
endAs of version 0.8.0, only jobs that include Sidekiq::Status::Worker will have their statuses tracked. Previous versions of this gem used to track statuses for all jobs, even when Sidekiq::Status::Worker was not included.
To overwrite expiration on a per-worker basis, write an expiration method like the one below:
class MyJob
include Sidekiq::Worker
include Sidekiq::Status::Worker # enables job status tracking
def expiration
@expiration ||= 60 * 60 * 24 * 30 # 30 days
end
def perform(*args)
# your code goes here
end
endThe job status and any additional stored details will remain in Redis until the expiration time is reached. It is recommended that you find an expiration time that works best for your workload.
As sidekiq-status stores information about jobs in Redis, it is necessary to set an expiration time for the data that gets stored. A default expiration time may be configured at the time the middleware is loaded via the :expiration parameter.
As explained above, the default expiration may also be overridden on a per-job basis by defining it within the job itself via a method called #expiration.
The expiration time set will be used as the Redis expire time, which is also known as the TTL (time to live). Once the expiration time has passed, all information about the job's status and any custom data stored via sidekiq-status will disappear.
It is advised that you set the expiration time greater than the amount of time required to complete the job.
The default expiration time is 30 minutes.
You may query for job status any time up to expiration:
job_id = MyJob.perform_async(*args)
# :queued, :working, :complete, :failed or :interrupted, nil after expiry (30 minutes)
status = Sidekiq::Status::status(job_id)
Sidekiq::Status::queued? job_id
Sidekiq::Status::working? job_id
Sidekiq::Status::retrying? job_id
Sidekiq::Status::complete? job_id
Sidekiq::Status::failed? job_id
Sidekiq::Status::interrupted? job_id
Important: If you try any of the above status method after the expiration time, the result will be nil or false.
Version 0.7.0 has added full support for ActiveJob. The status of ActiveJob jobs will be tracked automatically.
To also enable job progress tracking and data storage features, simply add the Sidekiq::Status::Worker module to your base class, like below:
# app/jobs/application_job.rb
class ApplicationJob {status: 'complete', update_time: 1360006573, vino: 'veritas'}
Sidekiq::Status::get job_id, :vino #=> 'veritas'
Sidekiq::Status::at job_id #=> 5
Sidekiq::Status::total job_id #=> 100
Sidekiq::Status::message job_id #=> "Almost done"
Sidekiq::Status::pct_complete job_id #=> 5scheduled_job_id = MyJob.perform_in 3600
Sidekiq::Status.cancel scheduled_job_id #=> true
# doesn't cancel running jobs, this is more like unscheduling, therefore an alias:
Sidekiq::Status.unschedule scheduled_job_id #=> true
# returns false if invalid or wrong scheduled_job_id is provided
Sidekiq::Status.unschedule some_other_unschedule_job_id #=> false
Sidekiq::Status.unschedule nil #=> false
Sidekiq::Status.unschedule '' #=> false
# Note: cancel and unschedule are alias methods.Important: If you schedule a job and then try any of the status methods after the expiration time, the result will be either nil or false. The job itself will still be in Sidekiq's scheduled queue and will execute normally. Once the job is started at its scheduled time, sidekiq-status' job metadata will once again be added back to Redis and you will be able to get status info for the job until the expiration time.
Job status and metadata will automatically be removed from Redis once the expiration time is reached. But if you would like to remove job information from Redis prior to the TTL expiration, Sidekiq::Status#delete will do just that. Note that this will also remove any metadata that was stored with the job.
# returns number of keys/jobs that were removed
Sidekiq::Status.delete(job_id) #=> 1
Sidekiq::Status.delete(bad_job_id) #=> 0This gem provides an extension to Sidekiq's web interface with an index at /statuses.
As of 0.7.0, status information for an individual job may be found at /statuses/:job_id.
As of 0.8.0, only jobs that include Sidekiq::Status::Worker will be reported in the web interface.
To use, setup the Sidekiq Web interface according to Sidekiq documentation and add the Sidekiq::Status::Web require:
require 'sidekiq/web'
require 'sidekiq-status/web'Drawing analogy from sidekiq testing by inlining,
sidekiq-status allows to bypass redis and return a stubbed :complete status.
Since inlining your sidekiq worker will run it in-process, any exception it throws will make your test fail.
It will also run synchronously, so by the time you get to query the job status, the job will have been completed
successfully.
In other words, you'll get the :complete status only if the job didn't fail.
Inlining example:
You can run Sidekiq workers inline in your tests by requiring the sidekiq/testing/inline file in your {test,spec}_helper.rb:
require 'sidekiq/testing/inline'
To use sidekiq-status inlining, require it too in your {test,spec}_helper.rb:
require 'sidekiq-status/testing/inline'
Bug reports and pull requests are welcome. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
git checkout -b my-new-feature)git commit -am 'Add some feature')git push origin my-new-feature)MIT License, see LICENSE for more details. © 2012 - 2016 Evgeniy Tsvigun
暂无开放 Issues,或尚未同步最近议题。