darkginger
darkginger

Reputation: 690

Delayed Job is failing with no log or alert

Setting up a Delayed Job for the first time, and it is not working in my testing environment after following this RailsCast.

I have two methods that I want to run - and in testing, I want to run them 15 seconds after a page loads. These methods are calculate_rankings and update_wallet from the Participation.rb model.

After installing the gem per the guide above, I am trying two things to debug: 1) I am running rake jobs:work, where there is no output after Starting job worker, and 2) I have added an alert to my controller's method, which does not activate.

Post-gem installation and running the migration (which was successful), I have made two changes in the code.

  1. Created app/jobs/chipsupdater.rb

This was the error. The file was incorrectly named and was therefore not called correctly in the controller. It should have been app/jobs/chips_update_job.rb based on the method I wrote below.

     class ChipsUpdaterJob < Struct.new(:game_id)
        def perform
          Participation.calculate_ranking(game_id)
          Participation.update_wallet(game_id)
        end
       end
  1. Updated my ScoreboardController that I want to initialize the delayed job:

    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
    

With no flash alert or output in the jobs console, my belief is that I am not correctly calling the Delayed Job. Can you spot where I am going wrong (or why I can't debug using the jobs console)?

EDIT

Per the discussion in the comments, I am now running the worker in a test environment. However, I am continuing to fail to a) post jobs to my DelayedJobs table or b) achieve our key output, which is to use the two methods in the ChipsUpdater Job file.

Thus, my belief is that I am failing to correctly call the delayed job based on this failure.

For context, I want to include two more potentially relevant files.

First, here is the Participation model that is referenced in the 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

If this is taken out of a delay context and run as a normal method from the Scoreboard Controller, I have confirmed locally that it runs the ranking process successfully. The delay seems to be the failure.

/bin/delayed_job

require File.expand_path(File.join(File.dirname(__FILE__), '..', 'config', 'environment'))

require 'delayed/command'
Delayed::Command.new(ARGV).daemonize

Upvotes: 0

Views: 1525

Answers (2)

Artem Biserov
Artem Biserov

Reputation: 109

Did you add this line to config/application.rb

config.active_job.queue_adapter = :delayed_job

Upvotes: 0

PhilVarg
PhilVarg

Reputation: 4811

If youre trying to run a job in your testing environment then you need to set that environment when running the rake command

RAILS_ENV=test bundle exec rake jobs:work

also, try just printing instead of using a flash notice. a print will more likley show up on the tab you run the job from

can you confirm youre actually going to that chipsupdater route in one of your tests?

Upvotes: 2

Related Questions