silent_worker 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +1 -1
- data/README.md +4 -4
- data/lib/silent_worker.rb +34 -10
- data/lib/silent_worker/version.rb +1 -1
- data/spec/silent_worker_spec.rb +29 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 73bc5d82b300cd804ce3001b57f386755d868ca7
|
4
|
+
data.tar.gz: 2cc6e8bbf5c0cd9e7415a5a9bbff7386ead2a754
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 41a03163ed2afd3e83c7906ffa57dd631c0486e2b548b86c21691e404d7afcb840bc4ab318ceafff59fc62e013de54531b8ac50e39e8a9f2a4e5e5320ba87d96
|
7
|
+
data.tar.gz: f9d1b47f994058f7d6a0c3518ec59c992c3b8781dd76c7d5ff563adc7c7c4626b39c52992d475a1cb1b8c203a518fd59ab367117070c11f83012ff8a99d28c74
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -57,12 +57,12 @@ sw = SilentWorker.new(parallel) do |url|
|
|
57
57
|
system("wget", url)
|
58
58
|
end
|
59
59
|
|
60
|
-
File.open('url_list.txt') do |
|
61
|
-
sw <<
|
60
|
+
File.open('url_list.txt').each_line do |line|
|
61
|
+
sw << line.strip
|
62
62
|
end
|
63
63
|
|
64
|
-
File.open('url_list2.txt') do |
|
65
|
-
sw <<
|
64
|
+
File.open('url_list2.txt').each_line do |line|
|
65
|
+
sw << line.strip
|
66
66
|
end
|
67
67
|
|
68
68
|
sw.wait
|
data/lib/silent_worker.rb
CHANGED
@@ -11,35 +11,39 @@ class SilentWorker
|
|
11
11
|
@workers = workers
|
12
12
|
@threads = []
|
13
13
|
@queue = Queue.new
|
14
|
+
setup_signal_traps
|
14
15
|
start
|
15
16
|
end
|
16
17
|
|
17
18
|
def <<(data)
|
18
|
-
@queue.
|
19
|
+
@queue.enq(data)
|
19
20
|
end
|
20
21
|
|
21
22
|
def wait
|
22
23
|
finish!
|
23
|
-
@
|
24
|
-
@queue.push(FINISH_DATA)
|
25
|
-
end
|
26
|
-
@threads.each(&:join)
|
24
|
+
@threads.find_all(&:alive?).each(&:join)
|
27
25
|
end
|
28
26
|
|
29
27
|
def abort
|
30
28
|
@threads.each(&:kill)
|
31
29
|
wait
|
32
30
|
end
|
31
|
+
alias :stop! :abort
|
33
32
|
|
34
33
|
def stop
|
35
|
-
|
34
|
+
finish!
|
36
35
|
end
|
37
36
|
|
38
37
|
def start
|
39
|
-
@
|
40
|
-
|
38
|
+
return if @working
|
39
|
+
|
40
|
+
@working = true
|
41
|
+
@finished = false
|
42
|
+
@workers.times do |n|
|
43
|
+
@threads << Thread.start(@job, @queue, n) do |job, queue, n|
|
44
|
+
Thread.current[:num] = n
|
41
45
|
loop do
|
42
|
-
data = queue.
|
46
|
+
data = queue.deq
|
43
47
|
break if @finished && data == FINISH_DATA
|
44
48
|
job.call(data)
|
45
49
|
end
|
@@ -50,7 +54,27 @@ class SilentWorker
|
|
50
54
|
private
|
51
55
|
|
52
56
|
def finish!
|
53
|
-
@
|
57
|
+
@working = false
|
58
|
+
|
59
|
+
unless @finished
|
60
|
+
@finished = true
|
61
|
+
@workers.times do
|
62
|
+
@queue.enq(FINISH_DATA)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def setup_signal_traps
|
68
|
+
Signal.trap("INT") { abort }
|
69
|
+
Signal.trap("TERM") { abort }
|
70
|
+
Signal.trap("QUIT") { abort }
|
71
|
+
|
72
|
+
# Maybe forgotten to call #wait
|
73
|
+
Signal.trap("EXIT") do
|
74
|
+
return if @finished
|
75
|
+
warn "\nWARNING: You should call SilentWorker#wait to wait jobs are completed. Now abort them.\n"
|
76
|
+
abort
|
77
|
+
end
|
54
78
|
end
|
55
79
|
|
56
80
|
end
|
data/spec/silent_worker_spec.rb
CHANGED
@@ -43,12 +43,13 @@ describe SilentWorker do
|
|
43
43
|
describe "#stop and #start" do
|
44
44
|
let(:sw) { SilentWorker.new {|data| data } }
|
45
45
|
|
46
|
-
after { sw.abort }
|
46
|
+
after { sw.abort; sw.queue.clear }
|
47
47
|
|
48
48
|
it "jobs won't be fired when stopping workers" do
|
49
49
|
sw << 1
|
50
50
|
sw.stop
|
51
51
|
sw << 2
|
52
|
+
sw.wait
|
52
53
|
sw.queue.pop.should == 2
|
53
54
|
end
|
54
55
|
|
@@ -57,10 +58,37 @@ describe SilentWorker do
|
|
57
58
|
sw << 1
|
58
59
|
sw.stop
|
59
60
|
sw << 2
|
61
|
+
sw.wait
|
60
62
|
sw.queue.length.should == 1
|
63
|
+
|
61
64
|
sw.start
|
62
65
|
sw.wait
|
63
66
|
sw.queue.length.should == 0
|
64
67
|
end
|
65
68
|
end
|
69
|
+
|
70
|
+
describe "Signal trapping" do
|
71
|
+
context "should abort" do
|
72
|
+
before do
|
73
|
+
sw = SilentWorker.new { sleep 10 }
|
74
|
+
sw.should_receive(:abort)
|
75
|
+
sleep 0.1 if ENV["TRAVIS_CI"]
|
76
|
+
end
|
77
|
+
|
78
|
+
it "INT" do
|
79
|
+
Process.kill("INT", $$)
|
80
|
+
end
|
81
|
+
|
82
|
+
it "TERM" do
|
83
|
+
Process.kill("TERM", $$)
|
84
|
+
end
|
85
|
+
|
86
|
+
it "QUIT" do
|
87
|
+
Process.kill("QUIT", $$)
|
88
|
+
end
|
89
|
+
|
90
|
+
it "EXIT", :pending => "How to test this?" do
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
66
94
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: silent_worker
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- uu59
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-07-
|
11
|
+
date: 2013-07-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|