Created
December 17, 2013 08:48
-
-
Save seyhunak/8001937 to your computer and use it in GitHub Desktop.
Rails - Sidekiq Middleware to publish to Faye for real time batch status updates
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Sidekiq | |
module Middleware | |
module Server | |
class PublishBatchWorkerCompleted | |
def call(worker, job, queue) | |
yield | |
ensure | |
begin | |
unless worker.bid.nil? | |
status = Sidekiq::Batch::Status.new(worker.bid) | |
publish "/batches/status/#{worker.bid}", status.success_pct | |
end | |
rescue | |
puts "failed to post to faye" | |
end | |
end | |
def publish channel, pct | |
uri = URI.parse 'http://localhost:3000/faye' | |
Net::HTTP.post_form(uri, { message: { data: pct, channel: channel}.to_json }) | |
end | |
end | |
end | |
end | |
end | |
Sidekiq.configure_server do |config| | |
config.server_middleware do |chain| | |
chain.insert_before Sidekiq::Middleware::Server::Batch, Sidekiq::Middleware::Server::PublishBatchWorkerCompleted | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment