将元数据保存到 Sidekiq 上的作业
Save metadata to jobs on Sidekiq
使用 Sidekiq 时是否可以将元数据保存到作业中?
例如,我想将验证作为后台作业执行,这样当它完成时,遇到的任何错误都将作为元数据保存在作业中。
如果可能的话,我还能在作业完成或停止后恢复此元数据吗?
提前致谢。
作业完成后无法恢复任何状态。
在你的情况下,你似乎需要将数据保存或发送到其他地方(如数据库)以便稍后读取并采取行动。
Sidekiq 不是直接开箱即用的,但我已经用 sidekiq-status
完成了这个
例如,在您的场景中,它看起来像这样:
class ValidatorJob
include Sidekiq::Worker
include Sidekiq::Status::Worker
def perform(*args)
# Run validations
# after they are done, you can store any data with the store method
store attr1: 'failed'
end
end
是的,Sidekiq 提供 中间件(客户端和服务器)以及向作业添加元数据的可能性。
def call(worker_class, job, queue, redis_pool)
# return false/nil to stop the job from going to redis
return false if queue != 'default'
job['customer'] = Customer.current_id
yield
end
查看 this link 文档。
使用 Sidekiq 时是否可以将元数据保存到作业中?
例如,我想将验证作为后台作业执行,这样当它完成时,遇到的任何错误都将作为元数据保存在作业中。
如果可能的话,我还能在作业完成或停止后恢复此元数据吗?
提前致谢。
作业完成后无法恢复任何状态。
在你的情况下,你似乎需要将数据保存或发送到其他地方(如数据库)以便稍后读取并采取行动。
Sidekiq 不是直接开箱即用的,但我已经用 sidekiq-status
完成了这个例如,在您的场景中,它看起来像这样:
class ValidatorJob
include Sidekiq::Worker
include Sidekiq::Status::Worker
def perform(*args)
# Run validations
# after they are done, you can store any data with the store method
store attr1: 'failed'
end
end
是的,Sidekiq 提供 中间件(客户端和服务器)以及向作业添加元数据的可能性。
def call(worker_class, job, queue, redis_pool)
# return false/nil to stop the job from going to redis
return false if queue != 'default'
job['customer'] = Customer.current_id
yield
end
查看 this link 文档。