bushpig 0.1.4 → 0.1.8.1
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/gemset.nix +1 -1
- data/lib/bushpig.rb +3 -3
- data/lib/bushpig/client.rb +5 -3
- data/lib/bushpig/job.rb +31 -10
- data/lib/bushpig/server.rb +15 -9
- 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: 953cc975f6a214726b0991f0b856032d509815a8fdbaa5e99297f261508e96fd
|
4
|
+
data.tar.gz: 13abb819a5af786cd90e1f65962b81529c7df023395592a721399f11397891b8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0faa12236f02cdde323dbfca852613ea1b674e0928ad3e5f1f81267fc0611e04c2bcfaa4ecc4edbd0eb48d34ef141726a11c14bbc4dc52b775ae68dbc989b6f7
|
7
|
+
data.tar.gz: 71eb9f88710fd21e9a339706061e197c720af991077c92f2d0c77b80a984295ff8ad65a0d8456ba8e2563467443b01d5d038e61edb4f7f87d37736e7e048c3da
|
data/Gemfile.lock
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
@@ -6,8 +6,14 @@ require 'json'
|
|
6
6
|
|
7
7
|
module Bushpig
|
8
8
|
module Job
|
9
|
-
def self.job(*args)
|
9
|
+
def self.job(handler, *args)
|
10
10
|
Struct.new(*args) do
|
11
|
+
@job_handler = handler
|
12
|
+
|
13
|
+
def self.job_handler
|
14
|
+
@job_handler
|
15
|
+
end
|
16
|
+
|
11
17
|
def job_id
|
12
18
|
@job_id ||= SecureRandom.hex(32)
|
13
19
|
end
|
@@ -16,35 +22,50 @@ module Bushpig
|
|
16
22
|
@job_id = job_id
|
17
23
|
end
|
18
24
|
|
25
|
+
def job_key
|
26
|
+
job_id
|
27
|
+
end
|
28
|
+
|
19
29
|
def job_payload
|
20
|
-
JSON.generate({ class: self.class.name, args: each.to_a })
|
30
|
+
JSON.generate({ class: self.class.name, id: job_id, args: each.to_a })
|
21
31
|
end
|
22
32
|
end
|
23
33
|
end
|
24
34
|
|
25
|
-
def self.keyed(unique_key, *args)
|
35
|
+
def self.keyed(handler, unique_key, *args)
|
26
36
|
Struct.new(*args) do
|
27
|
-
|
37
|
+
@job_handler = handler
|
38
|
+
@unique_key = unique_key
|
39
|
+
|
40
|
+
def self.job_handler
|
41
|
+
@job_handler
|
42
|
+
end
|
28
43
|
|
29
44
|
def job_id
|
30
|
-
|
45
|
+
@job_id ||= SecureRandom.hex(32)
|
46
|
+
end
|
47
|
+
|
48
|
+
def job_id=(job_id)
|
49
|
+
@job_id = job_id
|
50
|
+
end
|
51
|
+
|
52
|
+
def job_key
|
53
|
+
@unique_key.inject(Digest::SHA256.new) do |digest, key|
|
31
54
|
digest.update(self[key].to_s)
|
32
55
|
end.hexdigest
|
33
56
|
end
|
34
57
|
|
35
|
-
def job_id=(job_id); end
|
36
|
-
|
37
58
|
def job_payload
|
38
|
-
JSON.generate({ class: self.class.name, args: each.to_a })
|
59
|
+
JSON.generate({ class: self.class.name, id: job_id, args: each.to_a })
|
39
60
|
end
|
40
61
|
end
|
41
62
|
end
|
42
63
|
|
43
|
-
def self.hydrate(
|
64
|
+
def self.hydrate(job_payload)
|
44
65
|
h = JSON.parse(job_payload, symbolize_names: true)
|
45
66
|
klass = const_get(h[:class])
|
46
67
|
job = klass.new(*h[:args])
|
47
|
-
job.job_id =
|
68
|
+
job.job_id = h[:id]
|
48
69
|
job
|
49
70
|
end
|
50
71
|
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,7 +48,7 @@ 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
|
@@ -64,7 +70,7 @@ module Bushpig
|
|
64
70
|
end
|
65
71
|
|
66
72
|
def notify_exception(job, exception)
|
67
|
-
honeybadger&.notify(exception, context: { job_id: job.job_id, job: job.to_s })
|
73
|
+
honeybadger&.notify(exception, context: { job_id: job.job_id, job_key: job.job_key, job: job.to_s })
|
68
74
|
end
|
69
75
|
|
70
76
|
def monotonic_time
|
@@ -74,7 +80,7 @@ module Bushpig
|
|
74
80
|
def fetch(queue)
|
75
81
|
redis_pool.with do |conn|
|
76
82
|
begin
|
77
|
-
res = conn.bzpopmin(Bushpig.
|
83
|
+
res = conn.bzpopmin(Bushpig.queue_key(queue), @timeout)
|
78
84
|
rescue Redis::TimeoutError
|
79
85
|
# TODO: warn user (once) that redis timeout set lower than pop timeout
|
80
86
|
conn.close
|
@@ -82,20 +88,20 @@ module Bushpig
|
|
82
88
|
end
|
83
89
|
return nil if res.nil?
|
84
90
|
|
85
|
-
(_set,
|
86
|
-
# conn.sadd('running',
|
91
|
+
(_set, key, _score) = res
|
92
|
+
# conn.sadd('running', key)
|
87
93
|
|
88
|
-
payload = conn.get(Bushpig.job_key(
|
94
|
+
payload = conn.get(Bushpig.job_key(key))
|
89
95
|
return nil if payload.nil? # most likely job expired
|
90
96
|
|
91
|
-
Bushpig::Job.hydrate(
|
97
|
+
Bushpig::Job.hydrate(payload)
|
92
98
|
end
|
93
99
|
end
|
94
100
|
|
95
101
|
def complete(job)
|
96
102
|
redis_pool.with do |conn|
|
97
|
-
# conn.srem('running', job.
|
98
|
-
conn.del(Bushpig.job_key(job.
|
103
|
+
# conn.srem('running', job.job_key)
|
104
|
+
conn.del(Bushpig.job_key(job.job_key))
|
99
105
|
end
|
100
106
|
end
|
101
107
|
end
|
data/lib/bushpig/version.rb
CHANGED