bushpig 0.1.5 → 0.1.10.3
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 +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: 1efcbe37d7ae873706796ef83eacb28e8eaa8146c4f2a11059047d8720a46178
|
4
|
+
data.tar.gz: eed134f3dd8c22f164ae7033ce0b9268ce4c5e7afba581985bc4cb0a71cc31f1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d61e72abda5d75728bea4584482c75266edc5ed6631c1a2e35f2b923f007372120e635223fa932ba0dd4a54b6faff013f0f4f90651969a50925d53d5e6b15582
|
7
|
+
data.tar.gz: ba09ef47797160869d6f4f967e2b0907f062fe327a3e6da9c705a9a37b4e2f38759cf139d29792a3f715307ab0bf5fceed5717cb332fab57a66f1b158cd1d16a
|
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,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) 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