Jaffa
Jaffa

Reputation: 12710

Class variable reset with rails / spring

I've a class defined as such:

class PublicationJob < ActiveJob::Base
  def self.jobs
    @jobs ||= Hash{|h, k| h[k] = []}
  end
  def self.register(format, job_class)
    jobs[format] << job_class
  end
  # [...]
end

To register different job classes, I put in an initializer:

PublicationJob.register(:tex, SaveJob)
PublicationJob.register(:saved_tex, TexJob)
#...

The in the rails console I try:

PublicationJob.jobs
#> {:tex => [SaveJob], :saved_tex => [TexJob]}

But if I exit the console (Ctrl-D) then restart it, at some point the hash will be empty!

Why is the class variable reset in this case?

I use rails 4.2.1 and spring, and I know that if I kill/stop spring it works again for some time. Is it related to spring?

Upvotes: 3

Views: 540

Answers (2)

Tom H
Tom H

Reputation: 207

I know this is rather old, but I have encountered this issue a couple of times, and feel that you don't have to abandon spring if you set class level variables at initialization.

All you need to do is re-assign them in a spring ".after_fork" block. So for the above issue, place in a "config/spring.rb" file, the following:

if ("Spring".constantize rescue nil)
  Spring.after_fork do
    PublicationJob.register(:tex, SaveJob)
    PublicationJob.register(:saved_tex, TexJob)
  end
end

This will reset those variables after spring completes the fork and reloading code. I wrap it in a check to ensure that Spring is available, which it likely won't be in production.

Upvotes: 2

Jaffa
Jaffa

Reputation: 12710

Okay, so this was entirely Spring related, and I fixed it by removing spring.

Thanks to @NekoNova who pointed me to the right part of the documentation, I found that:

This saves off the first version of the User class, which will not be the same object as User after the code has been reloaded:

[...]

So to avoid this problem, don't save off references to application constants in your initialization code.

In other words, I can't initialize my classes using initializers, because althought it'll work in production, it won't work in development.

Upvotes: 2

Related Questions