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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 07f2121ce3806939cd41665f3829d2b7d181e31ad371626bb91e99d4eb251dbe
4
- data.tar.gz: 9936ecfd5202819dc9f8cd4f2e73f1f49a0361784fe172ed55a1811a330eb9b3
3
+ metadata.gz: 1efcbe37d7ae873706796ef83eacb28e8eaa8146c4f2a11059047d8720a46178
4
+ data.tar.gz: eed134f3dd8c22f164ae7033ce0b9268ce4c5e7afba581985bc4cb0a71cc31f1
5
5
  SHA512:
6
- metadata.gz: 9f47000a9e2edb39989880da0c5c2b3b104f9ce165648c1c4494a8c12286e6aa3371a4c26fdb93d10f4c76dca2acd58536966ad882249f1c23f935e88e13376b
7
- data.tar.gz: daef3436de8188a5fc413154acf365733073d1f5a2cbe3c3fe373b6001a9b1e612670f714d7ca7220a22f28f24fff33fca0f34d5b8348750c4c23c324265d14b
6
+ metadata.gz: d61e72abda5d75728bea4584482c75266edc5ed6631c1a2e35f2b923f007372120e635223fa932ba0dd4a54b6faff013f0f4f90651969a50925d53d5e6b15582
7
+ data.tar.gz: ba09ef47797160869d6f4f967e2b0907f062fe327a3e6da9c705a9a37b4e2f38759cf139d29792a3f715307ab0bf5fceed5717cb332fab57a66f1b158cd1d16a
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- bushpig (0.1.5)
4
+ bushpig (0.1.10.3)
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.5";
189
+ version = "0.1.10.3";
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
@@ -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(job.job_id), job.job_payload, ex: ttl)
23
- 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)
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
- @@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) 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.5'
4
+ VERSION = '0.1.10.3'
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.5
4
+ version: 0.1.10.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shaun Sharples