shotgunner 0.1.3 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/shotgunner/parallel.rb +22 -7
  3. metadata +4 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0bd6afd5b6c1f99087fcb7954751e4e22ba361c60edb7c7908a220256d47d136
4
- data.tar.gz: 4af6839b27afbc55c84c0b0d6a86b0fd201d50f7104cb9a4dddfa0a67c8ed2c4
3
+ metadata.gz: 3c08913aab8f39266b071b796662097e01cccc22e46af6bbb0d7f53370042d74
4
+ data.tar.gz: 25b057a4846b34f1b713ab6a6c65b80e6251bacdc22c2a3bf581668c5790b71f
5
5
  SHA512:
6
- metadata.gz: 646316b5c02fe055e8763f2011003cf2c68b24e96ce301ab01d3a37950d5af74efbc65017ee60e01ba5a7cb8922328ac0052f57887bcbd3d5a49849b2747bff7
7
- data.tar.gz: 6ab8f72650cee928aca7a95bac8c8e517a92b9a04f45e069cd5a52728bd07d242a9686b4b34e961c8235876e410ec4ce9176c7e325f05a53cb8caac966f0c3bb
6
+ metadata.gz: df768f9d8813513d3a494dad52cffacb7ec8a49a3bb4f33a5af16acf1e4761c763ce55836a454d9b1b63945cdba35db1278173dab1c591b6e359b25c5b3c1b39
7
+ data.tar.gz: 69d8e6e92b5f68b42e53725a7a40dde784b56cc37350cd1bbfd9750e1eb1f5c0454c93ccba156c6759c25d88e429ca6cddb227dca06f83ecc95d367ebf4e843a
@@ -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.3
4
+ version: 0.1.8
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:
@@ -18,7 +18,8 @@ extra_rdoc_files: []
18
18
  files:
19
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: []