gpt4 book ai didi

ruby-on-rails - delayed_job 作为守护进程运行时作业失败。使用 rake 作业时运行良好 :work

转载 作者:数据小太阳 更新时间:2023-10-29 07:07:09 28 4
gpt4 key购买 nike

我实在是无计可施了。我不明白为什么它不起作用。

我创建了一个类,我使用 rake 命令对其进行调用和排队。当我使用“rake jobs:work”运行 worker 并调用命令“rake get_updates”时,它执行得很好。

但是,当我将 worker 作为守护进程运行时 (RAILS_ENV=production bin/delayed_job start) 并调用命令“rake get_updates”时,它会产生错误。

app/workers/get_updates.rb

class GetUpdates
def perform

beginning = Time.now

include SoapHelper

require 'nokogiri'
require 'activerecord-import'

product_count = 0
contloop = true

while(contloop == true) do

@xml = Nokogiri::XML(get_unsent_data())
@guid = @xml.xpath('.//Message').attr("Id")
bulkUpdates = []

if (@xml.xpath('.//Product').count > 0)

@xml.xpath('.//Product').each do |p|

product_count += 1

update = Update.new
update.update_type = p.attr("UpdateType")

codes = Hash.new

p.children().each do |info|

info.xpath('.//ProductCodes/Code').each do |coco|
codes[coco.attr("Scheme").to_s] = coco.content
end

update.data = p.children().to_xml.to_s

end

bulkUpdates << update

end

if Update.import bulkUpdates

acknowledge_soap_receipt(@guid.to_s)

else

puts "Error, data not saved"
contloop = false

end

else
contloop = false
end

end

@time = "Time elapsed #{Time.now - beginning} seconds"
puts "Product Count: #{ product_count }"
puts @time

end
end

发条.rake

task :get_updates => :environment do

Delayed::Job.enqueue GetUpdates.new

end

在“application.rb”中自动加载路径

