例如,我有一个 ice_cube 重复规则,它每天都会发生。此规则的目标是每天轮换用户。
例如,如果我有 4 个用户:John、Peter、Mark、Matthew 和每日重复规则。我还使用延迟作业来创建单个作业以在 next_occurence 上运行,这将轮换 schedule_users 表中的用户(通过更改优先级值)。我一次只创建一个作业,例如,当执行第一次轮换时,将为下一次创建一个新作业。
我想查看哪些用户被安排在特定的一天。我将如何展望未来 3、5、7 等或未来任意天数的用户。
class Schedule < ApplicationRecord
belongs_to :project
belongs_to :user
has_many :schedule_users, -> { order priority: :desc }, dependent: :destroy
has_many :users, through: :schedule_users
enum frequency: { daily: 0, weekly: 1, biweekly: 2, monthly: 3 }
validates_presence_of :name, :start, :frequency, :project
def next_occurrence
ice_cube_schedule.next_occurrence.start_time
end
def jobs
Delayed::Job.where('handler LIKE ?', "%Schedule/#{id}\%")
end
def on_call_user
schedule_users.max_by(&:priority).user if users.present?
end
def priority(user)
schedule_users.where(user: user).first.try(:priority)
end
def ice_cube_schedule
schedule = IceCube::Schedule.new(start, end_time: self.end)
case frequency
when 'daily'
schedule.add_recurrence_rule IceCube::Rule.daily(1)
when 'weekly'
schedule.add_recurrence_rule IceCube::Rule.weekly(1)
when 'biweekly'
schedule.add_recurrence_rule IceCube::Rule.weekly(2)
when 'monthly'
schedule.add_recurrence_rule IceCube::Rule.monthly(1)
end
schedule
end
end
class ScheduleUser < ApplicationRecord
belongs_to :schedule
belongs_to :user
validates_presence_of :priority, :schedule, :user
end
class ReprioritizeScheduleUsersJob < ApplicationJob
queue_as :default
after_perform do |job|
schedule = job.arguments.first
ReprioritizeScheduleUsersJob.set(wait_until: schedule.next_occurrence).perform_later(schedule)
end
def perform(schedule)
ActiveRecord::Base.transaction do
schedule_users = schedule.schedule_users.reload
num_users = schedule_users.count
schedule.schedule_users.rotate.each_with_index do |schedule_user, index|
schedule_user.update(priority: num_users - index)
end
end
end
end