Skip to content

Instantly share code, notes, and snippets.

@seyhunak
Created December 17, 2013 08:48
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seyhunak/8001937 to your computer and use it in GitHub Desktop.
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
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