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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
# File 'lib/buffered_job/model.rb', line 13
def self.flush!
return if Lock.locked?
begin
Lock.lock!
jobs = self.all
skip = []
@last_results = []
jobs.each do |j|
if skip[j.id]
j.destroy
next
end
cojobs = jobs.select{ |o| o.user_id == j.user_id and o.category == j.category }
receiver = YAML.load(j.receiver)
if cojobs.size > 1
begin
targets = cojobs.map{|c|
YAML.load(c.target)
}
unless receiver.respond_to?(j.merge_method)
raise NoMergeMethodError,"define #{j.merge_method}"
end
if mailer?(receiver)
@last_results << receiver.send(j.merge_method,targets).deliver
else
@last_results << receiver.send(j.merge_method,targets)
end
rescue => er
@last_results << er
end
cojobs.each do |c|
skip[c.id] = true
end
else
begin
target = YAML.load(j.target)
if mailer?(receiver)
@last_results << receiver.send(j.method,target).deliver
else
@last_results << receiver.send(j.method,target)
end
rescue => er
@last_results << er
end
end
j.destroy
end
ensure
Lock.unlock!
end
end
|