bushpig 0.1.6 → 0.1.11
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 +3 -3
- data/lib/bushpig/job.rb +43 -10
- data/lib/bushpig/server.rb +9 -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: 6e851a30d506853e4b90dc634602965ea520d0499a72edf92c5ee0cf3ea39f10
|
4
|
+
data.tar.gz: ef79c0b258b38f10739dd3b6c5496afd2547401bf65586f381b700e582a5d651
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 928d495049bd4d9ab65d6255f4f8743bae42de26eb66f7d5f3ba84992536c90b27daca7693b90f6de85d58058ed8739d0a1db2fcd814f18c504049dc0cb5fa34
|
7
|
+
data.tar.gz: 5708e74eff120f8b9e8488d72debe2a573ff53f1ed2b99408d80faf40d1e2e0511bce5f197e468f0efb9e84c0c85f2906f439f726b118a6ad9fa489a2d70e78b
|
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
@@ -19,10 +19,10 @@ module Bushpig
|
|
19
19
|
end
|
20
20
|
|
21
21
|
def submit(queue, job, score: default_score, ttl: default_ttl)
|
22
|
-
@callback.call(job)
|
22
|
+
modified = @callback.call(job)
|
23
23
|
redis_pool.with do |conn|
|
24
|
-
conn.set(Bushpig.job_key(
|
25
|
-
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)
|
26
26
|
end
|
27
27
|
end
|
28
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,62 @@ 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 })
|
31
|
+
end
|
32
|
+
|
33
|
+
def handle
|
34
|
+
self.class.job_handler.call(self)
|
21
35
|
end
|
22
36
|
end
|
23
37
|
end
|
24
38
|
|
25
|
-
def self.keyed(unique_key, *args)
|
39
|
+
def self.keyed(handler, unique_key, *args)
|
26
40
|
Struct.new(*args) do
|
27
|
-
|
41
|
+
@job_handler = handler
|
42
|
+
@unique_key = unique_key
|
43
|
+
|
44
|
+
def self.job_handler
|
45
|
+
@job_handler
|
46
|
+
end
|
47
|
+
|
48
|
+
def self.job_unique_key
|
49
|
+
@unique_key
|
50
|
+
end
|
28
51
|
|
29
52
|
def job_id
|
30
|
-
|
53
|
+
@job_id ||= SecureRandom.hex(32)
|
54
|
+
end
|
55
|
+
|
56
|
+
def job_id=(job_id)
|
57
|
+
@job_id = job_id
|
58
|
+
end
|
59
|
+
|
60
|
+
def job_key
|
61
|
+
self.class.job_unique_key.inject(Digest::SHA256.new.update(self.class.name)) do |digest, key|
|
31
62
|
digest.update(self[key].to_s)
|
32
63
|
end.hexdigest
|
33
64
|
end
|
34
65
|
|
35
|
-
def job_id=(job_id); end
|
36
|
-
|
37
66
|
def job_payload
|
38
|
-
JSON.generate({ class: self.class.name, args: each.to_a })
|
67
|
+
JSON.generate({ class: self.class.name, id: job_id, args: each.to_a })
|
68
|
+
end
|
69
|
+
|
70
|
+
def handle
|
71
|
+
self.class.job_handler.call(self)
|
39
72
|
end
|
40
73
|
end
|
41
74
|
end
|
42
75
|
|
43
|
-
def self.hydrate(
|
76
|
+
def self.hydrate(job_payload)
|
44
77
|
h = JSON.parse(job_payload, symbolize_names: true)
|
45
78
|
klass = const_get(h[:class])
|
46
79
|
job = klass.new(*h[:args])
|
47
|
-
job.job_id =
|
80
|
+
job.job_id = h[:id]
|
48
81
|
job
|
49
82
|
end
|
50
83
|
end
|
data/lib/bushpig/server.rb
CHANGED
@@ -48,7 +48,7 @@ module Bushpig
|
|
48
48
|
end
|
49
49
|
|
50
50
|
def handle(job)
|
51
|
-
puts "Job starting: jid-#{job.job_id} #{job}"
|
51
|
+
puts "Job starting: jid-#{job.job_id} jkey-#{job.job_key} #{job}"
|
52
52
|
started = monotonic_time
|
53
53
|
@handler.call(job)
|
54
54
|
finished = monotonic_time
|
@@ -70,7 +70,7 @@ module Bushpig
|
|
70
70
|
end
|
71
71
|
|
72
72
|
def notify_exception(job, exception)
|
73
|
-
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 })
|
74
74
|
end
|
75
75
|
|
76
76
|
def monotonic_time
|
@@ -80,7 +80,7 @@ module Bushpig
|
|
80
80
|
def fetch(queue)
|
81
81
|
redis_pool.with do |conn|
|
82
82
|
begin
|
83
|
-
res = conn.bzpopmin(Bushpig.
|
83
|
+
res = conn.bzpopmin(Bushpig.queue_key(queue), @timeout)
|
84
84
|
rescue Redis::TimeoutError
|
85
85
|
# TODO: warn user (once) that redis timeout set lower than pop timeout
|
86
86
|
conn.close
|
@@ -88,20 +88,20 @@ module Bushpig
|
|
88
88
|
end
|
89
89
|
return nil if res.nil?
|
90
90
|
|
91
|
-
(_set,
|
92
|
-
# conn.sadd('running',
|
91
|
+
(_set, key, _score) = res
|
92
|
+
# conn.sadd('running', key)
|
93
93
|
|
94
|
-
payload = conn.get(Bushpig.job_key(
|
94
|
+
payload = conn.get(Bushpig.job_key(key))
|
95
95
|
return nil if payload.nil? # most likely job expired
|
96
96
|
|
97
|
-
Bushpig::Job.hydrate(
|
97
|
+
Bushpig::Job.hydrate(payload)
|
98
98
|
end
|
99
99
|
end
|
100
100
|
|
101
101
|
def complete(job)
|
102
102
|
redis_pool.with do |conn|
|
103
|
-
# conn.srem('running', job.
|
104
|
-
conn.del(Bushpig.job_key(job.
|
103
|
+
# conn.srem('running', job.job_key)
|
104
|
+
conn.del(Bushpig.job_key(job.job_key))
|
105
105
|
end
|
106
106
|
end
|
107
107
|
end
|
data/lib/bushpig/version.rb
CHANGED