-
Notifications
You must be signed in to change notification settings - Fork 154
Handle events asynchronously
Wisper currently has adapters for Sidekiq, Celluloid and ActiveJob.
# choose one...
gem 'wisper-sidekiq'
gem 'wisper-celluloid'
gem 'wisper-activejob'
publisher.subscribe(MyListener, async: true)
This is the same as usual except we set async: true
.
When handling events asynchronously using Sidekiq the listener should be a class. This is a limitation of the background queues in general since they need to serialize the listener and instantiate it later in a new process/thread. An object has internal state which is difficult to serialize.
publish(:user_created, user.id)
It is the subscription that determines if the event is handled asynchronously or not, so publishing remains the same as usual.
The only caveat is broadcast arguments must be simple seralizable objects such as string and integer. Except in the case of ActiveJob which supports GlobalID meaning ActiveRecord models can also be used.
If you have problems first check that the background queue is set up correctly, can you create a job on the queue without using wisper?
Are exceptions occuring in the job? Try setting async: false
and re-running the code.
Are you expecting async to work in your tests some queues may run jobs synchronously in test environments.
Ensure critical jobs are Idempotent.
If you are still having problems please ask a question on StackOverflow and tag it wisper
.
Need to ask a question? Please ask on StackOverflow tagging the question with wisper.
Found a bug? Please report it on the Issue tracker.