pwork 1.0.1 → 1.1.0

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
- SHA1:
3
- metadata.gz: 2731486c2af84b607e31651780ca3b57511e1496
4
- data.tar.gz: ddbe9066147e3a962302493eecc992541a221290
2
+ SHA256:
3
+ metadata.gz: 249a3ee4b5ad1dd558bcdae10f0309404ec9841693c1f22714904cc8c70f43fb
4
+ data.tar.gz: 260de7aa9d02fad3f92faf0b00c70bb61689e8a03db64a9bac491c6566b900b3
5
5
  SHA512:
6
- metadata.gz: 75958149fd8a60da17ff86b7e1d2a9750944d1f4a5e5bc0177a416f6ab7c4795f46350cd694395414813112859684cd456c57fb8e0613e9e33e8f7d2149a7a38
7
- data.tar.gz: 25fc545ed0139c580d5f5f8fcf6fd80b87cd17688603882984421729480feeb1b332e482cda1c01f1b69911599e8f301a1e3b693d2e0e6d2d37ff2057a20fd17
6
+ metadata.gz: 9da793781dfdfa26129d40240783b5c0b7a923ac6ee42297c9632e02308ad5fa5fee4fecb0b5a8ffc887d5df9f76dafe72739cf6ca4eacdae2637af27930024d
7
+ data.tar.gz: 0833eacb1192ca4fce94848b952aa151f0c92767e077c85573f15c4acc544133c974c6522e8900b82d85c94be170115e9e6dc5e062b8c888147b4016be10fa78
data/lib/pwork.rb CHANGED
@@ -1,4 +1,3 @@
1
- require 'pwork/version'
2
1
  require_relative 'pwork/helpers/thread'
3
2
 
4
3
  require_relative 'pwork/array'
@@ -8,4 +7,6 @@ if RUBY_PLATFORM =~ /java/
8
7
  require_relative 'pwork/jruby/thread_pool'
9
8
  else
10
9
  require_relative 'pwork/mri/thread_pool'
