Tag: ruby on rails

CallbackWorker

At Recurrency, we use Sidekiq for background processing. We have several cases where we need to create a model object, save it, and then have some method on that object execute in the background. Instead of creating individual worker classes for each of these use cases, our CTO – Jonathan George – came up with a simple reusable worker class to make this extremely easy.  He came up with CallbackWorker, which I have iterated on, simplified, and ended up getting down to 9 lines with the use of splat arguments, Global ID, and general Ruby awesomeness.

To use this generic worker, just call perform_async with your model object’s Global ID, the method to call as a symbol, and any parameters that need to be passed to the method:

This obviously won’t work for every situation, but it makes some of the simpler background processing tasks quicker to implement and reduces the amount of code we have to write and maintain.

If you port this to another background processing library, please share it in the comments.