chore-core 1.8.2 → 1.8.3
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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8a1e15581a9bd8310bd8d1f4ae4392f35df44afd
|
4
|
+
data.tar.gz: 72776fab3cc9fc4013c9c0a80f55dddb2bef7277
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4b3f27a13ae37b90e18b693bffe1d09f36ab9cdf33ca1ce0a0d9d69fb9c9a1d60836f54ed963549ec6103c59f02e8a95e824b40397489c8ec44c042000e856be
|
7
|
+
data.tar.gz: 4beb0d44d569c8eaea6306cd5bab3dd0f89f2ddd59892aa6b3ab5dc9c4f17107eb6fbaa6fab45530ba2c7e87b7eede5737f41d9f2727df6238de9a10998166f7
|
@@ -10,7 +10,10 @@ module Chore
|
|
10
10
|
def initialize(manager, opts={})
|
11
11
|
@options = opts
|
12
12
|
@manager = manager
|
13
|
+
@stopped = false
|
13
14
|
@worker = nil
|
15
|
+
@queue = Queue.new
|
16
|
+
@queue << :worker
|
14
17
|
end
|
15
18
|
|
16
19
|
# Starts the <tt>SingleWorkerStrategy</tt>. Currently a noop
|
@@ -18,6 +21,11 @@ module Chore
|
|
18
21
|
|
19
22
|
# Stops the <tt>SingleWorkerStrategy</tt> if there is a worker to stop
|
20
23
|
def stop!
|
24
|
+
return if @stopped
|
25
|
+
|
26
|
+
@stopped = true
|
27
|
+
Chore.logger.info { "Manager #{Process.pid} stopping" }
|
28
|
+
|
21
29
|
worker.stop! if worker
|
22
30
|
end
|
23
31
|
|
@@ -25,16 +33,14 @@ module Chore
|
|
25
33
|
# single worker strategy, this should never be called if the worker is in
|
26
34
|
# progress.
|
27
35
|
def assign(work)
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
else
|
37
|
-
Chore.logger.error { "#{self.class}#assign: single worker is unavailable, but assign has been re-entered: #{caller * "\n"}" }
|
36
|
+
return unless acquire_worker
|
37
|
+
|
38
|
+
begin
|
39
|
+
@worker = worker_klass.new(work, @options)
|
40
|
+
@worker.start
|
41
|
+
true
|
42
|
+
ensure
|
43
|
+
release_worker
|
38
44
|
end
|
39
45
|
end
|
40
46
|
|
@@ -42,9 +48,25 @@ module Chore
|
|
42
48
|
Worker
|
43
49
|
end
|
44
50
|
|
45
|
-
|
46
|
-
|
47
|
-
|
51
|
+
private
|
52
|
+
|
53
|
+
# Attempts to essentially acquire a lock on a worker. If no workers are
|
54
|
+
# available, then this will block until one is.
|
55
|
+
def acquire_worker
|
56
|
+
result = @queue.pop
|
57
|
+
|
58
|
+
if @stopped
|
59
|
+
# Strategy has stopped since the worker was acquired
|
60
|
+
release_worker
|
61
|
+
nil
|
62
|
+
else
|
63
|
+
result
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
# Releases the lock on a worker so that another thread can pick it up.
|
68
|
+
def release_worker
|
69
|
+
@queue << :worker
|
48
70
|
end
|
49
71
|
end
|
50
72
|
end
|
data/lib/chore/version.rb
CHANGED
@@ -2,6 +2,8 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Chore::Strategy::SingleWorkerStrategy do
|
4
4
|
let(:manager) { double('Manager') }
|
5
|
+
let(:job_timeout) { 60 }
|
6
|
+
let(:job) { Chore::UnitOfWork.new(SecureRandom.uuid, 'test', job_timeout, Chore::Encoder::JsonEncoder.encode(TestJob.job_hash([1,2,"3"])), 0) }
|
5
7
|
subject { described_class.new(manager) }
|
6
8
|
|
7
9
|
describe '#stop!' do
|
@@ -31,4 +33,27 @@ describe Chore::Strategy::SingleWorkerStrategy do
|
|
31
33
|
end
|
32
34
|
end
|
33
35
|
|
36
|
+
describe '#assign' do
|
37
|
+
let(:worker) { double('Worker', start: nil) }
|
38
|
+
|
39
|
+
it 'starts a new worker' do
|
40
|
+
expect(Chore::Worker).to receive(:new).with(job, {}).and_return(worker)
|
41
|
+
subject.assign(job)
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'can be called multiple times' do
|
45
|
+
expect(Chore::Worker).to receive(:new).twice.with(job, {}).and_return(worker)
|
46
|
+
2.times { subject.assign(job) }
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'should release the worker if an exception occurs' do
|
50
|
+
allow_any_instance_of(Chore::Worker).to receive(:start).and_raise(ArgumentError)
|
51
|
+
expect(subject).to receive(:release_worker)
|
52
|
+
|
53
|
+
begin
|
54
|
+
subject.assign(job)
|
55
|
+
rescue ArgumentError
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
34
59
|
end
|