Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Railtie to delayed_job_active_record #106

Closed
wants to merge 1 commit into from

Conversation

yuki24
Copy link
Contributor

@yuki24 yuki24 commented May 1, 2015

I'm following up on #105.

This changes delayed_job_active_record to use Railtie and ActiveSupport.on_load to set it up on Rails so that Delayed::Backend::ActiveRecord::Job will respect arbitrary configs.

Explicitly requiring ActiveRecord models in
`lib/delayed_job_active_record.rb` may result in losing custom
configurations that are set in config/initializers on Rails.

Let' use Railtie and ActiveSupport.on_load to set it up so that
Delayed::Backend::ActiveRecord::Job model will respect arbitrary
configs.
@coveralls
Copy link

Coverage Status

Coverage increased (+0.24%) to 89.66% when pulling a95ead2 on yuki24:add-railtie into 270b06e on collectiveidea:master.

@betamatt
Copy link
Collaborator

betamatt commented May 1, 2015

I was a bit concerned by the engine change but I don't think there's any downside to a railtie.

👍

@albus522
Copy link
Member

albus522 commented May 1, 2015

My only concern with this is that AR might not load in dev mode. I am pretty sure the onload is trigger the first time ActiveRecord::Base is referenced and dev doesn't load anything until it is needed. Therefore DJ would not get setup until a model is actually used.

@albus522
Copy link
Member

albus522 commented May 1, 2015

We may be able to add an after_initialize to the Railtie to trigger a load

@dsander
Copy link

dsander commented Oct 15, 2016

This is working well for us, we only needed to add a ActiveSupport hook to be able to configure delayed_job_active_record in our initializer. (b314972)

@jzinn
Copy link

jzinn commented Feb 27, 2017

I have a problem with this commit in production mode when running rake jobs:work and rake jobs:clear.

This is the error I get:

NameError: uninitialized constant Delayed::Job
/.../vendor/bundle/ruby/2.3.0/gems/delayed_job-4.1.2/lib/delayed/worker.rb:310:in `reserve_job'
/.../vendor/bundle/ruby/2.3.0/gems/delayed_job-4.1.2/lib/delayed/worker.rb:305:in `reserve_and_run_one_job'
/.../vendor/bundle/ruby/2.3.0/gems/delayed_job-4.1.2/lib/delayed/worker.rb:213:in `block in work_off'
/.../vendor/bundle/ruby/2.3.0/gems/delayed_job-4.1.2/lib/delayed/worker.rb:212:in `times'
/.../vendor/bundle/ruby/2.3.0/gems/delayed_job-4.1.2/lib/delayed/worker.rb:212:in `work_off'
/.../vendor/bundle/ruby/2.3.0/gems/delayed_job-4.1.2/lib/delayed/worker.rb:175:in `block (4 levels) in start'
/.../vendor/bundle/ruby/2.3.0/gems/delayed_job-4.1.2/lib/delayed/worker.rb:174:in `block (3 levels) in start'
/.../vendor/bundle/ruby/2.3.0/gems/delayed_job-4.1.2/lib/delayed/lifecycle.rb:61:in `block in initialize'
/.../vendor/bundle/ruby/2.3.0/gems/delayed_job-4.1.2/lib/delayed/lifecycle.rb:66:in `execute'
/.../vendor/bundle/ruby/2.3.0/gems/delayed_job-4.1.2/lib/delayed/lifecycle.rb:40:in `run_callbacks'
/.../vendor/bundle/ruby/2.3.0/gems/delayed_job-4.1.2/lib/delayed/worker.rb:173:in `block (2 levels) in start'
/.../vendor/bundle/ruby/2.3.0/gems/delayed_job-4.1.2/lib/delayed/worker.rb:172:in `loop'
/.../vendor/bundle/ruby/2.3.0/gems/delayed_job-4.1.2/lib/delayed/worker.rb:172:in `block in start'
/.../vendor/bundle/ruby/2.3.0/gems/delayed_job-4.1.2/lib/delayed/plugins/clear_locks.rb:7:in `block (2 levels) in <class:ClearLocks>'
/.../vendor/bundle/ruby/2.3.0/gems/delayed_job-4.1.2/lib/delayed/lifecycle.rb:79:in `block (2 levels) in add'
/.../vendor/bundle/ruby/2.3.0/gems/delayed_job-4.1.2/lib/delayed/lifecycle.rb:61:in `block in initialize'
/.../vendor/bundle/ruby/2.3.0/gems/delayed_job-4.1.2/lib/delayed/lifecycle.rb:79:in `block in add'
/.../vendor/bundle/ruby/2.3.0/gems/delayed_job-4.1.2/lib/delayed/lifecycle.rb:66:in `execute'
/.../vendor/bundle/ruby/2.3.0/gems/delayed_job-4.1.2/lib/delayed/lifecycle.rb:40:in `run_callbacks'
/.../vendor/bundle/ruby/2.3.0/gems/delayed_job-4.1.2/lib/delayed/worker.rb:171:in `start'
/.../vendor/bundle/ruby/2.3.0/gems/delayed_job-4.1.2/lib/delayed/tasks.rb:11:in `block (2 levels) in <top (required)>'
/.../vendor/bundle/ruby/2.3.0/gems/rake-12.0.0/exe/rake:27:in `<top (required)>'
Tasks: TOP => jobs:work
(See full trace by running task with --trace)

