-
Notifications
You must be signed in to change notification settings - Fork 12
/
app.rb
89 lines (71 loc) · 1.92 KB
/
app.rb
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
require 'logger'
require "zlib"
require "base64"
# Lowkiq.build_splitter = ->() { Lowkiq.build_by_node_splitter 2, 0 }
Lowkiq.format_error = -> (error) { error.full_message(highlight: false) }
Lowkiq.dump_error = Proc.new do |msg|
compressed = Zlib::Deflate.deflate(msg.to_s)
Base64.encode64(compressed)
end
Lowkiq.load_error = Proc.new do |input|
decoded = Base64.decode64(input)
Zlib::Inflate.inflate(decoded)
rescue
input
end
$logger = Logger.new(STDOUT)
Lowkiq.server_middlewares << -> (worker, batch, &block) do
$logger.info "Started job for #{worker} #{batch}"
block.call
$logger.info "Finished job for #{worker} #{batch}"
end
Lowkiq.server_middlewares << -> (worker, batch, &block) do
begin
block.call
rescue => e
$logger.error "#{e.message} #{worker} #{batch}"
raise e
end
end
Lowkiq.last_words = ->(ex) { puts ex }
module ATestWorker
extend Lowkiq::Worker
self.max_retry_count = 2
def self.perform_async(jobs)
jobs.each do |job|
job.merge! id: job[:payload][:id]
end
super
end
def self.perform(batch)
sleep Random.rand
if Random.rand(5) == 0
fail "error"
end
end
end
module ATest2Worker
extend Lowkiq::Worker
self.max_retry_count = 2
def self.perform_async(jobs)
jobs.each do |job|
job.merge! id: job[:payload][:id]
end
super
end
def self.perform(batch)
sleep Random.rand
if Random.rand(5) == 0
fail "error"
end
end
end
Lowkiq.workers = [ ATestWorker, ATest2Worker ]
ATestWorker.perform_async 1000.times.map { |id| { payload: {id: id},
perform_in: Time.now.to_f + Random.rand(10)} }
ATest2Worker.perform_async 1000.times.map { |id| { payload: {id: id},
perform_in: Time.now.to_f + Random.rand(10)} }
require 'rack'
Thread.new do
Rack::Handler::WEBrick.run Lowkiq::Web, Port: 8080, Host: '0.0.0.0'
end