shotgunner 0.1.4 → 0.1.5

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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/shotgunner/parallel.rb +22 -7
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6ec6606a8eab14fe6a3e852250000220fac0241fb7acb77d87e81eddd9545115
4
- data.tar.gz: f278873f790da5b90a14c0664dea0941778346a2628c58737ed1905df9d1745f
3
+ metadata.gz: 1ec304efcbca4c8edc0eb78f9257c261dbb9f5a86276ab742f6bef34dcbc8003
4
+ data.tar.gz: 050ca25cca81c3da5a4afc7921b20078e4aa249a020a3b59ef5aa3284902fb63
5
5
  SHA512:
6
- metadata.gz: eab16fc2914834b6a003f8c37735b8226544fe538ebf004fc06c283dae6f743ed5a2d889408b3c35ade831a742063f5ef3f03c565bcfa3f2deed2f2b388bd083
7
- data.tar.gz: 546bf51dacc3fdfee6761a9723a723ac60161bb18bd3cffd454be25d8ae1666ae31dea623685f4bfeb4eae6ca8d784485f3b0e91ac336098e76daedc023a5f8d
6
+ metadata.gz: 918a61027e0d43e841a287bfc81fc4cbdac6da2092480fb16ff647360496892181ca169d68d6b2c9f97b5a91625d153bee7a1428d03fb22007abd84504008a2a
7
+ data.tar.gz: fd376df404ab44ffc9424772392e12eb6e4bf4093a1fdc16ceb81035d9de2712c58ac3cd56b88edf7e681b499deaf2d2b0ff67c1bda1ed5ebab4022f79763d0b
@@ -15,20 +15,35 @@ module Shotgunner
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]&.dup || []
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' if threads == (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.4
4
+ version: 0.1.5
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: