除了 Active Job 之外,是否有 ApplicationController 的等价物?
Is there an equivalent of ApplicationController but for Active Job?
我在我的 Rails 应用程序中使用 ActiveJob
并尝试添加一些应该由我的所有作业共享的通用功能(确切地说是异常处理)。
一个例子是 -
class MyAbstractJob < ActiveJob::Base
rescue_from StandardError do |exception|
# do exception handling stuff ...
end
end
class FooJob < MyAbstractJob
queue_as :my_queue
def perform *args
# do job stuff ...
end
end
此模式类似于对控制器使用 ApplicationController
,它继承自 ActionController::Base
并提供某个地方来实现任何控制器子类上可用的共享设施。
这实际上似乎在大多数情况下都有效,除非我尝试调用 queue_as :my_queue
- 在这种情况下我最终会看到 -
NoMethodError: undefined method 'queue_as' for #<Class:0x007f088905c970>
我在 Rails 文档中没有看到对这个特定模式的引用。有谁知道一个好的方法吗?
这很奇怪,我认为你的代码是正确的。我已经按原样测试了您的代码,没有进行任何更改,也没有发现任何问题:
FooJob.perform_later
# Performing FooJob from Inline(my_queue)
# Performed FooJob from Inline(my_queue) in 3.81ms
# Enqueued FooJob (Job ID: 6fd5ea5c-06bb-46d8-943c-8cc6f1e6ac33) to Inline(my_queue)
# => #<FooJob:0x0000000464de90 @arguments=[], @job_id="6fd5ea5c-06bb-46d8-943c-8cc6f1e6ac33", @queue_name="my_queue">
我已经将两个 class 都放入了 app/jobs
目录。
您在问题中显示的错误似乎与继承相关。您确定您的 FooJob
class 真的继承自 MyAbstractJob
吗?也许您可能还需要重新加载 Rails 才能确定。
如果您的问题仍然存在,请post真正的classes代码,而不是简化的示例。
我在我的 Rails 应用程序中使用 ActiveJob
并尝试添加一些应该由我的所有作业共享的通用功能(确切地说是异常处理)。
一个例子是 -
class MyAbstractJob < ActiveJob::Base
rescue_from StandardError do |exception|
# do exception handling stuff ...
end
end
class FooJob < MyAbstractJob
queue_as :my_queue
def perform *args
# do job stuff ...
end
end
此模式类似于对控制器使用 ApplicationController
,它继承自 ActionController::Base
并提供某个地方来实现任何控制器子类上可用的共享设施。
这实际上似乎在大多数情况下都有效,除非我尝试调用 queue_as :my_queue
- 在这种情况下我最终会看到 -
NoMethodError: undefined method 'queue_as' for #<Class:0x007f088905c970>
我在 Rails 文档中没有看到对这个特定模式的引用。有谁知道一个好的方法吗?
这很奇怪,我认为你的代码是正确的。我已经按原样测试了您的代码,没有进行任何更改,也没有发现任何问题:
FooJob.perform_later
# Performing FooJob from Inline(my_queue)
# Performed FooJob from Inline(my_queue) in 3.81ms
# Enqueued FooJob (Job ID: 6fd5ea5c-06bb-46d8-943c-8cc6f1e6ac33) to Inline(my_queue)
# => #<FooJob:0x0000000464de90 @arguments=[], @job_id="6fd5ea5c-06bb-46d8-943c-8cc6f1e6ac33", @queue_name="my_queue">
我已经将两个 class 都放入了 app/jobs
目录。
您在问题中显示的错误似乎与继承相关。您确定您的 FooJob
class 真的继承自 MyAbstractJob
吗?也许您可能还需要重新加载 Rails 才能确定。
如果您的问题仍然存在,请post真正的classes代码,而不是简化的示例。