Active Job Base¶ ↑
Active Job 对象可以配置为使用不同的后端队列框架。要指定要使用的队列适配器,
ActiveJob::Base.queue_adapter = :inline
可以在 QueueAdapters 中找到支持的适配器列表。
可以通过创建一个继承自 ActiveJob::Base 类的类来定义 Active Job 对象。唯一需要实现的方法是“perform”方法。
定义 Active Job 对象
class ProcessPhotoJob < ActiveJob::Base def perform(photo) photo.watermark!('Rails') photo.rotate!(90.degrees) photo.resize_to_fit!(300, 300) photo.upload! end end
传递的记录使用 Global ID 进行序列化/反序列化。更多信息可以在 ActiveJob::Arguments 中找到。
要将一个任务排队,以便在队列系统空闲时尽快执行
ProcessPhotoJob.perform_later(photo)
要将一个任务排队,以便在将来的某个时间点处理
ProcessPhotoJob.set(wait_until: Date.tomorrow.noon).perform_later(photo)
更多信息可以在 ActiveJob::Core::ClassMethods#set 中找到。
也可以立即处理一个任务,而无需发送到队列
ProcessPhotoJob.perform_now(photo)
Exceptions¶ ↑
-
DeserializationError- 反序列化错误的错误类。 -
SerializationError- 序列化错误的错误类。
包含的模块