jondot / sneakers

A fast background processing framework for Ruby and RabbitMQ
https://github.com/jondot/sneakers
MIT License
2.24k stars 333 forks source link

How to automatically spawn workers in Rails #408

Closed MartinCura closed 4 years ago

MartinCura commented 4 years ago

I'm sorry if this feels like a noob question, but i read the Wiki up and down and can't seem to find the proper way to do this.

I have an app server which will be listening for messages, so i want a Worker to always be running. The server's running on Passenger and we don't use Procfile, so foreman is not an option. Of course, i don't want to manually do rake sneakers:run each time the server is updated, restarted or cloned.

The question seems silly to me because it's such a simple thing, but i can't see from the docs what is the correct way to do it (maybe lack of experience). I've already tried simply doing

class ProcessingDoneWorker
  include Sneakers::Worker
  ...
end

Thread.new do
  Sneakers::Runner.new([ProcessingDoneWorker]).run
end

but this seems to spawn a new one every so often (while i only want one) and admittedly was just a quick and ugly solution. Putting the same code elsewhere creates similar or different problems.

gabrieljoelc commented 4 years ago

@MartinCura thanks for using sneakers and for calling out some possible gaps in our documentation.

How do you deploy your application?

MartinCura commented 4 years ago

Thanks for the response!

Locally i just use docker-compose, rails server with Puma. For production it's an Elastic Beanstalk we use Phusion Passenger (with Ruby 2.4 running on 64bit Amazon Linux/2.9.3). Thus it's Rack-based, if i'm not mistaken, and we have a minimalist config.ru.

gabrieljoelc commented 4 years ago
MartinCura commented 4 years ago

I think i had ran into that post, but i didn't follow that section because it seemed about something else (it doesn't even mention Sneakers).

Anyway, at least for the moment i just replaced the Sneakers worker with Bunny::subscribe. It's uglier, but good enough for our rather simple use case, at least for the moment. If Sneakers gets an easier way to run its Workers, i'll consider switching back.

Thanks for your help anyway :)

gabrieljoelc commented 4 years ago

np. thanks for the question.