活动作业抛出意外错误

Posted

技术标签:

【中文标题】活动作业抛出意外错误【英文标题】:Active jobs throwing unexpected error 【发布时间】:2015-10-04 18:09:08 【问题描述】:

我正在尝试让 active_job 与 delay_job 一起工作。一切工作正常......因为代码按预期执行,但delayed_job 条目报告出现错误。我得到的错误如下。

uninitialized constant ProcessEventJob 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/activesupport-4.2.1/lib/active_support/inflector/methods.rb:261:in `const_get' 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/activesupport-4.2.1/lib/active_support/inflector/methods.rb:261:in `block in constantize' 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/activesupport-4.2.1/lib/active_support/inflector/methods.rb:259:in `each' 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/activesupport-4.2.1/lib/active_support/inflector/methods.rb:259:in `inject' 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/activesupport-4.2.1/lib/active_support/inflector/methods.rb:259:in `constantize' 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/activesupport-4.2.1/lib/active_support/core_ext/string/inflections.rb:66:in `constantize' 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/activejob-4.2.1/lib/active_job/core.rb:25:in `deserialize' 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/activejob-4.2.1/lib/active_job/execution.rb:20:in `execute' 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/activejob-4.2.1/lib/active_job/queue_adapters/delayed_job_adapter.rb:34:in `perform' 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/delayed_job-4.0.6/lib/delayed/backend/base.rb:94:in `block in invoke_job' 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/delayed_job-4.0.6/lib/delayed/lifecycle.rb:61:in `call' 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/delayed_job-4.0.6/lib/delayed/lifecycle.rb:61:in `block in initialize' 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/delayed_job-4.0.6/lib/delayed/lifecycle.rb:66:in `call' 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/delayed_job-4.0.6/lib/delayed/lifecycle.rb:66:in `execute' 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/delayed_job-4.0.6/lib/delayed/lifecycle.rb:40:in `run_callbacks' 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/delayed_job-4.0.6/lib/delayed/backend/base.rb:91:in `invoke_job' 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/delayed_job-4.0.6/lib/delayed/worker.rb:204:in `block (2 levels) in run' 
/home/getsmart/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/timeout.rb:89:in `block in timeout' 
/home/getsmart/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/timeout.rb:99:in `call' 
/home/getsmart/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/timeout.rb:99:in `timeout' 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/delayed_job-4.0.6/lib/delayed/worker.rb:204:in `block in run' 
/home/getsmart/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/benchmark.rb:303:in `realtime' 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/delayed_job-4.0.6/lib/delayed/worker.rb:203:in `run' 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/delayed_job-4.0.6/lib/delayed/worker.rb:280:in `block in reserve_and_run_one_job' 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/delayed_job-4.0.6/lib/delayed/lifecycle.rb:61:in `call' 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/delayed_job-4.0.6/lib/delayed/lifecycle.rb:61:in `block in initialize' 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/delayed_job-4.0.6/lib/delayed/lifecycle.rb:66:in `call' 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/delayed_job-4.0.6/lib/delayed/lifecycle.rb:66:in `execute' 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/delayed_job-4.0.6/lib/delayed/lifecycle.rb:40:in `run_callbacks' 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/delayed_job-4.0.6/lib/delayed/worker.rb:280:in `reserve_and_run_one_job' 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/delayed_job-4.0.6/lib/delayed/worker.rb:187:in `block in work_off' 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/delayed_job-4.0.6/lib/delayed/worker.rb:186:in `times' 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/delayed_job-4.0.6/lib/delayed/worker.rb:186:in `work_off' 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/delayed_job-4.0.6/lib/delayed/worker.rb:150:in `block (4 levels) in start' 
/home/getsmart/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/benchmark.rb:303:in `realtime' 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/delayed_job-4.0.6/lib/delayed/worker.rb:149:in `block (3 levels) in start' 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/delayed_job-4.0.6/lib/delayed/lifecycle.rb:61:in `call' 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/delayed_job-4.0.6/lib/delayed/lifecycle.rb:61:in `block in initialize' 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/delayed_job-4.0.6/lib/delayed/lifecycle.rb:66:in `call' 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/delayed_job-4.0.6/lib/delayed/lifecycle.rb:66:in `execute' 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/delayed_job-4.0.6/lib/delayed/lifecycle.rb:40:in `run_callbacks' 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/delayed_job-4.0.6/lib/delayed/worker.rb:148:in `block (2 levels) in start' 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/delayed_job-4.0.6/lib/delayed/worker.rb:147:in `loop' 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/delayed_job-4.0.6/lib/delayed/worker.rb:147:in `block in start' 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/delayed_job-4.0.6/lib/delayed/plugins/clear_locks.rb:7:in `call' 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/delayed_job-4.0.6/lib/delayed/plugins/clear_locks.rb:7:in `block (2 levels) in <class:ClearLocks>' 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/delayed_job-4.0.6/lib/delayed/lifecycle.rb:79:in `call' 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/delayed_job-4.0.6/lib/delayed/lifecycle.rb:79:in `block (2 levels) in add' 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/delayed_job-4.0.6/lib/delayed/lifecycle.rb:61:in `call' 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/delayed_job-4.0.6/lib/delayed/lifecycle.rb:61:in `block in initialize' 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/delayed_job-4.0.6/lib/delayed/lifecycle.rb:79:in `call' 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/delayed_job-4.0.6/lib/delayed/lifecycle.rb:79:in `block in add' 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/delayed_job-4.0.6/lib/delayed/lifecycle.rb:66:in `call' 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/delayed_job-4.0.6/lib/delayed/lifecycle.rb:66:in `execute' 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/delayed_job-4.0.6/lib/delayed/lifecycle.rb:40:in `run_callbacks' 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/delayed_job-4.0.6/lib/delayed/worker.rb:146:in `start' 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/delayed_job-4.0.6/lib/delayed/command.rb:124:in `run' 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/delayed_job-4.0.6/lib/delayed/command.rb:112:in `block in run_process' 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/daemons-1.2.2/lib/daemons/application.rb:265:in `call' 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/daemons-1.2.2/lib/daemons/application.rb:265:in `block in start_proc' 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/daemons-1.2.2/lib/daemons/daemonize.rb:84:in `call' 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/daemons-1.2.2/lib/daemons/daemonize.rb:84:in `call_as_daemon' 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/daemons-1.2.2/lib/daemons/application.rb:269:in `start_proc' 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/daemons-1.2.2/lib/daemons/application.rb:295:in `start' 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/daemons-1.2.2/lib/daemons/controller.rb:56:in `run' 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/daemons-1.2.2/lib/daemons.rb:193:in `block in run_proc' 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/daemons-1.2.2/lib/daemons/cmdline.rb:88:in `call' 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/daemons-1.2.2/lib/daemons/cmdline.rb:88:in `catch_exceptions' 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/daemons-1.2.2/lib/daemons.rb:192:in `run_proc' 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/delayed_job-4.0.6/lib/delayed/command.rb:110:in `run_process' 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/delayed_job-4.0.6/lib/delayed/command.rb:91:in `block in daemonize' 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/delayed_job-4.0.6/lib/delayed/command.rb:89:in `times' 
/home/getsmart/.rvm/gems/ruby-2.2.2@phoenix/gems/delayed_job-4.0.6/lib/delayed/command.rb:89:in `daemonize' bin/delayed_job:5:in `<main>'