I've determined that the initializer runs but the active support on_load never runs.

module Delayed
  module Backend
    module ActiveRecord
      class Railtie < ::Rails::Railtie
        initializer 'delayed_job_active_record' do |_app|

          puts 'This line is executed'

          ActiveSupport.on_load(:active_record) do

            puts 'This line is never executed'

            require "delayed/backend/active_record"
            Delayed::Worker.backend = :active_record
          end
        end
      end
    end
  end
end

The rake tasks work fine in development mode.

@jzinn
Copy link

jzinn commented Feb 28, 2017

If I modify the rake task to reference ActiveRecord::Base, then it works:

namespace :jobs do
  desc 'Clear the delayed_job queue.'
  task :clear => :environment do
    ActiveRecord::Base
    Delayed::Job.delete_all
  end
end

@jzinn
Copy link

jzinn commented Feb 28, 2017

My workaround is to add a lib/tasks/jobs.rake file in my application that adds a prerequisite to the jobs:clear and jobs:work tasks that references ActiveRecord::Base so that the ActiveSupport.on_load(:active_record) block executes.

namespace :jobs do
  task :reference_active_record_base do
    ActiveRecord::Base
  end

  task clear: :reference_active_record_base
  task work: :reference_active_record_base
end

@yuki24
Copy link
Contributor Author

yuki24 commented Feb 28, 2017

I'll rebase the commit later today.

@jzinn
Copy link

jzinn commented Feb 28, 2017

I should mention that I ran into the Rails 5 issue #128, and I am using the changes in this pull request through @lleger's fork mentioned in #128 (comment).

@rdunlop
Copy link

rdunlop commented Oct 12, 2017

Is there any chance that this will be resolved soon? I'm trying to upgrade a rails 4.2 project to rails 5.1, and I am hitting this problem because this gem is causing my Rails.application.config.active_record.belongs_to_required_by_default = false to be discarded.

I would try to use @lleger's fork, but:

  • it's 25 commits behind master
  • it does not allow Rails 5.1
  • comments in this PR indicate that it's not perfect, (the comments about jobs:clear)

Can we get this resolved so that we can use a Rails 5.0 project with Rails.application.config.active_record.belongs_to_required_by_default = false successfully? or need I look into alternative options?

Thank you

@p8 p8 mentioned this pull request Jul 9, 2019
@albus522
Copy link
Member

albus522 commented Nov 8, 2019

We will be using the approach in #172 soon

@albus522 albus522 closed this Nov 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants