Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiple methods per sidekiq worker

I don't get it.

Per Sidekiq documentation, each worker (mine is called FeedWorker) can only contain one method called perform. Well, what if I want to run mulitple methods through the same worker?

For instance, my FeedWorker (you guessed it, it processes an activity feed) should run the following 3 methods:

announce_foo
announce_bar
invite_to_foo

I don't think this is an unreasonable expectation. I'm sure other folks have considered this. I'm no genius, but I know I'm not breaking new ground in expectations here. Yet it's not clear how one would do this.

Right now, it looks like I have to code this way:

def perform(id, TYPE)
  if TYPE == BAR
    Bar.find(id) and_announce_bar
  else
    Foo.find(id) and_announce_foo
  end
end

Boring and ugly code. There must be better out there. Any help appreciated!

like image 329
Laurent Avatar asked Apr 27 '13 19:04

Laurent


People also ask

Is Sidekiq multithreaded?

At the same time, Sidekiq uses multithreading so it is much more memory-efficient than Rescue.

How many employees does Sidekiq have?

Today Sidekiq uses a default concurrency of 25. These means Sidekiq will spawn 25 worker threads and execute up to 25 jobs concurrently in a process.

How Sidekiq works or how would you implement Sidekiq?

Sidekiq is an open-source framework that provides efficient background processing for Ruby applications. It uses Redis as an in-memory data structure to store all of its job and operational data. It's important to be aware that Sidekiq by default doesn't do scheduling, it only executes jobs.

How many Sidekiq threads are there?

By default Sidekiq uses 10 threads which means that either your database on your worker will need to be 10+ threads or you will need to configure your Sidekiq process to use fewer threads.


2 Answers

perform method is the entry point of your Worker. Inside of it you can create as many instance methods as you want, to organize your code as it best fits your need. It's a good practice though to keep worker code as slim as possible. Calling other objects from inside of it for example is a way to achieve that. You'll find your code will be easier to test too.

like image 66
Fabrizio Regini Avatar answered Sep 22 '22 16:09

Fabrizio Regini


I had the same question for awhile and now have a rather simple solution: use the Delayed Extension method on any class, as explained in docs, for ex:

# Usage: EmailWorker.delay.do_something

class EmailWorker
  class << self
    def send_this(attrs)
      MyMailer.some_action(attrs).deliver
    end

    def send_that(attrs)
      MyMailer.another_action(attrs).deliver
    end
  end
end

Any class can be delayed, so no need to include Sidekiq::Worker if you're not going to use perform_async method.

The problem I've had with this is that the per-worker options won't be used unless you go thru the perform_async method.

like image 38
Luke W Avatar answered Sep 23 '22 16:09

Luke W