lifeguard 0.0.8 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +8 -0
- data/README.md +4 -0
- data/lib/lifeguard/infinite_threadpool.rb +13 -4
- data/lib/lifeguard/reaper.rb +3 -18
- data/lib/lifeguard/threadpool.rb +16 -2
- data/lib/lifeguard/version.rb +1 -1
- data/lifeguard.gemspec +1 -1
- data/spec/lifeguard/reaper_spec.rb +1 -1
- data/spec/lifeguard/threadpool_spec.rb +32 -7
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8c0e136e3ea9cf546ef269e33ebd6b5e1f84fa7f
|
4
|
+
data.tar.gz: af7f496ae700677de545303d763bca5eb285d648
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b0d4d7ef4c2f6fa8a18e8dba4ce01ea25f1ae72c838c30833e6316120ee8582fdacafde991d87f02e96dfac7708ae8f68858fe6684572bda33704993702bdfb6
|
7
|
+
data.tar.gz: 333d49ff95dd267e49e0e38a3f151729c21b2fee459f5bbb6616c2b6aa8abe24428c870c5bcb9ce871efc5191ed8dbae79358a2347fbb42fe338b9afef430b10
|
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
[![Build Status](https://travis-ci.org/moneydesktop/lifeguard.svg?branch=master)](https://travis-ci.org/moneydesktop/lifeguard)
|
2
|
+
|
3
|
+
[![Code Climate](https://codeclimate.com/github/moneydesktop/lifeguard/badges/gpa.svg)](https://codeclimate.com/github/moneydesktop/lifeguard)
|
4
|
+
|
1
5
|
lifeguard
|
2
6
|
=========
|
3
7
|
|
@@ -13,6 +13,7 @@ module Lifeguard
|
|
13
13
|
def async(*args, &block)
|
14
14
|
return false if @shutdown
|
15
15
|
|
16
|
+
check_queued_jobs
|
16
17
|
job_started = super
|
17
18
|
|
18
19
|
unless job_started
|
@@ -23,10 +24,12 @@ module Lifeguard
|
|
23
24
|
end
|
24
25
|
|
25
26
|
def check_queued_jobs
|
26
|
-
if
|
27
|
-
|
28
|
-
|
29
|
-
|
27
|
+
return if busy?
|
28
|
+
return if @queued_jobs.size <= 0
|
29
|
+
|
30
|
+
queued_job = @queued_jobs.pop
|
31
|
+
async(*queued_job[:args], &queued_job[:block])
|
32
|
+
check_queued_jobs
|
30
33
|
end
|
31
34
|
|
32
35
|
def kill!(*args)
|
@@ -40,6 +43,12 @@ module Lifeguard
|
|
40
43
|
return_value
|
41
44
|
end
|
42
45
|
|
46
|
+
def prune_busy_threads
|
47
|
+
response = super
|
48
|
+
check_queued_jobs
|
49
|
+
response
|
50
|
+
end
|
51
|
+
|
43
52
|
def shutdown(*args)
|
44
53
|
@shutdown = true
|
45
54
|
super
|
data/lib/lifeguard/reaper.rb
CHANGED
@@ -7,6 +7,7 @@ module Lifeguard
|
|
7
7
|
@threadpool = threadpool
|
8
8
|
@reaping_interval = reaping_interval
|
9
9
|
@thread = ::Thread.new { self.run! }
|
10
|
+
::Thread.pass until alive?
|
10
11
|
end
|
11
12
|
|
12
13
|
##
|
@@ -16,30 +17,14 @@ module Lifeguard
|
|
16
17
|
@thread.alive?
|
17
18
|
end
|
18
19
|
|
19
|
-
def reap!
|
20
|
-
@threadpool.prune_busy_threads
|
21
|
-
end
|
22
|
-
|
23
20
|
def run!
|
24
21
|
loop do
|
25
22
|
sleep(@reaping_interval)
|
26
|
-
|
27
|
-
timeout!
|
28
|
-
ready_thread_count = @threadpool.pool_size - @threadpool.busy_size
|
29
|
-
|
30
|
-
if ready_thread_count > 0 && @threadpool.respond_to?(:check_queued_jobs)
|
31
|
-
ready_thread_count.times do
|
32
|
-
@threadpool.check_queued_jobs
|
33
|
-
end
|
34
|
-
end
|
23
|
+
@threadpool.prune_busy_threads if @threadpool
|
24
|
+
@threadpool.timeout! if @threadpool
|
35
25
|
end
|
36
26
|
rescue
|
37
27
|
retry
|
38
28
|
end
|
39
|
-
|
40
|
-
def timeout!
|
41
|
-
@threadpool.timeout!
|
42
|
-
end
|
43
|
-
|
44
29
|
end
|
45
30
|
end
|
data/lib/lifeguard/threadpool.rb
CHANGED
@@ -1,16 +1,19 @@
|
|
1
1
|
require 'thread'
|
2
|
+
require 'securerandom'
|
2
3
|
|
3
4
|
module Lifeguard
|
4
5
|
class Threadpool
|
5
6
|
DEFAULT_REAPING_INTERVAL = 5 # in seconds
|
6
7
|
DEFAULT_POOL_SIZE = 2
|
7
8
|
|
8
|
-
attr_accessor :pool_size
|
9
|
+
attr_accessor :name, :options, :pool_size
|
9
10
|
|
10
11
|
##
|
11
12
|
# Constructor
|
12
13
|
#
|
13
14
|
def initialize(opts = {})
|
15
|
+
@options = opts
|
16
|
+
@name = opts[:name] || ::SecureRandom.uuid
|
14
17
|
@pool_size = opts[:pool_size] || DEFAULT_POOL_SIZE
|
15
18
|
|
16
19
|
# Important info about "timeout", it is controlled by the reaper
|
@@ -23,12 +26,16 @@ module Lifeguard
|
|
23
26
|
@mutex = ::Mutex.new
|
24
27
|
@busy_threads = []
|
25
28
|
|
26
|
-
|
29
|
+
restart_reaper_unless_alive
|
27
30
|
end
|
28
31
|
|
29
32
|
##
|
30
33
|
# Public Instance Methods
|
31
34
|
#
|
35
|
+
def busy?
|
36
|
+
busy_size >= pool_size
|
37
|
+
end
|
38
|
+
|
32
39
|
def busy_size
|
33
40
|
@busy_threads.size
|
34
41
|
end
|
@@ -49,6 +56,7 @@ module Lifeguard
|
|
49
56
|
|
50
57
|
def async(*args, &block)
|
51
58
|
queued_the_work = false
|
59
|
+
restart_reaper_unless_alive
|
52
60
|
|
53
61
|
unless block
|
54
62
|
raise "Threadpool#async must be passed a block"
|
@@ -129,5 +137,11 @@ module Lifeguard
|
|
129
137
|
@busy_threads.select!(&:alive?)
|
130
138
|
end
|
131
139
|
|
140
|
+
def restart_reaper_unless_alive
|
141
|
+
return if @reaper && @reaper.alive?
|
142
|
+
|
143
|
+
@reaper = ::Lifeguard::Reaper.new(self, options[:reaping_interval] || DEFAULT_REAPING_INTERVAL)
|
144
|
+
end
|
145
|
+
|
132
146
|
end
|
133
147
|
end
|
data/lib/lifeguard/version.rb
CHANGED
data/lifeguard.gemspec
CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
|
|
10
10
|
spec.email = ["brianastien@gmail.com", "brandonsdewitt@gmail.com"]
|
11
11
|
spec.summary = %q{A Supervised threadpool implementation in ruby.}
|
12
12
|
spec.description = %q{Do you have a threadpool? Do you need someone to watch it? Look no further!}
|
13
|
-
spec.homepage = ""
|
13
|
+
spec.homepage = "https://github.com/moneydesktop/lifeguard"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
16
16
|
spec.files = `git ls-files`.split($/)
|
@@ -1,7 +1,6 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe ::Lifeguard::Threadpool do
|
4
|
-
|
5
4
|
subject { described_class.new(:pool_size => 5) }
|
6
5
|
|
7
6
|
after(:each) do
|
@@ -9,15 +8,40 @@ describe ::Lifeguard::Threadpool do
|
|
9
8
|
sleep(0.1)
|
10
9
|
end
|
11
10
|
|
11
|
+
describe "#busy?" do
|
12
|
+
it "reports false when the busy_size < pool_size" do
|
13
|
+
threadpool = described_class.new(:pool_size => 1, :reaping_interval => 1)
|
14
|
+
expect(threadpool.busy?).to be false
|
15
|
+
end
|
16
|
+
|
17
|
+
it "reports true when the busy_size >= pool_size" do
|
18
|
+
threadpool = described_class.new(:pool_size => 1, :reaping_interval => 1)
|
19
|
+
threadpool.async do
|
20
|
+
sleep(1)
|
21
|
+
end
|
22
|
+
|
23
|
+
expect(threadpool.busy?).to be true
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe "#name" do
|
28
|
+
let(:name) { "AWESOME_NAME" }
|
29
|
+
|
30
|
+
it "allows a name to be set via an option" do
|
31
|
+
threadpool = described_class.new(:name => name, :pool_size => 1, :reaping_interval => 1)
|
32
|
+
expect(threadpool.name).to eq(name)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
12
36
|
describe "#timeout!" do
|
13
37
|
it "doesn't timeout when no timeout set" do
|
14
38
|
threadpool = described_class.new()
|
15
|
-
threadpool.timeout?.should
|
39
|
+
threadpool.timeout?.should be false
|
16
40
|
end
|
17
41
|
|
18
42
|
it "does timeout when timeout set" do
|
19
43
|
threadpool = described_class.new(:timeout => 30)
|
20
|
-
threadpool.timeout?.should
|
44
|
+
threadpool.timeout?.should be true
|
21
45
|
end
|
22
46
|
|
23
47
|
it "uses the reaper to timeout threads that are all wiley" do
|
@@ -39,7 +63,7 @@ describe ::Lifeguard::Threadpool do
|
|
39
63
|
sleep(0.1)
|
40
64
|
subject.kill!
|
41
65
|
sleep(1)
|
42
|
-
@expected.should
|
66
|
+
@expected.should be false
|
43
67
|
end
|
44
68
|
|
45
69
|
it 'rejects all pending tasks' do
|
@@ -49,17 +73,18 @@ describe ::Lifeguard::Threadpool do
|
|
49
73
|
sleep(0.1)
|
50
74
|
subject.kill!
|
51
75
|
sleep(1)
|
52
|
-
@expected.should
|
76
|
+
@expected.should be false
|
53
77
|
end
|
54
78
|
|
55
79
|
it 'kills all threads' do
|
80
|
+
subject
|
56
81
|
before_thread_count = Thread.list.size
|
57
82
|
100.times { subject.async{ sleep(1) } }
|
58
83
|
sleep(0.1)
|
59
84
|
Thread.list.size.should > before_thread_count
|
60
85
|
subject.kill!
|
61
86
|
sleep(0.1)
|
62
|
-
Thread.list.size.should eq(before_thread_count
|
87
|
+
Thread.list.size.should eq(before_thread_count)
|
63
88
|
end
|
64
89
|
end
|
65
90
|
|
@@ -69,7 +94,7 @@ describe ::Lifeguard::Threadpool do
|
|
69
94
|
end
|
70
95
|
|
71
96
|
it 'returns true when the block is added to the queue' do
|
72
|
-
subject.async{ sleep }.should
|
97
|
+
subject.async{ sleep }.should be true
|
73
98
|
end
|
74
99
|
|
75
100
|
it 'calls the block with the given arguments' do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lifeguard
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brian Stien
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2016-02-21 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: better_receive
|
@@ -119,6 +119,7 @@ extra_rdoc_files: []
|
|
119
119
|
files:
|
120
120
|
- ".gitignore"
|
121
121
|
- ".rspec"
|
122
|
+
- ".travis.yml"
|
122
123
|
- Gemfile
|
123
124
|
- LICENSE.txt
|
124
125
|
- README.md
|
@@ -133,7 +134,7 @@ files:
|
|
133
134
|
- spec/lifeguard/threadpool_spec.rb
|
134
135
|
- spec/spec_helper.rb
|
135
136
|
- spec/support/.gitkeep
|
136
|
-
homepage:
|
137
|
+
homepage: https://github.com/moneydesktop/lifeguard
|
137
138
|
licenses:
|
138
139
|
- MIT
|
139
140
|
metadata: {}
|
@@ -153,7 +154,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
153
154
|
version: '0'
|
154
155
|
requirements: []
|
155
156
|
rubyforge_project:
|
156
|
-
rubygems_version: 2.
|
157
|
+
rubygems_version: 2.2.2
|
157
158
|
signing_key:
|
158
159
|
specification_version: 4
|
159
160
|
summary: A Supervised threadpool implementation in ruby.
|