DelayedJob worker environment does not properly load initializers?

128 views
Skip to first unread message

Paul Cechner

unread,
Feb 21, 2019, 10:26:21 PM2/21/19
to delayed_job
Hi all, 

I have noticed that `rails jobs:work` seems to load my rails initializers, but my DJ jobs when they are executing do not seem to see the results of those initializers running

To demonstrate the problem I can create a job to run that has some static state:

# my_job.rb
class MyJob
  @@x = 0
  def self.inc_x
    @@x = @@x + 1
  end
  def self.print_x
    puts "x is #{@@x}"
  end

  def execute_job
    self.class.print_x
  end
end

and then in my initializer:
# my_job_initializer.rb
MyJob.inc_x

However if I open a `rails console` and create a new job:
MyJob.schedule_job
# the delayed job worker process prints 'x is 0'
The worker process prints out x is 0

So my question is what is the correct way to initialize a rails application state so that the DelayedJob workers (as well as the rails application) are affected by that initialization?

Thanks everyone for your help!

David Genord II

unread,
Feb 21, 2019, 10:35:41 PM2/21/19
to delay...@googlegroups.com
DJ runs the standard rails initialization process. The only difference is specific to how rails runs the rake tasks. Rails hard codes it such that the rake task will not eager load the application. They do this for performance reasons, but can have mixed results.

I suspect you have something weird going on there, but your example setup does not give enough info. Can you setup a complete basic app that shows the issue?

David Genord II

--

---
You received this message because you are subscribed to the Google Groups "delayed_job" group.
To unsubscribe from this group and stop receiving emails from it, send an email to delayed_job...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Paul Cechner

unread,
Feb 22, 2019, 1:07:14 AM2/22/19
to delayed_job
Hi David, 

Please look at https://github.com/cechner/dj_test for a small application demonstrating the problem.

The README should contain all the information needed

Thanks!

David Genord II

unread,
Feb 22, 2019, 8:25:09 AM2/22/19
to delay...@googlegroups.com
I see what is happening. The development environment across rails, DJ included, does code reloading. So any state setup in an initializer on reloadable code in rails will be gone after the reload. Rails code reloading does not rerun any of the initialization process. There is however a specific hook in rails that does run after each reload, but I can not remember off the top of my head what.

David Genord II

Paul Cechner

unread,
Feb 23, 2019, 12:34:11 AM2/23/19
to delayed_job
Thanks a lot David, you have provided me with the keywords I needed to move on with this problem

On a side-note - In general it seems that my approach of using singletons to orchestrate my application is not recommended (I understand the problems with singletons!) but there seems to be a dearth of advice on the topic of orchestrating your rails services at run-time so Ill push ahead

My original problem is that I have a bunch of services that may or may not connect to each other (various services can be enabled/disabled in an in-database configuration)

For future reference, I have for now solved the problem by:

# config/initializers/my_application_config.rb
module MyApplication
 
class Application < Rails::Application
   
# was previously doing config.after_initialize, but to_prepare seems to be called every time a reload happens
    config
.to_prepare do
     
ServiceOrchestrator.prepare
   
end
 
end
end


# app/services/service_orchestrator.rb
class ServiceOrchestrator
 
def self.prepare
   
# clear because this seems to be invoked twice every reload for some reason
   
MyService1.clear_config
   
MyService2.clear_config

   
MyService2.configure_with_other_service(MyService1.instance)
 
end
end
Reply all
Reply to author
Forward
0 new messages