shotgunner 0.1.0 → 0.1.5

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: 32f3dfa30655a6843d61bffc54e25c80c50e6fdb6965ec389f6ce1af2cb8367e
4
- data.tar.gz: 6671f973787b736fd090eb169f3e2ac1a2a89dc373d7f64c5495bc7319459c80
3
+ metadata.gz: 1ec304efcbca4c8edc0eb78f9257c261dbb9f5a86276ab742f6bef34dcbc8003
4
+ data.tar.gz: 050ca25cca81c3da5a4afc7921b20078e4aa249a020a3b59ef5aa3284902fb63
5
5
  SHA512:
6
- metadata.gz: ae49737f4e038d2ace4a667afc69e584e24d5798722126bd1b5fd83aecb8d060e9981e985210a670284b303ab6a2f669df8f30c99ea8137345064641b66bea9f
7
- data.tar.gz: dda54ed6fdf9d4fd9cac5248032d90664c512920d75bb68658b0f14994c77f841d36f7022e1305126dc18f0994bc2d088918e761f8fb72dd7170d11952ba4c91
6
+ metadata.gz: 918a61027e0d43e841a287bfc81fc4cbdac6da2092480fb16ff647360496892181ca169d68d6b2c9f97b5a91625d153bee7a1428d03fb22007abd84504008a2a
7
+ data.tar.gz: fd376df404ab44ffc9424772392e12eb6e4bf4093a1fdc16ceb81035d9de2712c58ac3cd56b88edf7e681b499deaf2d2b0ff67c1bda1ed5ebab4022f79763d0b
@@ -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' 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.0
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:
@@ -16,8 +16,8 @@ executables: []
16
16
  extensions: []
17
17
  extra_rdoc_files: []
18
18
  files:
19
- - lib/shotgunner.rb
20
- homepage:
19
+ - lib/shotgunner/parallel.rb
20
+ homepage: https://github.com/nucleom42/shotgunner
21
21
  licenses: []
22
22
  metadata: {}
23
23
  post_install_message: