Delayed_job无法运行用户定义的方法 [英] Delayed_job Won't Run User defined method

查看:71
本文介绍了Delayed_job无法运行用户定义的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图让DelayedJob在后台运行一些用户定义的方法。对于此测试用例,我在助手中定义了以下方法:

I have been trying to get DelayedJob to run some user defined methods in the background. For this test case I defined the following method in a helper:

 def test_case
    u = User.new
     u.first_name = "JimBob"
     u.last_name = "joe"
     u.email = "itworked@eureka.com"
     u.password = "sailsJ123"
     u.password_confirmation = "sailsJ123"
     u.save
  end

然后,在控制器动作,我定义:

Then, in a controller action, I define:

  def action_name
     #whatever it does outside of this
     test_case

  end

这会导致test_case在此处执行操作时创建新用户跑。如果我尝试延迟工作,则将其更改为:

This causes test_case to create a new user when the action here is run. If I try to delay the job, I change it to:

  def action_name
     #whatever it does outside of this
     self.delay.test_case
  end

这将导致创建延迟作业,但有某些迹象表明我做错了:特别是,delay_job表中的处理程序包含在action_name中加载的所有其他信息(在这种情况下,该文件是一个大约50000个字符的非常大的文件),而不仅仅是指定运行test_case之类的东西。

This causes a delayed_job to be created, but with certain signs that I'm doing this wrong: specifically, the handler in the delayed_job table contains all the information loaded elsewhere in action_name (in this case a very large file of about 50000 characters), not just a simple handler specifying to run test_case or something.

如果我更改此设置以继续测试,则尝试这样做:

If I change this around to keep testing, I have tried to do:

 def action_name
     #whatever it does outside of this
     u = User.new
     u.first_name = "JimBob"
     u.last_name = "joe"
     u.email = "itworked@eureka.com"
     u.password = "sailsJ123"
     u.password_confirmation = "sailsJ123"
     u.delay.save
  end

这会创建一个DJ成功完成,并且按预期方式加载了指定详细信息的处理程序(例如u.name => JimBob ...)。但是,当作业运行时,不会创建新用户。我什至尝试过切换它只是为了破坏用户(尽管考虑到密码字段的处理方式,尽管创建DJ用户可能很困难),但这还是行不通的。

This creates a DJ successfully and, as expected, it loads a handler specifying the details (eg u.name => "JimBob"...). But when the job is run no new user gets created. I've even tried to switch it just to destroy a user (i though maybe creating a user with DJ might be hard, given the way password fields are handled) but this doesn't work.

长话短说:


  1. 如何让DJ正确运行后台方法?

  2. 在创建看起来很简单的延迟作业的情况下,如何对数据库进行任何更改?


推荐答案

您不能在ActiveRecord模型上延迟#save,因为DJ将在执行操作之前尝试从数据库中重新加载模型。所有数据将丢失。而是使用创建用户的#perform方法创建Job类。

You cannot delay #save on ActiveRecord models since DJ will attempt to reload the model from the database before performing your action. All of the data will be lost. Instead, create a Job class with a #perform method that creates the User.

这篇关于Delayed_job无法运行用户定义的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