shotgunner 0.1.1 → 0.1.6

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
  SHA256:
3
- metadata.gz: 634fceedc5b6c81c68dd0337c018a4422dbc033e309186d6693ae8632fbbcce9
4
- data.tar.gz: 5404cbd0b26740c65f690cfba0e49837b7b24730f28afa855eccbe5b5a6503c9
3
+ metadata.gz: 85cd18f2f681df1a5c63ecdeb70d2f42d6bf60a65b47f1468c72a744e5043fed
4
+ data.tar.gz: c9cc0ebe0eb0e484096701cbe1eb4f0ba9c2e5b70eefc8c68e2d4f504b31d3ca
5
5
  SHA512:
6
- metadata.gz: 609d03631f4b3454d9dd9afd38e7a488952d4326a708c63e2b241c39084d3e73524b8d9e3afb6f9baaa63d8d5b8a26f67b0aeed592ddc2594a3f53000d651299
7
- data.tar.gz: 16dfd8dce8f1b090d952c9a836c0ff90b4a72eff0885fafd95c4978309373c2e839e83834c17aad76e3bd732f99be809b5083ea42ed79eb3dac17c1e7db01fc0
6
+ metadata.gz: e74d52c2a3a5917fea3ab9e84c3204673bf08281348391a77db3ee6dc886699b95427bbbbcf002c82344ad7770da6d1660c42f7da1510453ea400e4abf92d2c4
7
+ data.tar.gz: e326e3b6d6511db5b4cd81a2f7b6abbe4e844beba787ec73b589f47b2cf6724bee06a803efb4439319a11e11950dc3b504130bd1b99db4cc87f808cc3f1dfead
@@ -9,26 +9,41 @@ module Shotgunner
9
9
 
10
10
  module InstanceMethods
11
11
  def run(options = {}, &block)
12
- self.class.call(options, &block)
12
+ self.class.run(options, &block)
13
13
  end
14
14
  end
15
15
 
16
16
  module ClassMethods
17
17
  def run(options = {}, &block)
18
- mutex = Mutex.new
19
- threads = options[:threads] || 4
20
- tasks = options[:tasks] || []
21
- raise ArgumentError, 'There is no tasks array defined!' if tasks.empty?
22
- call_results = []
18
+ call_results, mutex, tasks, threads = initiate(options)
19
+
20
+ validate_params(tasks, threads)
21
+
23
22
  threads.times.map do
24
23
  Thread.new(tasks, call_results) do |tasks, call_results|
25
24
  while (task = mutex.synchronize { tasks.pop })
26
25
  call_result = yield(task)
27
- mutex.synchronize { call_results << call_result }
26
+ mutex.synchronize { call_results[task] = call_result }
28
27
  end
29
28
  end
30
29
  end.each(&:join)
31
- call_results
30
+
31
+ call_results.map{|v| v[1]}
32
+ end
33
+
34
+ private
35
+
36
+ def validate_params(tasks, threads)
37
+ raise ArgumentError, 'There is no tasks array defined!' if tasks.empty?
38
+ raise ArgumentError, 'Invalid threads number, please select number from 1..100' unless threads&.between?(1, 100)
39
+ end
40
+
41
+ def initiate(options)
42
+ mutex = Mutex.new
43
+ threads = options[:threads] || 4
44
+ tasks = options[:tasks]&.dup || []
45
+ call_results = Hash[tasks.map { |x| [x, nil] }]
46
+ [call_results, mutex, tasks, threads]
32
47
  end
33
48
  end
34
49
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shotgunner
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oleg Saltykov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-02-18 00:00:00.000000000 Z
11
+ date: 2021-05-04 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email:
@@ -16,7 +16,7 @@ executables: []
16
16
  extensions: []
17
17
  extra_rdoc_files: []
18
18
  files:
19
- - lib/shotgunner.rb
19
+ - lib/shotgunner/parallel.rb
20
20
  homepage: https://github.com/nucleom42/shotgunner
21
21
  licenses: []
22
22
  metadata: {}