unique_thread 0.3.0 → 0.4.0
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 +4 -4
- data/lib/unique_thread.rb +31 -18
- data/lib/unique_thread/locksmith.rb +1 -1
- data/lib/unique_thread/stopwatch.rb +0 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9c30eb2c2e3046dd457f030e2aff44def4aefdf3
|
4
|
+
data.tar.gz: 22ad34869f19e0aaf27d1bad6bc8a72299fc6ee6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
43
|
-
|
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
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
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
|