bushpig 0.1.4 → 0.1.9

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: 4c75a2936af84505177c5cd6f439e8e0a482eaf2f186e984b40272131af09abc
4
- data.tar.gz: 05575e010e390a949969d649d9e66c46011c95e84ad9940cbe051c8fd1b2c19d
3
+ metadata.gz: 75f414bd5b386fdcd5d452374ab9d6f1076e59353e6a08416cd710f8119f0b9b
4
+ data.tar.gz: 91cca09ccea7aa3b9169d15fe8174d16312892c8db7668408f0c594afcdcaef2
5
5
  SHA512:
6
- metadata.gz: 23d1ed290ad2df8c4ba1f17099cc7c858bccce7052b64779009e938707c40f70050003e44b24cba20086c757f26bddd41f1141841c566f4f44ec2074df649b17
7
- data.tar.gz: 51b8ab6e67249f1a703103af8405f07e3871d42bb1a7c10c42326fdf73542c8fa05f3b027ec59f5328d11d3b31ce512eef09d54ce18e9ccb845a3e18f8ceb42f
6
+ metadata.gz: af10ea043bc302564ac0430b3531624c3d65b9c15cfc0e81e48f3cce9df314219b63c3bc1354de308420a6dd61d151858a9dcea1b9d999cdd23c201e9c12bcf3
7
+ data.tar.gz: e45960eddf67527a28203e73c7805b14cdfd7fc0399fac5b18d99833ee1d9d054794d437fb30c69c1c6b405a910127d2118d1d8bc9a92839c9a28404ad9e4e2e
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.9)
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.9";
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,58 @@ 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 call(job)
34
+ self.class.job_handler.call(job)
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
28
47
 
29
48
  def job_id
30
- @@unique_key.inject(Digest::SHA256.new) do |digest, key|
49
+ @job_id ||= SecureRandom.hex(32)
50
+ end
51
+
52
+ def job_id=(job_id)
53
+ @job_id = job_id
54
+ end
55
+
56
+ def job_key
57
+ @unique_key.inject(Digest::SHA256.new) do |digest, key|
31
58
  digest.update(self[key].to_s)
32
59
  end.hexdigest
33
60
  end
34
61
 
35
- def job_id=(job_id); end
36
-
37
62
  def job_payload
38
- JSON.generate({ class: self.class.name, args: each.to_a })
63
+ JSON.generate({ class: self.class.name, id: job_id, args: each.to_a })
64
+ end
65
+
66
+ def call(job)
67
+ self.class.job_handler.call(job)
39
68
  end
40
69
  end
41
70
  end
42
71
 
43
- def self.hydrate(job_id, job_payload)
72
+ def self.hydrate(job_payload)
44
73
  h = JSON.parse(job_payload, symbolize_names: true)
45
74
  klass = const_get(h[:class])
46
75
  job = klass.new(*h[:args])
47
- job.job_id = job_id
76
+ job.job_id = h[:id]
48
77
  job
49
78
  end
50
79
  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.9'
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.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shaun Sharples