ProcessEventJob 是我在app/jobs/process_event_job.rb 中的工作类。

class ProcessEventJob < ActiveJob::Base
  queue_as :event_handler_cycle

  def perform(event)
    Events::EventHandler.new(raw_data: event).call
  end
end

我用它来称呼它

ProcessEventJob.perform_later(params)

我已经重启了服务器和delayed_jobs。确保它在正确的环境变量下运行。不太确定还能做什么。我也可以从控制台正常访问 ProcessEventJob。

我认为 app 文件夹中的文件会自动需要。

任何关于如何调试它的想法将不胜感激。

编辑

注意下面的代码调用了延迟的作业。它是我们用来捕获事件的暴露端点。

module Api
  module V1
    class SegmentEventsController < ApiController
      def create
        begin
          ProcessEventJob.perform_later(params)
          render_success
        rescue StandardError => e
          render_fail
        end
      end

      private

      def render_success
        render status: 200, json:  "success" => "true" 
      end

      def render_fail
        render status: 400, json:  "success" => "false" 
      end

      def restrict_access
        unless ApiKey.exists?(access_token: params[:access_token])
          head :unauthorized
        end
      end
    end
  end
end


Rails.application.configure do
  # Settings specified here will take precedence over those in config/application.rb.

  # Code is not reloaded between requests.
  config.cache_classes = true

  # Eager load code on boot. This eager loads most of Rails and
  # your application in memory, allowing both threaded web servers
  # and those relying on copy on write to perform better.
  # Rake tasks automatically ignore this option for performance.
  config.eager_load = true

  # Full error reports are disabled and caching is turned on.
  config.consider_all_requests_local       = false
  config.action_controller.perform_caching = true

  # Enable Rack::Cache to put a simple HTTP cache in front of your application
  # Add `rack-cache` to your Gemfile before enabling this.
  # For large-scale production use, consider using a caching reverse proxy like nginx, varnish or squid.
  # config.action_dispatch.rack_cache = true

 # Disable Rails's static file server (Apache or nginx will already do this).
  config.serve_static_files = false

  # Compress javascripts and CSS.
  config.assets.js_compressor = :uglifier
  config.assets.css_compressor = :sass

  # Do not fallback to assets pipeline if a precompiled asset is missed.
  config.assets.compile = false

  # Generate digests for assets URLs.
  config.assets.digest = true

  # `config.assets.precompile` has moved to config/initializers/assets.rb

  # Specifies the header that your server uses for sending files.
  # config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache
  # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx

  # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
  # config.force_ssl = true

  # Set to :debug to see everything in the log.
  config.log_level = :info

  # Prepend all log lines with the following tags.
  # config.log_tags = [ :subdomain, :uuid ]

  # Use a different logger for distributed setups.
  # config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new)

  # Use a different cache store in production.
  # config.cache_store = :mem_cache_store

  # Enable serving of images, stylesheets, and JavaScripts from an asset server.
  # config.action_controller.asset_host = "http://assets.example.com"

  # Precompile additional assets.
  # application.js, application.css, and all non-JS/CSS in app/assets folder are already added.
  # config.assets.precompile += %w( search.js )

  # Ignore bad email addresses and do not raise email delivery errors.
  # Set this to true and configure the email server for immediate delivery to raise delivery errors.
  config.action_mailer.raise_delivery_errors = false
  config.action_mailer.default_url_options =  :host => 'mywebsite.co.za' 
  config.action_mailer.perform_deliveries = true
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = 
      :address              => "smtp.gmail.com",
      :port                 => 587,
      :user_name            => 'email',
      :password             => 'password',
      :authentication       => 'plain',
      :enable_starttls_auto => true  

  # Enable locale fallbacks for I18n (makes lookups for any locale fall back to
  # the I18n.default_locale when a translation cannot be found).
  config.i18n.fallbacks = true

  # Send deprecation notices to registered listeners.
  config.active_support.deprecation = :notify

  # Disable automatic flushing of the log to improve performance.
  # config.autoflush_log = false

  # Use default logging formatter so that PID and timestamp are not suppressed.
  config.log_formatter = ::Logger::Formatter.new

  # Do not dump schema after migrations.
  config.active_record.dump_schema_after_migration = false

  config.active_job.queue_adapter = :delayed_job
