Reputation: 993
In my ruby on rails web app I want to queue DML operations.
Now I do not know if it would be best use an existing queue (like RabbitMQ) or to write my own but I guess writing my own would be a good idea.
Do you have any tips on how to write a database queue and integrate it in ruby on rails?
Thanks.
Upvotes: 1
Views: 657
Reputation: 61
It's quite often you need some consumer-producer implementation on your own. You can create own processor and tasks, it's not so hard. Here is an example you can use. Just implement own backend logic using MySQL.
module MyJobs
def self.backend
@backend
end
def self.backend=(backend)
@backend = backend
end
class Processor
def self.start(concurrency = 1)
concurrency.times { |n| new("Processor #{n}") }
end
def initialize(name)
thread = Thread.new do
loop do
payload = MyJobs.backend.pop
worker_class = payload[:worker]
worker_class.new.perform(*payload[:args])
end
end
thread.name = name
end
end
module Worker
module ClassMethods
def perform_async(*args)
MyJobs.backend.push(worker: self, args: args)
end
end
end
end
MyJobs.backend = Queue.new
MyJobs::Processor.start(5)
Upvotes: 1