bushpig 0.1.1 → 0.1.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/bin/bushpig +6 -0
- data/gemset.nix +1 -1
- data/lib/bushpig.rb +3 -3
- data/lib/bushpig/client.rb +5 -3
- data/lib/bushpig/job.rb +16 -6
- data/lib/bushpig/server.rb +27 -10
- data/lib/bushpig/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8d3c78ffe7777e02a7e1a7d4f4e1838ab47c80a5c9d0c1d61e6547c2b956065f
|
4
|
+
data.tar.gz: 64bc3d1e149b54e84a28e9f637b9495b7a6e9d7eec451304e310afd8bb473067
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9a1ca507019ef95f735f9b0558d1b4867fccec40a6fac62ced66f69d0ab57168a081af14dd661876221213c92e3c184393dabedefe8f706a1a12aa6354955bdd
|
7
|
+
data.tar.gz: 7148a4dabeff3df6575b455a08306342d9b488938593841882ed025e1f7a6fcbb35d22c5f29554a2269c78f3aa853808c84570d889aca8fadcac55ef5a362ec7
|
data/Gemfile.lock
CHANGED
data/bin/bushpig
CHANGED
data/gemset.nix
CHANGED
data/lib/bushpig.rb
CHANGED
@@ -43,11 +43,11 @@ module Bushpig
|
|
43
43
|
defined?(Bushpig::CLI)
|
44
44
|
end
|
45
45
|
|
46
|
-
def self.
|
46
|
+
def self.queue_key(queue)
|
47
47
|
"set:#{queue}"
|
48
48
|
end
|
49
49
|
|
50
|
-
def self.job_key(
|
51
|
-
"job:#{
|
50
|
+
def self.job_key(job_key)
|
51
|
+
"job:#{job_key}"
|
52
52
|
end
|
53
53
|
end
|
data/lib/bushpig/client.rb
CHANGED
@@ -4,9 +4,10 @@ module Bushpig
|
|
4
4
|
class Client
|
5
5
|
attr_accessor :default_ttl
|
6
6
|
|
7
|
-
def initialize(pool)
|
7
|
+
def initialize(pool, &callback)
|
8
8
|
@pool = pool
|
9
9
|
@default_ttl = nil
|
10
|
+
@callback = callback
|
10
11
|
end
|
11
12
|
|
12
13
|
def redis_pool
|
@@ -18,9 +19,10 @@ module Bushpig
|
|
18
19
|
end
|
19
20
|
|
20
21
|
def submit(queue, job, score: default_score, ttl: default_ttl)
|
22
|
+
modified = @callback.call(job)
|
21
23
|
redis_pool.with do |conn|
|
22
|
-
conn.set(Bushpig.job_key(
|
23
|
-
conn.zadd(Bushpig.
|
24
|
+
conn.set(Bushpig.job_key(modified.job_key), modified.job_payload, ex: ttl)
|
25
|
+
conn.zadd(Bushpig.queue_key(queue), score, modified.job_key)
|
24
26
|
end
|
25
27
|
end
|
26
28
|
end
|
data/lib/bushpig/job.rb
CHANGED
@@ -16,8 +16,12 @@ module Bushpig
|
|
16
16
|
@job_id = job_id
|
17
17
|
end
|
18
18
|
|
19
|
+
def job_key
|
20
|
+
job_id
|
21
|
+
end
|
22
|
+
|
19
23
|
def job_payload
|
20
|
-
JSON.generate({ class: self.class.name, args: each.to_a })
|
24
|
+
JSON.generate({ class: self.class.name, id: job_id, args: each.to_a })
|
21
25
|
end
|
22
26
|
end
|
23
27
|
end
|
@@ -27,24 +31,30 @@ module Bushpig
|
|
27
31
|
@@unique_key = unique_key
|
28
32
|
|
29
33
|
def job_id
|
34
|
+
@job_id ||= SecureRandom.hex(32)
|
35
|
+
end
|
36
|
+
|
37
|
+
def job_id=(job_id)
|
38
|
+
@job_id = job_id
|
39
|
+
end
|
40
|
+
|
41
|
+
def job_key
|
30
42
|
@@unique_key.inject(Digest::SHA256.new) do |digest, key|
|
31
43
|
digest.update(self[key].to_s)
|
32
44
|
end.hexdigest
|
33
45
|
end
|
34
46
|
|
35
|
-
def job_id=(job_id); end
|
36
|
-
|
37
47
|
def job_payload
|
38
|
-
JSON.generate({ class: self.class.name, args: each.to_a })
|
48
|
+
JSON.generate({ class: self.class.name, id: job_id, args: each.to_a })
|
39
49
|
end
|
40
50
|
end
|
41
51
|
end
|
42
52
|
|
43
|
-
def self.hydrate(
|
53
|
+
def self.hydrate(job_payload)
|
44
54
|
h = JSON.parse(job_payload, symbolize_names: true)
|
45
55
|
klass = const_get(h[:class])
|
46
56
|
job = klass.new(*h[:args])
|
47
|
-
job.job_id =
|
57
|
+
job.job_id = h[:id]
|
48
58
|
job
|
49
59
|
end
|
50
60
|
end
|
data/lib/bushpig/server.rb
CHANGED
@@ -19,10 +19,16 @@ module Bushpig
|
|
19
19
|
puts 'INT received, shutdown flagged'
|
20
20
|
@done = true
|
21
21
|
end
|
22
|
+
Signal.trap('TERM') do
|
23
|
+
reset_signals
|
24
|
+
puts 'TERM received, shutdown flagged'
|
25
|
+
@done = true
|
26
|
+
end
|
22
27
|
end
|
23
28
|
|
24
29
|
def reset_signals
|
25
30
|
Signal.trap('INT', 'DEFAULT')
|
31
|
+
Signal.trap('TERM', 'DEFAULT')
|
26
32
|
end
|
27
33
|
|
28
34
|
def serve(queue)
|
@@ -42,18 +48,29 @@ module Bushpig
|
|
42
48
|
end
|
43
49
|
|
44
50
|
def handle(job)
|
45
|
-
puts "Job starting: jid-#{job.job_id} #{job}"
|
51
|
+
puts "Job starting: jid-#{job.job_id} jkey-#{job.job_key} #{job}"
|
46
52
|
started = monotonic_time
|
47
53
|
@handler.call(job)
|
48
54
|
finished = monotonic_time
|
49
55
|
elapsed = finished - started
|
50
56
|
puts "Job completed: jid-#{job.job_id} #{elapsed} seconds"
|
51
57
|
rescue StandardError => e
|
52
|
-
# Job handler raised exception
|
53
58
|
finished = monotonic_time
|
54
59
|
elapsed = finished - started
|
55
60
|
puts "Job raised exception: jid-#{job.job_id} #{elapsed} seconds: #{e}"
|
56
|
-
|
61
|
+
notify_exception(job, e)
|
62
|
+
end
|
63
|
+
|
64
|
+
def honeybadger
|
65
|
+
@honeybadger ||= begin
|
66
|
+
Honeybadger
|
67
|
+
rescue NameError
|
68
|
+
nil
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def notify_exception(job, exception)
|
73
|
+
honeybadger&.notify(exception, context: { job_id: job.job_id, job_key: job.job_key, job: job.to_s })
|
57
74
|
end
|
58
75
|
|
59
76
|
def monotonic_time
|
@@ -63,7 +80,7 @@ module Bushpig
|
|
63
80
|
def fetch(queue)
|
64
81
|
redis_pool.with do |conn|
|
65
82
|
begin
|
66
|
-
res = conn.bzpopmin(Bushpig.
|
83
|
+
res = conn.bzpopmin(Bushpig.queue_key(queue), @timeout)
|
67
84
|
rescue Redis::TimeoutError
|
68
85
|
# TODO: warn user (once) that redis timeout set lower than pop timeout
|
69
86
|
conn.close
|
@@ -71,20 +88,20 @@ module Bushpig
|
|
71
88
|
end
|
72
89
|
return nil if res.nil?
|
73
90
|
|
74
|
-
(_set,
|
75
|
-
# conn.sadd('running',
|
91
|
+
(_set, key, _score) = res
|
92
|
+
# conn.sadd('running', key)
|
76
93
|
|
77
|
-
payload = conn.get(Bushpig.job_key(
|
94
|
+
payload = conn.get(Bushpig.job_key(key))
|
78
95
|
return nil if payload.nil? # most likely job expired
|
79
96
|
|
80
|
-
Bushpig::Job.hydrate(
|
97
|
+
Bushpig::Job.hydrate(payload)
|
81
98
|
end
|
82
99
|
end
|
83
100
|
|
84
101
|
def complete(job)
|
85
102
|
redis_pool.with do |conn|
|
86
|
-
# conn.srem('running', job.
|
87
|
-
conn.del(Bushpig.job_key(job.
|
103
|
+
# conn.srem('running', job.job_key)
|
104
|
+
conn.del(Bushpig.job_key(job.job_key))
|
88
105
|
end
|
89
106
|
end
|
90
107
|
end
|
data/lib/bushpig/version.rb
CHANGED