end

编辑

所以我似乎取得了一些进展,但仍然没有意义。我的登台设置运行了多个应用程序。其中两个使用delayed_jobs。

我认为这两者之间可能发生了一些冲突,所以我停止了我不需要的延迟工作,突然间一切都开始正常工作了。我遇到了一个post,它说如果你使用 -i 可以运行两个实例 ...

我不确定这是否是我正在寻找的。

【问题讨论】:

从哪里调用 ProcessEventJob? 查看从哪里调用它的编辑。 如果您手动需要process_event_job.rb,可能在初始化程序中,这是否有效? 那行不通。如果我能提供更多信息,请告诉我。 【参考方案1】:

您的application.rb 中有config.active_job.queue_adapter = :delayed_job 吗?

我有这个文件script/delayed_job 包含

#!/usr/bin/env ruby

require File.expand_path(File.join(File.dirname(__FILE__), '..', 'config', 'environment'))
require 'delayed/command'
Delayed::Command.new(ARGV).daemonize

然后在服务器上输入你的项目目录并运行

RAILS_ENV=staging bundle exec script/delayed_job start

【讨论】:

是的。那是行不通的。它在我们的本地机器上运行良好,但我们无法让它在我们的暂存环境中运行。 您可以粘贴用于在暂存时启动延迟作业的命令吗?

以上是关于活动作业抛出意外错误的主要内容,如果未能解决你的问题,请参考以下文章

Jest Enzyme 测试抛出意外的令牌错误

Kurento媒体服务器抛出“处理方法时出现意外错误:未找到工厂'PlayerEndPoint''”

提取操作中的 BigQuery 错误:错误处理作业意外。请再试一次

NSString 到 String 抛出“致命错误:在展开可选值时意外发现 nil”

回调函数抛出意外的“不一致的参数”错误

代码未在 nodejs 中编译,抛出意外错误(Web3.js)