How to resolve deserialization error in delayed job?

前端 未结 7 1977
遇见更好的自我
遇见更好的自我 2021-02-06 22:38

I am trying to use DelayedJob and the job is failing, giving the following error in the database:

{Delayed::DeserializationError
/Library/Ruby/Gems/1.8/gems/delayed_j         


        
7条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-06 23:09

    Sometimes when we upgrade libs delayed jobs still keep old references.

    Try to find the id of delayed_job in logs and play to parse its handler to ruby to find the wrong reference

    j = DelayedJob.find(XXX)
    data = YAML.load_dj(j.handler)
    data.to_ruby
    

    I made a pull request to help with this problem.

    Meanwhile you can use this lines

    # config/initializers/delayed_job.rb
    
    # Monkey patch to use old class references
    module Psych
    
      class << self; attr_accessor :old_class_references end
      @old_class_references = {}
    
      class ClassLoader
        private
    
        def find klassname
          klassname = ::Psych.old_class_references[klassname] || klassname
          @cache[klassname] ||= resolve(klassname)
        end
      end
    
      module Visitors
        class ToRuby < Psych::Visitors::Visitor
          def revive klass, node
            if klass.is_a? String
              klassname = ::Psych.old_class_references[klass] || klass
              klass = Kernel.const_get(klassname) rescue klassname
            end
            s = register(node, klass.allocate)
            init_with(s, revive_hash({}, node), node)
          end
        end
      end
    end
    
    # Add all old dependencies (hash keys) pointing to new references (hash values)
    Psych.old_class_references = {
      'ActiveRecord::AttributeSet' => 'ActiveModel::AttributeSet'
      # ...
    }
    

提交回复
热议问题