shotgunner 0.1.2 → 0.1.7

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: 0e26cf24b66858379c2686db0cbb1b0c2ee9b79bad5651081ddd2201962c04ec
4
- data.tar.gz: 85360701416bb02f73d0d0affac60c2d5b5903c78edcf9c432c15c8eaca44264
3
+ metadata.gz: f467b5b6f1e41e9fbe2ee7f639412aa95b2a735f4c698033159e5577d7759e26
4
+ data.tar.gz: '084a8f4bd422dd8edc9193f7c6edfb30c34e38a15f17f390c66fa54ffa056716'
5
5
  SHA512:
6
- metadata.gz: 5fff505543d55d3f47b06314b1c47ab2146fb80dcbf733c7ef4a58d5f9ec951c8861e92c109b360f968851c435692464ac7647cc97336d75b3eea19fef7c6a3b
7
- data.tar.gz: 5dcf323cb11bfd71f26b53033388cf973a17b9dc129d9512e9e3e978423f9b9a4f6985267f8483c87fae46896e05e453e1cd91e9bb0a3fdb3f5ac2cf93fe4e00
6
+ metadata.gz: 1f33b2f4304e5d61571cd843b2db91552c34c9d894acaa14a189a70da1a4567d20009bd92b1e3f1afae786911f9adf72753b84bb8ad177226a714b67f5aa1306
7
+ data.tar.gz: 9f471c6da8bf5ac4e3e36211b6aad0c83f986e8025e985d1c1dd686324a66910c64e10c350e2cb3a4709a99c50199d1ca23c692559264b430313f94c99201a9c
@@ -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] || []
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.2
4
+ version: 0.1.7
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,9 +16,10 @@ 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
- licenses: []
21
+ licenses:
22
+ - MIT
22
23
  metadata: {}
23
24
  post_install_message:
24
25
  rdoc_options: []