unique_thread 0.3.0 → 0.4.0

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
  SHA1:
3
- metadata.gz: 84cdf521ad296d825e220ccea5b2610e70bef0dc
4
- data.tar.gz: 07fe1de3059244ae0681c152fd249fdf75568d59
3
+ metadata.gz: 9c30eb2c2e3046dd457f030e2aff44def4aefdf3
4
+ data.tar.gz: 22ad34869f19e0aaf27d1bad6bc8a72299fc6ee6
5
5
  SHA512:
6
- metadata.gz: 34a8c80ee3e585946fc6c4d662bf0ce1f525a4506880e5f585f081cc47cf68715d14c860060d5c86f7852d154fde9ac7cbbea1fcd4767bf047c0d1e2c3271f58
7
- data.tar.gz: 2f7be9c25bfa9f6fded8c7d1fb9507f06790d43b6f784c8bfe41f563c63b8dd0f9a7e313063c0d18a9133fa91eefdc551769dbc8a6cf2cb69702956a190e30c9
6
+ metadata.gz: 9be00638892f61190a29bd1f42d85fb54bd5ba99d6a1590eef2298b68ba29537b5ee1f3546652c0ac5c363d205be3581c39e61f5ddf1ef19e3e203a55b292bf9
7
+ data.tar.gz: 20960248f2f34bb8d3b995dea6cad608b717013a5cfefe298146d3b671a7e9b41ea1a71ae9787c9cf48ef5a403be779abf247158283ec514a16903dbc7311267
data/lib/unique_thread.rb CHANGED
@@ -7,7 +7,7 @@ require_relative 'unique_thread/locksmith'
7
7
 
8
8
  class UniqueThread
9
9
  class << self
10
- attr_writer :logger, :redis
10
+ attr_writer :logger, :redis, :error_handlers
11
11
 
12
12
  def logger
13
13
  @logger ||= default_logger
@@ -17,6 +17,20 @@ class UniqueThread
17
17
  @redis ||= Redis.new
18
18
  end
19
19
 
20
+ def error_handlers
21
+ @error_handlers ||= []
22
+ end
23
+
24
+ def safe_thread
25
+ Thread.new do
26
+ begin
27
+ yield
28
+ rescue StandardError => error
29
+ report_error(error)
30
+ end
31
+ end
32
+ end
33
+
20
34
  private
21
35
 
22
36
  def default_logger
@@ -29,6 +43,11 @@ class UniqueThread
29
43
  Logger.new($stdout)
30
44
  end
31
45
  end
46
+
47
+ def report_error(exception)
48
+ logger.error(exception.inspect)
49
+ error_handlers.each { |handler| handler.call(exception) }
50
+ end
32
51
  end
33
52
 
34
53
  attr_reader :stopwatch, :locksmith
@@ -39,28 +58,22 @@ class UniqueThread
39
58
  end
40
59
 
41
60
  def run(&block)
42
- safe_infinite_loop do
43
- lock = locksmith.new_lock
44
-
45
- if lock.acquired?
46
- self.class.logger.info('Lock acquired! Running the unique thread.')
47
- lock.while_held(&block)
48
- else
49
- self.class.logger.debug('Could not acquire the lock. Sleeping until next attempt.')
50
- stopwatch.sleep_until_next_attempt(lock.locked_until.to_f)
51
- end
61
+ self.class.safe_thread do
62
+ loop { try_being_the_unique_thread(&block) }
52
63
  end
53
64
  end
54
65
 
55
66
  private
56
67
 
57
- def safe_infinite_loop
58
- Thread.new do
59
- begin
60
- loop { yield }
61
- rescue StandardError => error
62
- self.class.logger.error(error)
63
- end
68
+ def try_being_the_unique_thread(&block)
69
+ lock = locksmith.new_lock
70
+
71
+ if lock.acquired?
72
+ self.class.logger.info('Lock acquired! Running the unique thread.')
73
+ lock.while_held(&block)
74
+ else
75
+ self.class.logger.debug('Could not acquire the lock. Sleeping until next attempt.')
76
+ stopwatch.sleep_until_next_attempt(lock.locked_until.to_f)
64
77
  end
65
78
  end
66
79
  end
@@ -70,7 +70,7 @@ class UniqueThread
70
70
  end
71
71
 
72
72
  def while_held
73
- worker = Thread.new do
73
+ worker = UniqueThread.safe_thread do
74
74
  yield
75
75
  UniqueThread.logger.error('The blocked passed is not an infinite loop.')
76
76
  end
@@ -12,7 +12,6 @@ class UniqueThread
12
12
  Time.now.to_f
13
13
  end
14
14
 
15
- # FIXME: Bad name. Lap maybe? Milestone?
16
15
  def next_renewal
17
16
  now + (downtime * 2 / 3)
18
17
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: unique_thread
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fernando Seror