ranking_job.rb
980 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class ProposalsDiscussionPlugin::RankingJob
def perform
ProposalsDiscussionPlugin::Topic.find_each do |topic|
ProposalsDiscussionPlugin::RankingJob::TopicRankingJob.new(topic.id).schedule
end
end
def after(job)
schedule(30.minutes.from_now)
end
def schedule(run_at = nil)
Delayed::Job.enqueue(self, {:run_at => run_at}) unless self.class.find_job.exists?
end
def self.find_job
Delayed::Job.by_handler("--- !ruby/object:ProposalsDiscussionPlugin::RankingJob {}\n").where('locked_at IS NULL')
end
class TopicRankingJob < Struct.new(:topic_id)
def perform
topic = ProposalsDiscussionPlugin::Topic.find_by_id(topic_id)
topic.update_ranking if topic.present?
end
def schedule
Delayed::Job.enqueue(self) unless find_job.exists?
end
def find_job
Delayed::Job.by_handler("--- !ruby/struct:ProposalsDiscussionPlugin::RankingJob::TopicRankingJob\ntopic_id: #{topic_id}\n")
end
end
end