bushpig 0.1.1 → 0.1.7

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: f268254d94c085b2d6f9ce94fdbe41a7f19f258dab8b4bdb8f693539ca2dae2a
4
- data.tar.gz: 9e6cd8def3f5098cb61ad97d4890a49d521b8bb0406d1c56502ca262622f0c21
3
+ metadata.gz: 8d3c78ffe7777e02a7e1a7d4f4e1838ab47c80a5c9d0c1d61e6547c2b956065f
4
+ data.tar.gz: 64bc3d1e149b54e84a28e9f637b9495b7a6e9d7eec451304e310afd8bb473067
5
5
  SHA512:
6
- metadata.gz: ad01e0e305262739d92ad507b6ddf69df77273d95a84a35e0d3fc92e6753413ce6c5a273d20cc870ff29beb14fdd4279f91da132ca785666c83ffb9661f1a638
7
- data.tar.gz: 9e8e78181b40dc8871e1367b8c7b18990256ab78da5e44c900bdf58d60b5609ed473bdb35c60e44a34879940ca2eeb1f69104b55d97e907a48a6faf4308e976e
6
+ metadata.gz: 9a1ca507019ef95f735f9b0558d1b4867fccec40a6fac62ced66f69d0ab57168a081af14dd661876221213c92e3c184393dabedefe8f706a1a12aa6354955bdd
7
+ data.tar.gz: 7148a4dabeff3df6575b455a08306342d9b488938593841882ed025e1f7a6fcbb35d22c5f29554a2269c78f3aa853808c84570d889aca8fadcac55ef5a362ec7
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- bushpig (0.1.1)
4
+ bushpig (0.1.7)
5
5
  connection_pool (~> 2.2)
6
6
  json (>= 1.8)
7
7
  redis (~> 3.3)
data/bin/bushpig CHANGED
@@ -1,4 +1,10 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
+ # frozen_string_literal: true
4
+
5
+ # don't buffer output (for logging if running as a systemd service)
6
+ $stdout.sync = true
7
+ $stderr.sync = true
8
+
3
9
  require 'bushpig/cli'
4
10
  Bushpig::CLI.start(ARGV)
data/gemset.nix CHANGED
@@ -186,7 +186,7 @@
186
186
  path = ./.;
187
187
  type = "path";
188
188
  };
189
- version = "0.1.1";
189
+ version = "0.1.7";
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
@@ -16,8 +16,12 @@ module Bushpig
16
16
  @job_id = job_id
17
17
  end
18
18
 
19
+ def job_key
20
+ job_id
21
+ end
22
+
19
23
  def job_payload
20
- JSON.generate({ class: self.class.name, args: each.to_a })
24
+ JSON.generate({ class: self.class.name, id: job_id, args: each.to_a })
21
25
  end
22
26
  end
23
27
  end
@@ -27,24 +31,30 @@ module Bushpig
27
31
  @@unique_key = unique_key
28
32
 
29
33
  def job_id
34
+ @job_id ||= SecureRandom.hex(32)
35
+ end
36
+
37
+ def job_id=(job_id)
38
+ @job_id = job_id
39
+ end
40
+
41
+ def job_key
30
42
  @@unique_key.inject(Digest::SHA256.new) do |digest, key|
31
43
  digest.update(self[key].to_s)
32
44
  end.hexdigest
33
45
  end
34
46
 
35
- def job_id=(job_id); end
36
-
37
47
  def job_payload
38
- JSON.generate({ class: self.class.name, args: each.to_a })
48
+ JSON.generate({ class: self.class.name, id: job_id, args: each.to_a })
39
49
  end
40
50
  end
41
51
  end
42
52
 
43
- def self.hydrate(job_id, job_payload)
53
+ def self.hydrate(job_payload)
44
54
  h = JSON.parse(job_payload, symbolize_names: true)
45
55
  klass = const_get(h[:class])
46
56
  job = klass.new(*h[:args])
47
- job.job_id = job_id
57
+ job.job_id = h[:id]
48
58
  job
49
59
  end
50
60
  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,18 +48,29 @@ 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
49
55
  elapsed = finished - started
50
56
  puts "Job completed: jid-#{job.job_id} #{elapsed} seconds"
51
57
  rescue StandardError => e
52
- # Job handler raised exception
53
58
  finished = monotonic_time
54
59
  elapsed = finished - started
55
60
  puts "Job raised exception: jid-#{job.job_id} #{elapsed} seconds: #{e}"
56
- # TODO: log exception to honeybadger
61
+ notify_exception(job, e)
62
+ end
63
+
64
+ def honeybadger
65
+ @honeybadger ||= begin
66
+ Honeybadger
67
+ rescue NameError
68
+ nil
69
+ end
70
+ end
71
+
72
+ def notify_exception(job, exception)
73
+ honeybadger&.notify(exception, context: { job_id: job.job_id, job_key: job.job_key, job: job.to_s })
57
74
  end
58
75
 
59
76
  def monotonic_time
@@ -63,7 +80,7 @@ module Bushpig
63
80
  def fetch(queue)
64
81
  redis_pool.with do |conn|
65
82
  begin
66
- res = conn.bzpopmin(Bushpig.set_key(queue), @timeout)
83
+ res = conn.bzpopmin(Bushpig.queue_key(queue), @timeout)
67
84
  rescue Redis::TimeoutError
68
85
  # TODO: warn user (once) that redis timeout set lower than pop timeout
69
86
  conn.close
@@ -71,20 +88,20 @@ module Bushpig
71
88
  end
72
89
  return nil if res.nil?
73
90
 
74
- (_set, jid, _score) = res
75
- # conn.sadd('running', jid)
91
+ (_set, key, _score) = res
92
+ # conn.sadd('running', key)
76
93
 
77
- payload = conn.get(Bushpig.job_key(jid))
94
+ payload = conn.get(Bushpig.job_key(key))
78
95
  return nil if payload.nil? # most likely job expired
79
96
 
80
- Bushpig::Job.hydrate(jid, payload)
97
+ Bushpig::Job.hydrate(payload)
81
98
  end
82
99
  end
83
100
 
84
101
  def complete(job)
85
102
  redis_pool.with do |conn|
86
- # conn.srem('running', job.job_id)
87
- conn.del(Bushpig.job_key(job.job_id))
103
+ # conn.srem('running', job.job_key)
104
+ conn.del(Bushpig.job_key(job.job_key))
88
105
  end
89
106
  end
90
107
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Bushpig
4
- VERSION = '0.1.1'
4
+ VERSION = '0.1.7'
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.1
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shaun Sharples