Rails 2 上的延迟作业:无法在“执行”中保存方法/如何知道工作何时完成?

发布于 2024-10-08 04:23:29 字数 1534 浏览 0 评论 0原文

我正在使用 Rails 2 的延迟作业插件,每次我尝试修改模型并将其保存在延迟作业所需的“执行”方法中时,它都会失败(没有错误消息或任何内容,它只是列为数据库失败)。

我的 Rails 模型文件之一(视频)中有“执行”方法,并且我将该模型的一个实例(@video,比方说)传递给 Delayed::Job.enqueue

这是一个已知问题吗?在队列中无法进行数据库修改吗?我做错了什么吗(它仅在尝试保存时失败,而不是在我实际更改属性时失败,这听起来像是数据库修改问题)。

如果这是预期的:我该如何解决它?我正在尝试将“完成”属性保存为 true,以便我知道模型何时准备好进入下一步。是否有一些标准方法可以确定延迟的工作何时完成?

编辑:我已经确认调用独立执行(没有延迟作业)在保存方面没有问题(没有错误或警告,或任何东西)。当我通过 DelayedJobs 调用它时,它在到达保存行后立即失败(没有超时)。

编辑:等等,我想我明白发生了什么:我的“执行”是“after_create”回调的一部分......这一切都很好,直到我尝试保存。看起来当我保存时,它会再次调用执行(虽然已经在执行中),但这并不适用于延迟作业(也不应该)。由于某种原因,我认为 after_create 只会被调用一次(而不是在每次保存后)。等等,一个简单的测试就表明情况确实如此。嗯...那么为什么在延迟作业中,当我保存时,执行会被调用两次,而当我不保存时,会调用一次呢?

我的代码:

after_create :start_transcodes

 def start_transcodes
     Delayed::Job.enqueue self  
 end
     def perform
      puts "performing"
      self.flash_status = 100
      self.save!
      puts "done"
     end

我所看到的:

    performing
    performing
2 jobs processed at 3.3406 j/s, 2 failed ...

我没有看到它说“完成”。

我在 Rails 日志中看到的是:

"* [JOB] Video failed with NameError: undefined local variable or method `flush_deletes' for #<Paperclip::Attachment:0xb6e51da0> - 2 failed attempts
undefined local variable or method `flush_deletes' for #<Paperclip::Attachment:0xb6e51da0>"

我正在为此类使用回形针插件,并且我可以全天调用 save(即使在该执行方法中)并且没有任何问题。我也可以整天调用 save(再次,即使是在执行中),并且不会看到我的 after_create 方法被多次调用 - 除非我使用延迟作业。(可能是它进行某种自动重试?)

我会看看我的回形针插件,看看发生了什么......

I'm using the Delayed Jobs plugin for Rails 2, and every time I try to modify a model and save it in the "perform" method required by Delayed Jobs, it fails out (no error messages or anything, it's just listed as a failure in the database).

I have the "perform" method in one of my rails model files (Video), and I'm passing an instance of that model (@video, let's say) to the Delayed::Job.enqueue

Is it a known issue that you can't do database modifications while in the queue? Am I doing something wrong (it only fails when it tries to save, not when I'm actually changing the attributes, and that sounds like a database modification issue).

If this IS expected: How can I fix it? I'm trying to save a "done" attribute to true, so I know when the model is ready to get to the next step. Is there some standard way to figure out when a delayed job is done?

EDIT: I have confirmed that calling perform standalone (without delayed job) has no problems with saving (no errors or warnings, or anything). When I call it through DelayedJobs it fails IMMEDIATELY (no time out) the second it gets to the save line.

EDIT: Wait, I think I see what is going on: my "perform" is part of an "after_create" call back... Which is all well and good, until I try to SAVE. It looks like when I save, it calls perform AGAIN (while already in perform), and that doesn't fly with Delayed Jobs (nor should it). For some reason I thought after_create would only get called once (not after every save). Wait, a simple test just showed that that IS the case. Hrrm... So why is perform called twice when I save, and once when I don't, in delayed jobs?

My code:

after_create :start_transcodes

 def start_transcodes
     Delayed::Job.enqueue self  
 end
     def perform
      puts "performing"
      self.flash_status = 100
      self.save!
      puts "done"
     end

What I see:

    performing
    performing
2 jobs processed at 3.3406 j/s, 2 failed ...

I don't see it say "done" ever.

What I DO see in my rails log is:

"* [JOB] Video failed with NameError: undefined local variable or method `flush_deletes' for #<Paperclip::Attachment:0xb6e51da0> - 2 failed attempts
undefined local variable or method `flush_deletes' for #<Paperclip::Attachment:0xb6e51da0>"

I am using the paperclip plugin for this class, and I can call save all day (even in that perform method) and get no problems. I ALSO can call save(again, even in perform) all day and not see my after_create method called more than once--UNLESS I"m using Delayed Job. (might be it doing some sort of auto retry?)

I'm gonna look around my paperclip plugin, see what's going on...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

仅此而已 2024-10-15 04:23:29

如果您的保存失败,则与delayed_job无关(至少不应该如此,除非保存时间超过MAX_RUN_TIME。)

尝试通过不使用delayed_job来诊断保存问题。

另请查看日志中的delayed_job.log 文件

If your save fails, its got nothing to do with delayed_job ( atleast it shouldn't be unless the save takes longer than the MAX_RUN_TIME. )

Try diagnosing the problem with the save, by not using delayed_job.

Also take a look at the delayed_job.log file in your logs

眼眸里的那抹悲凉 2024-10-15 04:23:29

好吧,不确定到底发生了什么,但我在我的 lib 目录中创建了一个骨架“TranscodeJob”类。这个类通过引用我想要它处理的视频进行初始化,然后处理它,保存它,并与延迟作业很好地配合。

基本上,看起来将我的整个复杂的 Video 对象(包含回形针插件)传递给 Delayed Job 是一件很可怕的事情,而传递一个简单的对象(没有比它需要的更多信息)会让事情变得容易得多。

下面是我使用的代码,它工作得很好(如果工作得很好,我可以一点一点地添加我长时间运行的代码,并确认它继续这样做,但它之前工作得很好,只是保存时打嗝)

class TranscodeJob
 def initialize(video_id)
     @video_id = video_id           
 end

 #delayed jobs expected method
 def perform
      @video = Video.find(@video_id)
      @video.flash_status = 100
      @video.save!
 end

结束

这代码仍然是从 after_create 过滤器中调用的,而且我没有看到它被调用两次,所以看起来我误以为 DelayedJobs 自动重试是递归之类的。

Okay, not sure EXACTLY what was happening, but I've made a skeleton "TranscodeJob" class in my lib directory. This class gets initialized with a reference to what video I want it to process, and processes it, and saves it, and plays nicely with Delayed Job.

Basically, it looks like passing my entire complicated Video object (complete with paperclip plugins) to Delayed Job was freaking things out, and passing a simple object, with no more info than it needs makes things much easier.

Below is the code I used, which works just fine (and if that works fine, i can add my long running code back little by little and confirm it continues to do so, but it worked fine before, just hiccuped with saving)

class TranscodeJob
 def initialize(video_id)
     @video_id = video_id           
 end

 #delayed jobs expected method
 def perform
      @video = Video.find(@video_id)
      @video.flash_status = 100
      @video.save!
 end

end

This code is STILL called from a after_create filter, and I'm not seeing it called twice, so it looks like I mistoke DelayedJobs auto-retry for recursion, or something.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文