bushpig 0.1.4 → 0.1.8.1

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: 4c75a2936af84505177c5cd6f439e8e0a482eaf2f186e984b40272131af09abc
4
- data.tar.gz: 05575e010e390a949969d649d9e66c46011c95e84ad9940cbe051c8fd1b2c19d
3
+ metadata.gz: 953cc975f6a214726b0991f0b856032d509815a8fdbaa5e99297f261508e96fd
4
+ data.tar.gz: 13abb819a5af786cd90e1f65962b81529c7df023395592a721399f11397891b8
5
5
  SHA512:
6
- metadata.gz: 23d1ed290ad2df8c4ba1f17099cc7c858bccce7052b64779009e938707c40f70050003e44b24cba20086c757f26bddd41f1141841c566f4f44ec2074df649b17
7
- data.tar.gz: 51b8ab6e67249f1a703103af8405f07e3871d42bb1a7c10c42326fdf73542c8fa05f3b027ec59f5328d11d3b31ce512eef09d54ce18e9ccb845a3e18f8ceb42f
6
+ metadata.gz: 0faa12236f02cdde323dbfca852613ea1b674e0928ad3e5f1f81267fc0611e04c2bcfaa4ecc4edbd0eb48d34ef141726a11c14bbc4dc52b775ae68dbc989b6f7
7
+ data.tar.gz: 71eb9f88710fd21e9a339706061e197c720af991077c92f2d0c77b80a984295ff8ad65a0d8456ba8e2563467443b01d5d038e61edb4f7f87d37736e7e048c3da
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- bushpig (0.1.4)
4
+ bushpig (0.1.8.1)
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.4";
189
+ version = "0.1.8";
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,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
- @@unique_key = unique_key
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
- @@unique_key.inject(Digest::SHA256.new) do |digest, key|
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(job_id, job_payload)
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 = job_id
68
+ job.job_id = h[:id]
48
69
  job
49
70
  end
50
71
  end
@@ -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.set_key(queue), @timeout)
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, jid, _score) = res
86
- # conn.sadd('running', jid)
91
+ (_set, key, _score) = res
92
+ # conn.sadd('running', key)
87
93
 
88
- payload = conn.get(Bushpig.job_key(jid))
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(jid, payload)
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.job_id)
98
- conn.del(Bushpig.job_key(job.job_id))
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Bushpig
4
- VERSION = '0.1.4'
4
+ VERSION = '0.1.8.1'
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.4
4
+ version: 0.1.8.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shaun Sharples