pwork 1.4.0 → 1.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/pwork/async.rb +5 -13
- data/lib/pwork/async/manager.rb +14 -52
- data/lib/pwork/async/task.rb +2 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 83211819e79a3fa139b22dec319bdc104cd26a4740c0a12b1e98beac4bb46858
|
4
|
+
data.tar.gz: 322a1b55a085eea1bb48c54f46f2180cbef93d812e63c0a2d960111bb25a5715
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6d0f8d800344762f067c498f7969541a35b511663339639f92dbf4831fdf10e1ccc97a4aeb2f78443f2795b0fdfc724233805bc7070fbb1a32fd7210b422ed29
|
7
|
+
data.tar.gz: 2a951e58edc74b7efc65f0bc3ba55acc88418b4b14efd16f514d458077a834756440acf5de31f99d1e61999ba9fc4cd1cef8d51251038a08f050ac5de01ceff4
|
data/lib/pwork/async.rb
CHANGED
@@ -11,7 +11,7 @@ module PWork
|
|
11
11
|
when 'test'
|
12
12
|
PWork::Async.async_test(options, &block)
|
13
13
|
else
|
14
|
-
PWork::Async.async_threaded(options, &block)
|
14
|
+
PWork::Async.async_threaded(options, self, &block)
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
@@ -33,12 +33,12 @@ module PWork
|
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
36
|
-
def self.async_threaded(options = {}, &block)
|
36
|
+
def self.async_threaded(options = {}, caller, &block)
|
37
37
|
if block_given?
|
38
|
-
options[:caller] =
|
38
|
+
options[:caller] = caller
|
39
39
|
PWork::Async.add_task(options, &block)
|
40
40
|
else
|
41
|
-
PWork::Async.wait_for_tasks({ caller:
|
41
|
+
PWork::Async.wait_for_tasks({ caller: caller, command: options })
|
42
42
|
end
|
43
43
|
end
|
44
44
|
|
@@ -47,8 +47,6 @@ module PWork
|
|
47
47
|
end
|
48
48
|
|
49
49
|
def self.add_task(options, &block)
|
50
|
-
manager.start unless manager.running
|
51
|
-
|
52
50
|
task = PWork::Async::Task.new.tap do |e|
|
53
51
|
e.block = block
|
54
52
|
e.caller = options[:caller]
|
@@ -75,9 +73,7 @@ module PWork
|
|
75
73
|
task_list = tasks.select { |t| t.caller == options[:caller] }
|
76
74
|
end
|
77
75
|
|
78
|
-
|
79
|
-
sleep(async_wait_sleep_iteration)
|
80
|
-
end
|
76
|
+
task_list.each { |t| t.thread.join }
|
81
77
|
|
82
78
|
handle_errors
|
83
79
|
|
@@ -96,10 +92,6 @@ module PWork
|
|
96
92
|
true
|
97
93
|
end
|
98
94
|
|
99
|
-
def self.async_wait_sleep_iteration
|
100
|
-
Float(ENV.fetch('PWORK_ASYNC_WAIT_SLEEP_ITERATION', '0.02'))
|
101
|
-
end
|
102
|
-
|
103
95
|
def self.mode
|
104
96
|
ENV.fetch('PWORK_ASYNC_MODE', 'thread').to_s.downcase
|
105
97
|
end
|
data/lib/pwork/async/manager.rb
CHANGED
@@ -3,53 +3,20 @@ require 'thread'
|
|
3
3
|
module PWork
|
4
4
|
module Async
|
5
5
|
class Manager
|
6
|
-
def initialize
|
7
|
-
@queue = Queue.new
|
8
|
-
@threads = []
|
9
|
-
@running = false
|
10
|
-
end
|
11
|
-
|
12
|
-
# This method is called to determine if this manager is running or not
|
13
|
-
def running
|
14
|
-
@running
|
15
|
-
end
|
16
|
-
|
17
|
-
# This method is called to start the async manager
|
18
|
-
def start
|
19
|
-
@running = true
|
20
|
-
pool_size.times do
|
21
|
-
@threads << Thread.new do
|
22
|
-
until @running == false
|
23
|
-
process_task
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
# This method is called to stop the async manager
|
30
|
-
def stop
|
31
|
-
@running = false
|
32
|
-
@threads = []
|
33
|
-
end
|
34
|
-
|
35
|
-
# This method is called to determine the size of the async thread pool
|
36
|
-
def pool_size
|
37
|
-
Integer(ENV.fetch('PWORK_ASYNC_POOL_SIZE', 10))
|
38
|
-
end
|
39
|
-
|
40
6
|
# This method is called to process an async task from the queue
|
41
|
-
def process_task
|
42
|
-
task =
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
7
|
+
def process_task(task)
|
8
|
+
task.thread = Thread.new do
|
9
|
+
task.state = :active
|
10
|
+
thread_helper.set_thread_vars(task.thread_local_storage)
|
11
|
+
begin
|
12
|
+
task.block.call
|
13
|
+
task.state = :complete
|
14
|
+
rescue => e
|
15
|
+
task.error = e
|
16
|
+
task.state = :error
|
17
|
+
ensure
|
18
|
+
thread_helper.reset_thread_vars
|
19
|
+
end
|
53
20
|
end
|
54
21
|
end
|
55
22
|
|
@@ -58,18 +25,13 @@ module PWork
|
|
58
25
|
PWork::Helpers::Threads
|
59
26
|
end
|
60
27
|
|
61
|
-
# This method is called to determine the number of tasks in the queue
|
62
|
-
def queue_count
|
63
|
-
@queue.length
|
64
|
-
end
|
65
|
-
|
66
28
|
# This method is called to add a task to the queue
|
67
29
|
def add_task(task)
|
68
30
|
raise PWork::Async::Exceptions::InvalidOptionsError.new(
|
69
31
|
'A valid async task must be specified.'
|
70
32
|
) unless task.is_a?(PWork::Async::Task)
|
71
33
|
task.thread_local_storage = PWork::Helpers::Threads.get_thread_vars
|
72
|
-
|
34
|
+
process_task(task)
|
73
35
|
end
|
74
36
|
end
|
75
37
|
end
|
data/lib/pwork/async/task.rb
CHANGED
@@ -8,10 +8,11 @@ module PWork
|
|
8
8
|
attr_accessor :error
|
9
9
|
attr_accessor :block
|
10
10
|
attr_accessor :caller
|
11
|
+
attr_accessor :thread
|
11
12
|
def initialize
|
12
13
|
self.id = SecureRandom.uuid
|
13
14
|
self.state = :pending
|
14
|
-
self.thread_local_storage =
|
15
|
+
self.thread_local_storage = {}
|
15
16
|
end
|
16
17
|
end
|
17
18
|
end
|