延迟作业失败,没有日志或警报

时间:2016-04-15 05:32:09

标签: ruby-on-rails ruby delayed-job

首次设置延迟作业,并且在执行此RailsCast后,它在我的测试环境中无法正常工作。

我有两种方法可以运行 - 在测试中,我希望在页面加载后15秒运行它们。这些方法来自 Participation.rb 模型中的calculate_rankingsupdate_wallet

根据上面的指南安装gem后,我正在尝试调试两件事:1)我正在运行rake jobs:workStarting job worker之后没有输出,2)我添加了一个警告我的控制器的方法,它不会激活。

post-gem安装和运行迁移(成功),我在代码中做了两处更改。

  1. 创建 app / jobs / chipsupdater.rb
  2. 这是错误。该文件名称不正确,因此未在控制器中正确调用。它应该是 app / jobs / chips_update_job.rb ,基于我在下面写的方法

         class ChipsUpdaterJob < Struct.new(:game_id)
            def perform
              Participation.calculate_ranking(game_id)
              Participation.update_wallet(game_id)
            end
           end
    
    1. 更新了我想要初始化延迟作业的 ScoreboardController

      class ScoreboardController < ApplicationController
         def index
      
          @participations = Participation.where(finished: true, game_id: session[:game_id]).order(score: :desc).limit(10)
          @game = Game.find(params[:game_id])
         end
       def chipsupdater
          Delayed::Job.enqueue(ChipsUpdaterJob.new(params[:game_id]), :run_at => 1.minute.from_now) 
          flash[:notice] = "Things are good. Not broken."
         end 
       end
      
    2. 在作业控制台中没有闪光警报或输出,我的信念是我没有正确地调用延迟作业。你能发现我出错的地方(或者为什么我不能使用作业控制台进行调试)?

      修改

      根据评论中的讨论,我现在正在测试环境中运行worker。但是,我仍然未能a)将作业发布到我的DelayedJobs表或b)实现我们的密钥输出,即使用ChipsUpdater作业文件中的两个方法。

      因此,我的信念是,我未能根据此失败正确地调用延迟的工作。

      对于上下文,我想要包含两个可能相关的文件。

      首先,这是ChipUpdaterJob

      中引用的参与模型
      class Participation < ActiveRecord::Base
      
      ef self.calculate_ranking(game_id)
            records = Participation.where(game_id: game_id, finished: true).order('score DESC, updated_at ASC')
      
            records.each_with_index do |record, index|
              puts "rank: #{index + 1}"
              puts record.inspect
              puts record['score']
              puts record['user_id']
              puts record['game_id']
      
              Ranking.create(game_id: game_id, user_id: record['user_id'], game_time: record['updated_at'], ranking: index + 1)
          end
        end
      

      如果这是从延迟上下文中取出并从记分板控制器作为常规方法运行,我已在本地确认它成功运行了排名过程。延迟似乎是失败。

      / bin / delayed_job

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

2 个答案:

答案 0 :(得分:2)

如果您尝试在测试环境中运行作业,则需要在运行rake命令时设置该环境

RAILS_ENV=test bundle exec rake jobs:work

另外,请尝试打印而不是使用闪光灯通知。从

运行作业的选项卡上可以看到更多的打印件

你可以确认你在一次测试中确实要去chipsupdater路线吗?

答案 1 :(得分:0)

您是否已将此行添加到config / application.rb

config.active_job.queue_adapter = :delayed_job
相关问题