1

I've been trying to implement a DelayedJob custom job for a very long time, but am not finding much information online in terms of how to do this from start to finish, and am finding almost nothing that is not about sending mass emails (I've read collectiveidea's Github intro, Railscasts, SO questions, etc). As someone relatively new to Rails, I imagine that while the instructions are likely clear for someone more experienced, they are not clear enough for someone at my level to understand how to get this to work properly.

The aim of my task is to run the job, and then destroy the object (I am aware that DelayedJob destroys all completed jobs, but I also want the object destroyed from my database as well upon completion of the job.)

Previously, I was doing this with a DelayedJob non-custom job in my controller's create method: user.delay.scrape, followed by user.delay.destroy, which worked well. Therefore, everything else in my application is working fine, and the problem lies strictly in how I am setting up this custom job. However, for various reasons, it would be much better in this case to create a custom job.

Below is the current (non-working) way I have DelayedJob set up in my app. However, when I run the app, the console reports: uninitialized constant UsersController::UserScrapeJob. Any suggestions of how to get this to work properly would be greatly appreciated, and I'd be happy to answer any questions about this request.

Here is my model:

class User < ActiveRecord::Base
 def scrape
  some code here...
 end
end

In my controller, the delayed job needs to function as part of the create method.

And here is my controller (with only the create method shown):

class UsersController < ApplicationController
 @user = User.new(params[:user])
 if @user.save
  Delayed::Job.enqueue UserScrapeJob.new(user.id)
 else
  render :action => 'new'
 end
end

And here is the job file userScrapeJob.rb, which is in the app/jobs folder:

class UserScrapeJob < Struct.new(:user_id)  
 def perform
  user = User.find(user_id)
  user.scrape
  user.destroy
 end
end
4

2 回答 2

2

创建作业时有一个错字,类的名称是UserScrapeJob,带有大写的“U”(ruby 中的类名称是常量)。

Delayed::Job.enqueue UserScrapeJob.new(user.id)

if ... else ... end您在 if、it's和 not中也有语法错误if ... end else ... end

于 2013-07-19T03:30:13.930 回答
1

尝试将您的作业文件从 重命名userScrapeJob.rbuser_scrape_job.rb.

当您调用UserScrapeJob.newRails 时,会将类名转换为snake case(即user_scrape_job)并查找该名称的相应文件user_scrape_job.rb。

于 2013-07-19T14:23:33.373 回答