concurrent_executor 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8f1975dab1c7cd6e9f3d28d145d6dbb9dbe56b9e2cdffbded41d1c429abe9b6c
4
+ data.tar.gz: b11700ed8f4e9fe790840b9381949b9917ba2433b2c0919dac981c2d7eaa4622
5
+ SHA512:
6
+ metadata.gz: 2b5c684a7a533597d6948e64b07cd85fb22d95c8b05b46fbe2fa3c542abba1db2b677d6c7902bd2e4c9514dd863195e847234476bdae89789404781fadd2120b
7
+ data.tar.gz: a4da54f9daa7366c557ffde0731334abaf3970af134a34c45a6d5e8fc0b5d68a52821126990ea41989601b90b294cda94665058066532c598e6e18e642baba52
@@ -0,0 +1,3 @@
1
+
2
+ .rspec_status
3
+ coverage
@@ -0,0 +1,91 @@
1
+ AllCops:
2
+ Exclude:
3
+ - Makefile
4
+ - vendor/**/*
5
+ - bin/**/*
6
+
7
+ Layout/EndOfLine:
8
+ Enabled: false
9
+
10
+ Style/DateTime:
11
+ Enabled: false
12
+
13
+ Style/Documentation:
14
+ Enabled: false
15
+
16
+ Lint/Debugger:
17
+ Enabled: true
18
+
19
+ Style/FrozenStringLiteralComment:
20
+ Enabled: true
21
+ EnforcedStyle: always
22
+
23
+ Style/TrailingCommaInHashLiteral:
24
+ Enabled: true
25
+ EnforcedStyleForMultiline: comma
26
+
27
+ Style/TrailingCommaInArrayLiteral:
28
+ Enabled: true
29
+ EnforcedStyleForMultiline: comma
30
+
31
+ Style/TrailingCommaInArguments:
32
+ Enabled: true
33
+ EnforcedStyleForMultiline: comma
34
+
35
+ Lint/UnusedMethodArgument:
36
+ AllowUnusedKeywordArguments: true
37
+
38
+ Layout/LineLength:
39
+ Enabled: true
40
+ Max: 280
41
+ IgnoreCopDirectives: true
42
+ IgnoredPatterns: ['\A#', '\A\s*sig { .* }\Z']
43
+ Exclude:
44
+ - '**/*_pb.rb'
45
+
46
+ Metrics/AbcSize:
47
+ Enabled: true
48
+ Max: 48
49
+
50
+ Metrics/CyclomaticComplexity:
51
+ Max: 9
52
+
53
+ Metrics/MethodLength:
54
+ Enabled: true
55
+ Max: 32
56
+
57
+ Layout/ParameterAlignment:
58
+ Enabled: true
59
+ EnforcedStyle: with_fixed_indentation
60
+
61
+ Naming/MethodParameterName:
62
+ Enabled: true
63
+ AllowedNames: ['io', 'id', 'to', 'by', 'on', 'in', 'at', '_'] # Defaults + _
64
+
65
+ Layout/MultilineMethodCallIndentation:
66
+ Enabled: true
67
+ EnforcedStyle: indented
68
+
69
+ Style/ParallelAssignment:
70
+ Enabled: true
71
+
72
+ Metrics/ClassLength:
73
+ Max: 240
74
+
75
+ Metrics/BlockLength:
76
+ Max: 30
77
+ Exclude:
78
+ - spec/**/*.rb
79
+ - '**/*_pb.rb'
80
+
81
+ Metrics/ParameterLists:
82
+ Max: 6
83
+
84
+ Lint/AmbiguousBlockAssociation:
85
+ Exclude:
86
+ - spec/**/*.rb
87
+
88
+ Style/BlockDelimiters:
89
+ Enabled: true
90
+ Exclude:
91
+ - spec/**/*
@@ -0,0 +1,8 @@
1
+ inherit_from: https://raw.githubusercontent.com/GetTerminus/ruby_shared_configs/master/.rubocop.yml
2
+
3
+ AllCops:
4
+ Exclude:
5
+ - Makefile
6
+ - vendor/**/*
7
+ - bin/*
8
+ - Guardfile
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in concurrent_executor.gemspec
6
+ gemspec
7
+
8
+ gem 'pry'
@@ -0,0 +1,70 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ concurrent_executor (1.0.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.0)
10
+ codecov (0.1.16)
11
+ json
12
+ simplecov
13
+ url
14
+ coderay (1.1.2)
15
+ diff-lcs (1.3)
16
+ docile (1.3.2)
17
+ jaro_winkler (1.5.4)
18
+ json (2.3.0)
19
+ method_source (1.0.0)
20
+ parallel (1.19.1)
21
+ parser (2.7.0.2)
22
+ ast (~> 2.4.0)
23
+ pry (0.13.0)
24
+ coderay (~> 1.1)
25
+ method_source (~> 1.0)
26
+ rainbow (3.0.0)
27
+ rake (12.3.3)
28
+ rexml (3.2.4)
29
+ rspec (3.9.0)
30
+ rspec-core (~> 3.9.0)
31
+ rspec-expectations (~> 3.9.0)
32
+ rspec-mocks (~> 3.9.0)
33
+ rspec-core (3.9.1)
34
+ rspec-support (~> 3.9.1)
35
+ rspec-expectations (3.9.1)
36
+ diff-lcs (>= 1.2.0, < 2.0)
37
+ rspec-support (~> 3.9.0)
38
+ rspec-mocks (3.9.1)
39
+ diff-lcs (>= 1.2.0, < 2.0)
40
+ rspec-support (~> 3.9.0)
41
+ rspec-support (3.9.2)
42
+ rubocop (0.80.0)
43
+ jaro_winkler (~> 1.5.1)
44
+ parallel (~> 1.10)
45
+ parser (>= 2.7.0.1)
46
+ rainbow (>= 2.2.2, < 4.0)
47
+ rexml
48
+ ruby-progressbar (~> 1.7)
49
+ unicode-display_width (>= 1.4.0, < 1.7)
50
+ ruby-progressbar (1.10.1)
51
+ simplecov (0.18.5)
52
+ docile (~> 1.1)
53
+ simplecov-html (~> 0.11)
54
+ simplecov-html (0.12.2)
55
+ unicode-display_width (1.6.1)
56
+ url (0.3.2)
57
+
58
+ PLATFORMS
59
+ ruby
60
+
61
+ DEPENDENCIES
62
+ codecov
63
+ concurrent_executor!
64
+ pry
65
+ rake (~> 12)
66
+ rspec (~> 3)
67
+ rubocop
68
+
69
+ BUNDLED WITH
70
+ 2.1.4
@@ -0,0 +1,29 @@
1
+ # concurrent_executor
2
+ Eat your enumerables in threads and fast!
3
+
4
+ ## Installation
5
+
6
+ Add this line to your application's Gemfile:
7
+
8
+ ```ruby
9
+ gem 'concurrent_executer'
10
+ ```
11
+
12
+ And then execute:
13
+
14
+ $ bundle install
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install concurrent_executer
19
+
20
+ ## Usage
21
+ ```ruby
22
+ require 'concurrent_executer'
23
+
24
+ some_enumerable = [1, 2, 3, 4]
25
+ ConcurrentExecutor.consume_enumerable(some_enumerable) do |num|
26
+ # do something to each item
27
+ end
28
+ ```
29
+
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "concurrent_executor"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/concurrent_executor/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'concurrent_executor'
7
+ spec.version = ConcurrentExecutorVersion::VERSION
8
+ spec.authors = ['Brian Malinconico']
9
+ spec.email = ['bmalinconico@terminus.com']
10
+
11
+ spec.summary = 'Execute things concurrently'
12
+ spec.description = 'Executes things concurrently'
13
+ spec.homepage = 'https://github.com/GetTerminus/concurrent_executor'
14
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.5.0')
15
+
16
+ spec.metadata['allowed_push_host'] = 'https://www.rubygems.org'
17
+
18
+ spec.metadata['homepage_uri'] = spec.homepage
19
+ spec.metadata['source_code_uri'] = 'https://github.com/GetTerminus/concurrent_executor'
20
+ spec.metadata['changelog_uri'] = 'https://github.com/GetTerminus/concurrent_executor/blob/master/CHANGELOG.md'
21
+
22
+ # Specify which files should be added to the gem when it is released.
23
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
25
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ end
27
+ spec.bindir = 'exe'
28
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ['lib']
30
+
31
+ spec.add_development_dependency 'codecov'
32
+ spec.add_development_dependency 'rake', '~> 12'
33
+ spec.add_development_dependency 'rspec', '~> 3'
34
+ spec.add_development_dependency 'rubocop'
35
+ end
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'logger'
4
+
5
+ class ConcurrentExecutor
6
+ MAX_NUMBER_OF_THREADS = 100
7
+ attr_accessor :threads, :queue, :number_of_threads, :executor, :logger
8
+
9
+ def initialize(number_of_threads: 4, queue_size: 100, executor: nil)
10
+ raise 'queue must be sized' unless queue_size
11
+ raise 'number of threads must be > 0' unless (0..MAX_NUMBER_OF_THREADS).cover?(number_of_threads)
12
+
13
+ @errors = Queue.new
14
+ @errored = false
15
+
16
+ self.logger = if defined?(Rails)
17
+ Rails.logger
18
+ elsif defined?(App) && App.respond_to?(:logger)
19
+ App.logger
20
+ else
21
+ Logger.new(STDERR)
22
+ end
23
+
24
+ self.threads = []
25
+ self.executor = executor
26
+ self.queue = SizedQueue.new(queue_size)
27
+ self.number_of_threads = number_of_threads
28
+
29
+ start_threads
30
+ end
31
+
32
+ def consume_enumerable(enum)
33
+ enum.each(&queue.method(:push))
34
+ rescue ClosedQueueError
35
+ self.class.logger.warn 'Queue closed during iteration'
36
+ end
37
+
38
+ class << self
39
+ def consume_enumerable(enum, **args, &blk)
40
+ executor = new(args.merge(executor: blk))
41
+ executor.consume_enumerable(enum)
42
+ rescue StandardError => e
43
+ puts e
44
+ raise e
45
+ ensure
46
+ executor&.graceful_shutdown
47
+ end
48
+ end
49
+
50
+ def graceful_shutdown
51
+ queue.close
52
+ threads.map(&:join)
53
+ raise @errors.pop(false) unless @errors.empty?
54
+ end
55
+
56
+ private
57
+
58
+ def start_threads
59
+ number_of_threads.times do
60
+ threads << if Thread.respond_to?(:new_traced)
61
+ Thread.new_traced(&method(:work_loop))
62
+ else
63
+ Thread.new(&method(:work_loop))
64
+ end
65
+ end
66
+ end
67
+
68
+ def work_loop
69
+ while (work_item = queue.pop)
70
+ begin
71
+ executor.call(work_item)
72
+ rescue StandardError => e
73
+ @errors << e
74
+ queue.clear
75
+ queue.close
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConcurrentExecutorVersion
4
+ VERSION = '1.0.0'
5
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: concurrent_executor
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Brian Malinconico
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-04-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: codecov
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '12'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '12'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Executes things concurrently
70
+ email:
71
+ - bmalinconico@terminus.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rubocop-https---raw-githubusercontent-com-GetTerminus-ruby-shared-configs-master--rubocop-yml"
78
+ - ".rubocop.yml"
79
+ - Gemfile
80
+ - Gemfile.lock
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - concurrent_executor.gemspec
86
+ - lib/concurrent_executor.rb
87
+ - lib/concurrent_executor/version.rb
88
+ homepage: https://github.com/GetTerminus/concurrent_executor
89
+ licenses: []
90
+ metadata:
91
+ allowed_push_host: https://www.rubygems.org
92
+ homepage_uri: https://github.com/GetTerminus/concurrent_executor
93
+ source_code_uri: https://github.com/GetTerminus/concurrent_executor
94
+ changelog_uri: https://github.com/GetTerminus/concurrent_executor/blob/master/CHANGELOG.md
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: 2.5.0
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubygems_version: 3.0.1
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: Execute things concurrently
114
+ test_files: []