pwork 1.4.0 → 1.5.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0f4c565cd3cbbe417681e9a2b4a4809efe4423652af4f8ecaa7e7e989b87dabe
4
- data.tar.gz: ef49af7bb9a4212bce14de2676345bc42fb6db16cda9b270d856344d66da6a58
3
+ metadata.gz: 83211819e79a3fa139b22dec319bdc104cd26a4740c0a12b1e98beac4bb46858
4
+ data.tar.gz: 322a1b55a085eea1bb48c54f46f2180cbef93d812e63c0a2d960111bb25a5715
5
5
  SHA512:
6
- metadata.gz: 7201204d08a15b4fd655d4e92811dd0226ded7055b355a565acca60fd6250430e98d158310b0bd5df15edc009f0c746e59abcbf25f7aea88a42fa1762c4793d0
7
- data.tar.gz: 2aa3d668f78be0fc8bd7e7c0a21e90010b4e2bc58a58dc7a0cbe0991546c16a3762697b8a4c111f7762551352e2f52a0a00a0a29dd6f8d077c12fa34bcd5f7aa
6
+ metadata.gz: 6d0f8d800344762f067c498f7969541a35b511663339639f92dbf4831fdf10e1ccc97a4aeb2f78443f2795b0fdfc724233805bc7070fbb1a32fd7210b422ed29
7
+ data.tar.gz: 2a951e58edc74b7efc65f0bc3ba55acc88418b4b14efd16f514d458077a834756440acf5de31f99d1e61999ba9fc4cd1cef8d51251038a08f050ac5de01ceff4
@@ -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] = self
38
+ options[:caller] = caller
39
39
  PWork::Async.add_task(options, &block)
40
40
  else
41
- PWork::Async.wait_for_tasks({ caller: self, command: options })
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
- until task_list.detect { |t| t.state == :pending || t.state == :active }.nil?
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
@@ -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 = @queue.pop
43
- task.state = :active
44
- thread_helper.set_thread_vars(task.thread_local_storage)
45
- begin
46
- task.block.call
47
- task.state = :complete
48
- rescue => e
49
- task.error = e
50
- task.state = :error
51
- ensure
52
- thread_helper.reset_thread_vars
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
- @queue.push(task)
34
+ process_task(task)
73
35
  end
74
36
  end
75
37
  end
@@ -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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pwork
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.0
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - vaughanbrittonsage