I'm building a rails app, but I need to be able to listen for events in redis.
How might I got about starting a single separate process when the rails server starts? I only ever need one instance of it running, and it shouldn't end until the rails server stops.
I tried setting up a rake task and invoking it, and the take task starts fine, but it wouldn't let puma start. There has to be a better way. This is what I have.
lib/tasks/redis_monitor.rb
namespace :redis_monitor do
task monitor: :environment do
$redis.subscribe('channel') do |on|
on.message do |channel, message|
# stuff.
end
end
end
end
config/application.rb
config.after_initialize do
Rails.application.load_tasks
Rake::Task['redis_monitor:monitor'].invoke
end
Thanks!