Skip to content

Handle events asynchronously

Shane Wilton edited this page Feb 3, 2015 · 1 revision

Wisper currently has adapters for Sidekiq, Celluloid and ActiveJob.

Gemfile

# choose one...
gem 'wisper-sidekiq'
gem 'wisper-celluloid'
gem 'wisper-activejob'

Subscribing

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.

Publishing

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.

Troubleshooting and Tips

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.