Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to organize side jobs by namespace

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.

like image 670
zapatos Avatar asked Feb 11 '16 04:02

zapatos


1 Answers

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
like image 145
Dimitris Klisiaris Avatar answered Oct 15 '22 03:10

Dimitris Klisiaris