ActiveJobChannel
Uses ActionCable to alert front-end users of finished ActiveJobs
Installation
Install in your Gemfile
gem 'active_job_channel'Setup an
ActionCablesubscription adapter- Note: A persisted subscription adapter is required for handling notifications
from background
ActiveJobprocesses. Currently only PostgreSQL and Redis are supported.
- Note: A persisted subscription adapter is required for handling notifications
from background
(Optional) If you need authorization for notifications, set up your own
ApplicationCable::ConnectionInclude
active_job_channel.jsin your layoutsjavascript_include_tag 'active_job_channel'or include it in your
app/assets/javascripts/application.js//= require active_job_channel
Usage
For each job you'd like to be notified about, enable active_job_channel
class MyJob < ActiveJob::Base
active_job_channel
end
To customize the client-side notification, define ActiveJobChannel.received
after including active_job_channel.js. The current default simply logs the
job status to the javascript console.
//= require notifyjs
//= require active_job_channel
ActiveJobChannel.received = function(data) {
var status = data.status;
var job_name = data.job_name;
if (status === 'success') { $.notify(job_name + ' succeeded!') }
else if (status === 'failure') { $.notify(job_name + ' failed!') }
}
Caveats
ActiveJobChannel depends on ActiveJob and ActionCable, and, as such, is
subject to their limitations:
- A persisted subscription adapter
is required for
ActionCableto handle notifications from backgroundActiveJobprocesses - Because
ActiveJobdoes not know when a job has permanently failed,ActiveJobChannelsends notfications for each failure, retried or final
Todo
- Better default front-end notification behavior
License
The gem is available as open source under the terms of the MIT License.