I have a lot of closely related ActiveJob side jobs and since each needs to run with perform
, I want to put them together in a folder namespace.
So for example, let's say I have:
app/jobs/hello_job.rb
app/jobs/goodbye_job.rb
app/jobs/thank_you_job.rb
and I call each of these like HelloJob.perform_later
.
What I want to have instead is something like:
app/jobs/greetings/hello_job.rb
app/jobs/greetings/goodbye_job.rb
app/jobs/greetings/thank_you_job.rb
and call them with something like Greetings::HelloJob.perform_later
.... although this does not work.
In ruby you can use modules as namespaces.
So you put the closely related ActiveJobs in a folder and you define each class inside a module with the same name as the folder.
For app/jobs/greetings/hello_job.rb
this works for me:
module Greetings
class HelloJob < ActiveJob::Base
queue_as :default
def perform
puts 'Hello!'
end
end
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With