11
- end
10
+ end
11
+
12
+ require_relative 'pwork/async'
@@ -0,0 +1,65 @@
1
+ require_relative 'async/task'
2
+ require_relative 'async/exceptions'
3
+ require_relative 'async/manager'
4
+
5
+ module PWork
6
+ module Async
7
+ def async(options = nil, &block)
8
+ if block_given?
9
+ PWork::Async.add_task(options, &block)
10
+ else
11
+ raise PWork::Async::Exceptions::InvalidOptionsError.new(
12
+ 'Unknown async option.'
13
+ ) unless options == :wait
14
+ PWork::Async.wait_for_tasks
15
+ end
16
+ end
17
+
18
+ def self.manager
19
+ @manager ||= PWork::Async::Manager.new
20
+ end
21
+
22
+ def self.add_task(options, &block)
23
+ manager.start unless manager.running
24
+
25
+ options = {} if options == nil
26
+ task = PWork::Async::Task.new.tap do |e|
27
+ e.block = block
28
+ end
29
+
30
+ unless options[:wait] == false
31
+ tasks << task
32
+ end
33
+
34
+ manager.add_task(task)
35
+
36
+ task.id
37
+ end
38
+
39
+ def self.tasks
40
+ Thread.current[:pwork_async_tasks] ||= []
41
+ end
42
+
43
+ def self.wait_for_tasks
44
+ until tasks.detect { |t| t.state == :pending || t.state == :active }.nil?
45
+ sleep(async_wait_sleep_iteration)
46
+ end
47
+ handle_errors
48
+ ensure
49
+ Thread.current[:pwork_async_tasks] = []
50
+ end
51
+
52
+ def self.handle_errors
53
+ error_messages = []
54
+ tasks.select { |t| t.state == :error }.each do |t|
55
+ error_messages << "Error: #{t.error.message}, #{t.error.backtrace}"
56
+ end
57
+ raise PWork::Async::Exceptions::TaskError.new("1 or more async errors occurred. #{error_messages.join(' | ')}") if error_messages.length > 0
58
+ true
59
+ end
60
+
61
+ def self.async_wait_sleep_iteration
62
+ Integer(ENV.fetch('PWORK_ASYNC_WAIT_SLEEP_ITERATION', 0.01))
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,2 @@
1
+ require_relative 'exceptions/invalid_options'
2
+ require_relative 'exceptions/task_error'
@@ -0,0 +1,11 @@
1
+ module PWork
2
+ module Async
3
+ module Exceptions
4
+ class InvalidOptionsError < StandardError
5
+ def initialize(message)
6
+ super(message)
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module PWork
2
+ module Async
3
+ module Exceptions
4
+ class TaskError < StandardError
5
+ def initialize(message)
6
+ super(message)
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,76 @@
1
+ require 'thread'
2
+
3
+ module PWork
4
+ module Async
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
+ # 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
53
+ end
54
+ end
55
+
56
+ # This method is called to expose the thread helper
57
+ def thread_helper
58
+ PWork::Helpers::Threads
59
+ end
60
+
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
+ # This method is called to add a task to the queue
67
+ def add_task(task)
68
+ raise PWork::Async::Exceptions::InvalidOptionsError.new(
69
+ 'A valid async task must be specified.'
70
+ ) unless task.is_a?(PWork::Async::Task)
71
+ task.thread_local_storage = PWork::Helpers::Threads.get_thread_vars
72
+ @queue.push(task)
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,17 @@
1
+ require 'securerandom'
2
+ module PWork
3
+ module Async
4
+ class Task
5
+ attr_accessor :id
6
+ attr_accessor :state
7
+ attr_accessor :thread_local_storage
8
+ attr_accessor :error
9
+ attr_accessor :block
10
+ def initialize
11
+ self.id = SecureRandom.uuid
12
+ self.state = :pending
13
+ self.thread_local_storage = []
14
+ end
15
+ end
16
+ end
17
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pwork
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
- - vaughanbrittonage
7
+ - vaughanbrittonsage
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-10-05 00:00:00.000000000 Z
11
+ date: 2018-02-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -69,7 +69,7 @@ dependencies:
69
69
  description: This is a library to facilitate with the execution of work on parallel
70
70
  threads.
71
71
  email:
72
- - vaughanbritton@gmail.com
72
+ - vaughan.britton@sage.com
73
73
  executables: []
74
74
  extensions: []
75
75
  extra_rdoc_files: []
@@ -78,12 +78,17 @@ files:
78
78
  - bin/setup
79
79
  - lib/pwork.rb
80
80
  - lib/pwork/array.rb
81
+ - lib/pwork/async.rb
82
+ - lib/pwork/async/exceptions.rb
83
+ - lib/pwork/async/exceptions/invalid_options.rb
84
+ - lib/pwork/async/exceptions/task_error.rb
85
+ - lib/pwork/async/manager.rb
86
+ - lib/pwork/async/task.rb
81
87
  - lib/pwork/helpers/thread.rb
82
88
  - lib/pwork/jruby/thread_pool.rb
83
89
  - lib/pwork/mri/thread_pool.rb
84
- - lib/pwork/version.rb
85
90
  - lib/pwork/worker.rb
86
- homepage: https://github.com/vaughanbrittonsage/pwork
91
+ homepage: https://github.com/sage/pwork
87
92
  licenses:
88
93
  - MIT
89
94
  metadata: {}
@@ -103,7 +108,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
103
108
  version: '0'
104
109
  requirements: []
105
110
  rubyforge_project:
106
- rubygems_version: 2.5.1
111
+ rubygems_version: 2.7.6
107
112
  signing_key:
108
113
  specification_version: 4
109
114
  summary: This is a library to facilitate with the execution of work on parallel threads.
data/lib/pwork/version.rb DELETED
@@ -1,3 +0,0 @@
1
- module PWork
2
- VERSION = "1.0.1"
3
- end