config.autoload_paths += %W(#{config.root}/app/workers)
config.autoload_paths += %W(#{config.root}/app/helpers)

尝试执行作业时的错误信息

Job failed to load: undefined class/module GetUpdates. Handler: "--- !ruby/object:GetUpdates {}\n\n" /usr/local/rvm/gems/ruby-1.9.3-p448/gems/delayed_job-4.0.0/lib/delayed/backend/base.rb:97:in `rescue in payload_object' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/delayed_job-4.0.0/lib/delayed/backend/base.rb:89:in `payload_object' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/delayed_job-4.0.0/lib/delayed/backend/base.rb:105:in `block in invoke_job' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/delayed_job-4.0.0/lib/delayed/lifecycle.rb:60:in `call' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/delayed_job-4.0.0/lib/delayed/lifecycle.rb:60:in `block in initialize' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/delayed_job-4.0.0/lib/delayed/lifecycle.rb:65:in `call' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/delayed_job-4.0.0/lib/delayed/lifecycle.rb:65:in `execute' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/delayed_job-4.0.0/lib/delayed/lifecycle.rb:38:in `run_callbacks' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/delayed_job-4.0.0/lib/delayed/backend/base.rb:102:in `invoke_job' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/delayed_job-4.0.0/lib/delayed/worker.rb:206:in `block (2 levels) in run' /usr/local/rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/timeout.rb:69:in `timeout' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/delayed_job-4.0.0/lib/delayed/worker.rb:206:in `block in run' /usr/local/rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/benchmark.rb:295:in `realtime' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/delayed_job-4.0.0/lib/delayed/worker.rb:205:in `run' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/delayed_job-4.0.0/lib/delayed/worker.rb:267:in `block in reserve_and_run_one_job' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/delayed_job-4.0.0/lib/delayed/lifecycle.rb:60:in `call' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/delayed_job-4.0.0/lib/delayed/lifecycle.rb:60:in `block in initialize' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/delayed_job-4.0.0/lib/delayed/lifecycle.rb:65:in `call' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/delayed_job-4.0.0/lib/delayed/lifecycle.rb:65:in `execute' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/delayed_job-4.0.0/lib/delayed/lifecycle.rb:38:in `run_callbacks' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/delayed_job-4.0.0/lib/delayed/worker.rb:267:in `reserve_and_run_one_job' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/delayed_job-4.0.0/lib/delayed/worker.rb:189:in `block in work_off' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/delayed_job-4.0.0/lib/delayed/worker.rb:188:in `times' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/delayed_job-4.0.0/lib/delayed/worker.rb:188:in `work_off' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/delayed_job-4.0.0/lib/delayed/worker.rb:153:in `block (4 levels) in start' /usr/local/rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/benchmark.rb:295:in `realtime' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/delayed_job-4.0.0/lib/delayed/worker.rb:152:in `block (3 levels) in start' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/delayed_job-4.0.0/lib/delayed/lifecycle.rb:60:in `call' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/delayed_job-4.0.0/lib/delayed/lifecycle.rb:60:in `block in initialize' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/delayed_job-4.0.0/lib/delayed/lifecycle.rb:65:in `call' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/delayed_job-4.0.0/lib/delayed/lifecycle.rb:65:in `execute' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/delayed_job-4.0.0/lib/delayed/lifecycle.rb:38:in `run_callbacks' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/delayed_job-4.0.0/lib/delayed/worker.rb:151:in `block (2 levels) in start' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/delayed_job-4.0.0/lib/delayed/worker.rb:150:in `loop' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/delayed_job-4.0.0/lib/delayed/worker.rb:150:in `block in start' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/delayed_job-4.0.0/lib/delayed/plugins/clear_locks.rb:7:in `call' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/delayed_job-4.0.0/lib/delayed/plugins/clear_locks.rb:7:in `block (2 levels) in <class:ClearLocks>' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/delayed_job-4.0.0/lib/delayed/lifecycle.rb:78:in `call' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/delayed_job-4.0.0/lib/delayed/lifecycle.rb:78:in `block (2 levels) in add' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/delayed_job-4.0.0/lib/delayed/lifecycle.rb:60:in `call' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/delayed_job-4.0.0/lib/delayed/lifecycle.rb:60:in `block in initialize' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/delayed_job-4.0.0/lib/delayed/lifecycle.rb:78:in `call' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/delayed_job-4.0.0/lib/delayed/lifecycle.rb:78:in `block in add' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/delayed_job-4.0.0/lib/delayed/lifecycle.rb:65:in `call' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/delayed_job-4.0.0/lib/delayed/lifecycle.rb:65:in `execute' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/delayed_job-4.0.0/lib/delayed/lifecycle.rb:38:in `run_callbacks' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/delayed_job-4.0.0/lib/delayed/worker.rb:149:in `start' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/delayed_job-4.0.0/lib/delayed/command.rb:104:in `run' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/delayed_job-4.0.0/lib/delayed/command.rb:92:in `block in run_process' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/daemons-1.1.9/lib/daemons/application.rb:255:in `call' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/daemons-1.1.9/lib/daemons/application.rb:255:in `block in start_proc' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/daemons-1.1.9/lib/daemons/daemonize.rb:82:in `call' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/daemons-1.1.9/lib/daemons/daemonize.rb:82:in `call_as_daemon' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/daemons-1.1.9/lib/daemons/application.rb:259:in `start_proc' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/daemons-1.1.9/lib/daemons/application.rb:296:in `start' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/daemons-1.1.9/lib/daemons/application_group.rb:159:in `block (2 levels) in start_all' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/daemons-1.1.9/lib/daemons/application_group.rb:158:in `fork' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/daemons-1.1.9/lib/daemons/application_group.rb:158:in `block in start_all' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/daemons-1.1.9/lib/daemons/application_group.rb:157:in `each' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/daemons-1.1.9/lib/daemons/application_group.rb:157:in `start_all' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/daemons-1.1.9/lib/daemons/controller.rb:80:in `run' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/daemons-1.1.9/lib/daemons.rb:197:in `block in run_proc' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/daemons-1.1.9/lib/daemons/cmdline.rb:109:in `call' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/daemons-1.1.9/lib/daemons/cmdline.rb:109:in `catch_exceptions' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/daemons-1.1.9/lib/daemons.rb:196:in `run_proc' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/delayed_job-4.0.0/lib/delayed/command.rb:90:in `run_process' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/delayed_job-4.0.0/lib/delayed/command.rb:83:in `block in daemonize' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/delayed_job-4.0.0/lib/delayed/command.rb:81:in `times' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/delayed_job-4.0.0/lib/delayed/command.rb:81:in `daemonize' bin/delayed_job:6:in `<main>'

最佳答案

尝试在初始化器中加载类:

# in config/initializers/delayed_job.rb
require 'get_updates'

这应该使其可用于作业。

关于ruby-on-rails - delayed_job 作为守护进程运行时作业失败。使用 rake 作业时运行良好 :work,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18617426/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com