bushpig 0.1.6 → 0.1.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fc9bf6fabf6038b858a576750b32c827ad7095bca278de9bf1ae207258fd0c94
4
- data.tar.gz: 36be95562838f7868ccc13d76a5e4b77bc75220f18f19693e2fee001d710e21b
3
+ metadata.gz: 6e851a30d506853e4b90dc634602965ea520d0499a72edf92c5ee0cf3ea39f10
4
+ data.tar.gz: ef79c0b258b38f10739dd3b6c5496afd2547401bf65586f381b700e582a5d651
5
5
  SHA512:
6
- metadata.gz: cd5f62fdff7ace18ffac4ba7332d29f68a53debeda49f155fe784fe3ff14f2e70ef07cf26623087d134ae7c8e361cd8fe2536afb9f135d05716f850680ae255b
7
- data.tar.gz: f90b3ba40eb138d0e9a589d9d47f727218761f0a4cc8e022da0fce3395894cd50415f15349cf421a3995a56ecc08582bbe6a9b6b47ea79b1288bbce41e4c5fbe
6
+ metadata.gz: 928d495049bd4d9ab65d6255f4f8743bae42de26eb66f7d5f3ba84992536c90b27daca7693b90f6de85d58058ed8739d0a1db2fcd814f18c504049dc0cb5fa34
7
+ data.tar.gz: 5708e74eff120f8b9e8488d72debe2a573ff53f1ed2b99408d80faf40d1e2e0511bce5f197e468f0efb9e84c0c85f2906f439f726b118a6ad9fa489a2d70e78b
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- bushpig (0.1.6)
4
+ bushpig (0.1.11)
5
5
  connection_pool (~> 2.2)
6
6
  json (>= 1.8)
7
7
  redis (~> 3.3)
data/gemset.nix CHANGED
@@ -186,7 +186,7 @@
186
186
  path = ./.;
187
187
  type = "path";
188
188
  };
189
- version = "0.1.6";
189
+ version = "0.1.11";
190
190
  };
191
191
  coderay = {
192
192
  groups = ["default" "development"];
data/lib/bushpig.rb CHANGED
@@ -43,11 +43,11 @@ module Bushpig
43
43
  defined?(Bushpig::CLI)
44
44
  end
45
45
 
46
- def self.set_key(queue)
46
+ def self.queue_key(queue)
47
47
  "set:#{queue}"
48
48
  end
49
49
 
50
- def self.job_key(job_id)
51
- "job:#{job_id}"
50
+ def self.job_key(job_key)
51
+ "job:#{job_key}"
52
52
  end
53
53
  end
@@ -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(job.job_id), job.job_payload, ex: ttl)
25
- conn.zadd(Bushpig.set_key(queue), score, job.job_id)
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
- @@unique_key = unique_key
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
- @@unique_key.inject(Digest::SHA256.new) do |digest, key|
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(job_id, job_payload)
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 = job_id
80
+ job.job_id = h[:id]
48
81
  job
49
82
  end
50
83
  end
@@ -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.set_key(queue), @timeout)
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, jid, _score) = res
92
- # conn.sadd('running', jid)
91
+ (_set, key, _score) = res
92
+ # conn.sadd('running', key)
93
93
 
94
- payload = conn.get(Bushpig.job_key(jid))
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(jid, payload)
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.job_id)
104
- conn.del(Bushpig.job_key(job.job_id))
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Bushpig
4
- VERSION = '0.1.6'
4
+ VERSION = '0.1.11'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bushpig
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shaun Sharples