简体   繁体   English

delayed_job不适用于Rails ActiveRecord范围

[英]delayed_job doesn't work with Rails ActiveRecord scope

I have delayed_job 3.0.2 installed, and it works with method calls on objects. 我安装了delayed_job 3.0.2,它适用于对象的方法调用。 However, if I call a scope on a class, for example, 但是,如果我在类上调用范围,例如,

Listing.delay.all

then I get error stack level too deep . 然后我得到错误stack level too deep This happens if I call any scope on any class I have. 如果我在任何类上调用任何范围,就会发生这种情况。

Is this error by design? 这是设计错误吗? What's the reason I get stack level too deep error here? 我在这里得到stack level too deep错误是什么原因?

Thank you. 谢谢。

Using delayed_job properly 正确使用delayed_job

To answer your question indirectly, it looks like you're not using delayed_job right. 要间接回答您的问题,看起来您没有正确使用delayed_job。 I'll explain what your code does and suggest what you probably are trying to do. 我会解释你的代码做了什么,并建议你可能正在尝试做什么。

Listing.delay.all

The method following delay (in this case, all ) will get executed in the background. delay的方法(在本例中为all )将在后台执行。 Instead of returning an Array of Listings, it will return a Delayed::Backend::ActiveRecord::Job object. 它将返回一个Delayed :: Backend :: ActiveRecord :: Job对象,而不是返回一个列表数组。 This isn't what is happening in your case, but I'll get to that. 这不是你的情况,但我会做到这一点。

Any job that you background should have a side effect , since the return value of the delayed jobs are not stored. 由于未存储延迟作业的返回值,因此您执行的任何作业都应具有副作用 Usually, the side effect is to store something in a database, create a file, or something else that can be detected and used later. 通常,副作用是将某些内容存储在数据库中,创建文件或其他可以在以后检测和使用的内容。 By looking at the delayed_job jobs table, you can see that the return value is not stored. 通过查看delayed_job作业表,您可以看到未存储返回值。

> Delayed::Backend::ActiveRecord::Job.column_names
 => ["id", "priority", "attempts", "handler", "last_error", "run_at", "locked_at", "failed_at", "locked_by", "queue", "created_at", "updated_at"]

That said, the Listing.all and all the other scope methods do not have any side effects; 也就是说, Listing.all和所有其他范围方法都没有任何副作用; they only looks up the scoped Listings and return them. 他们只查找范围的列表并返回它们。 When using delayed_job, be sure to use it only on methods that have side effects, such as updating the database, etc. 使用delayed_job时,请务必仅在具有副作用的方法上使用它,例如更新数据库等。

Unfortunately, without knowing what you're trying to accomplish, it's hard to give advice on how to use delayed_job in your scenario, or even if it's the right tool for the job. 不幸的是,在不知道你想要完成什么的情况下,很难就如何在你的场景中使用delayed_job提出建议,或者即使它是适合这项工作的工具。

Your error message - stack level too deep 您的错误消息 - 堆栈级别太深

First, I'll say that getting a stack level too deep error on Listing.delay.all is not normal. 首先,我会说在Listing.delay.all上获得堆栈级别太深的错误是不正常的。 I was able to use it on an ActiveRecord model User in my Rails 3 app with delayed_job 3.0.2, and it worked fine. 我能够在我的Rails 3应用程序中使用delayed_job 3.0.2在ActiveRecord模型User上使用它,并且它工作正常。 (It didn't do anything worthwhile, but it returned a Job instead of throwing the error you got.) (它没有做任何有价值的事情,但它返回了一个Job,而不是抛出你得到的错误。)

> User.delay.all
 => #<Delayed::Backend::ActiveRecord::Job id: 1, priority: 0, attempts: 0, handler: "--- !ruby/object:Delayed::PerformableMethod\nobject:...", last_error: nil, run_at: "2012-05-02 02:10:39", locked_at: nil, failed_at: nil, locked_by: nil, queue: nil, created_at: "2012-05-02 02:10:39", updated_at: "2012-05-02 02:10:39"> 

Again, there's not much anyone can do to help you figure out that error without more information. 同样,没有任何人可以帮助您在没有更多信息的情况下找出错误。 I recommend starting with figuring out if delayed_job is really the right tool for what you're doing (it's not if you're using it to get data to pass to a view), use it properly, and then see if you're still having the issue. 我建议首先弄清楚delayed_job是否真的是你正在做的事情的正确工具(如果你用它来将数据传递到一个视图就不是这样),正确使用它,然后看看你是否还在有问题。

You should create a custom job and then enqueue it. 您应该创建一个自定义作业,然后将其排入队列。

class UserJob < Struct.new
  def perform
    User.all
  end
end

Delayed::Job.enqueue UserJob.new()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM