sidekiq 使用

sidekiq 使用

配置好sidekiq和redis后首先新建job

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class FinishCourseJob < ApplicationJob
queue_as :course
# 上完课后完成课程
def perform(id, finished_at)
@@log = Logger.new('log/delayed_job.log')
course = Course.find_by(id: id)
@@log.info "当前课程状态:#{course.status}"
if course.online? && finished_at == course.finished_at.to_i && course.may_finish?
@@log.info '-------------完成课程---------------'
course.finish!
end
end
end

使用方法

1
2
3
4
5
6
7
8
# 立即执行
FinishCourseJob.perform_now(id)
# 常规异步执行,有空闲就执行
FinishCourseJob.perform_later(id)
# 指定延迟多久执行
FinishCourseJob.set( wait: 20.minutes ).perform_later
# 指定执行时间
FinishCourseJob.set(wait_until: start_registration_at).perform_later(id, start_registration_at.to_i)