How to organize side tasks by namespace

I have a lot of closely related ActiveJob side jobs, and since each of them needs to work with perform, I want to combine them into 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 one similar to HelloJob.perform_later.

Instead, I want something like:

app/jobs/greetings/hello_job.rb
app/jobs/greetings/goodbye_job.rb
app/jobs/greetings/thank_you_job.rb

and call them something like Greetings::HelloJob.perform_later.... although this does not work.

+4
source share
1 answer

In ruby you can use the modules as a namespace.

So, you put closely related ActiveJobs in a folder and define each class inside the module with the same name as the folder.

app/jobs/greetings/hello_job.rb :

module Greetings
  class HelloJob < ActiveJob::Base
    queue_as :default

    def perform
      puts 'Hello!'
    end
  end
end
+1

Source: https://habr.com/ru/post/1628238/


All Articles