Reputation: 1836
Suppose I have a Sidekiq worker like this:
class MyWorker
include Sidekiq::Worker
def perform
message = SomeImportantClass.new.get_message
flash.now[:notice] = message
end
end
So when the user visits page the worker is launched. Without leaving the page user gets notification from Sidekiq worker. I'm not so experienced in this field so I do not know where to start.
Upvotes: 0
Views: 396
Reputation: 230551
You can't access flash
from workers, because they are outside of request context.
You'll have to communicate with your request context via some kind of storage (your main DB or memcached/redis, for example).
The outline is like this:
Store your message in the storage
class MyWorker
include Sidekiq::Worker
def perform
message = SomeImportantClass.new.get_message
redis.set("messages:#{message.user.id.to_s}") = message.text
end
end
Make an endpoint in your app that will show these messages for the current user
class MyController
def status
msg = redis.get("messages:#{current_user.id.to_s}")
render text: msg
end
end
Have client-side JS poll this endpoint and show a notification/modal/whatever when it gets a message.
You have to use the polling or other mechanism of async update (websockets, etc.), because without it, once page is rendered, it won't automagically know about progress of your worker.
Upvotes: 1