together 0.1.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b73278ba2777147e2ab5b22777701d6a6c2187c2
4
+ data.tar.gz: 5ae31441b1d63bfdbee822daa5237645895a6a30
5
+ SHA512:
6
+ metadata.gz: a8a700106acf1b5a01316e99e3a7d33fb9651a4f7bf3048b73b92d94022c1b46691295d0e0ee2b6ac3e5d743bb12654868c2a98503d6c5b8f0b15424b9940ef6
7
+ data.tar.gz: 519151911abd3b976f15b5c1bb536d548968794006fe47822af2f416ca6e19dc3c4e14a05d493d7abfaf5ae858b8dae99632399ffebd051ae06dac3564cb0c5e
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.3
5
+ before_install: gem install bundler -v 1.16.2
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in together.gemspec
6
+ gemspec
@@ -0,0 +1,28 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ together (0.1.0)
5
+ mandate
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ mandate (0.1.2)
11
+ metaclass (0.0.4)
12
+ minitest (5.11.3)
13
+ mocha (1.5.0)
14
+ metaclass (~> 0.0.1)
15
+ rake (10.4.2)
16
+
17
+ PLATFORMS
18
+ ruby
19
+
20
+ DEPENDENCIES
21
+ bundler (~> 1.16)
22
+ minitest (~> 5.0)
23
+ mocha
24
+ rake (~> 10.0)
25
+ together!
26
+
27
+ BUNDLED WITH
28
+ 1.16.2
@@ -0,0 +1,84 @@
1
+ # Together
2
+
3
+ Together helps you execute parallel code easily in different threads. It works particuarly well with the [Mandate](https://github.com/iHiD/mandate) gem.
4
+
5
+ ## Usage
6
+
7
+ Together takes any objects that have `call` methods and executes them in parallel.
8
+
9
+ ```
10
+ Together.(
11
+ Proc.new { :foo },
12
+ Proc.new { :bar }
13
+ )
14
+
15
+ # => [:foo, :bar]
16
+ ```
17
+
18
+ Together has the following settings which can be passed into the method call:
19
+ - `timeout: 1`: How long before timing out?
20
+ - `raise_exceptions: true`: Raise any exceptions that happen in any of the sub-calls. If set to `false`, this will not raise exceptions but instead store them in an array called `exceptions`.
21
+
22
+ e.g.
23
+ ```
24
+ Together.(
25
+ Proc.new { :foo },
26
+ timeout: 2,
27
+ raise_exceptions: false
28
+ )
29
+ ```
30
+
31
+ ### Notes
32
+
33
+ The `timeout` param is applied from the moment the Together function is executed. Therefore the first thread gets a tiny amount longer to execute than the last. This is a deliberate decision.
34
+
35
+ ## Installation
36
+
37
+ Add this line to your application's Gemfile:
38
+
39
+ ```ruby
40
+ gem 'together'
41
+ ```
42
+
43
+ And then execute:
44
+
45
+ $ bundle
46
+
47
+ Or install it yourself as:
48
+
49
+ $ gem install together
50
+
51
+ ## Usage
52
+
53
+ TODO: Write usage instructions here
54
+
55
+ ## Development
56
+
57
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
58
+
59
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
60
+
61
+ ## Contributing
62
+
63
+ Firstly, thank you!! :heart::sparkling_heart::heart:
64
+
65
+ We'd love to have you involved. Please read our [contributing guide](https://github.com/iHiD/together/tree/master/CONTRIBUTING.md) for information on how to get stuck in.
66
+
67
+ ### Contributors
68
+
69
+ This project is managed by [Jeremy Walker](http://ihid.co.uk).
70
+
71
+ ## Licence
72
+
73
+ Copyright (C) 2017 Jeremy Walker
74
+
75
+ This program is free software: you can redistribute it and/or modify
76
+ it under the terms of the MIT License.
77
+
78
+ This program is distributed in the hope that it will be useful,
79
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
80
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
81
+ MIT License for more details.
82
+
83
+ A copy of the MIT License is available in [LICENCE.md](https://github.com/iHiD/together/blob/master/LICENCE.md)
84
+ along with this program.
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "together"
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,18 @@
1
+ require 'mandate'
2
+
3
+ require "together/version"
4
+ require "together/exceptions"
5
+
6
+ require "together/results"
7
+ require "together/executor"
8
+
9
+ module Together
10
+ def self.call(*mandates,
11
+ timeout: 1,
12
+ raise_exceptions: true)
13
+
14
+ Executor.(mandates,
15
+ timeout: timeout,
16
+ raise_exceptions: raise_exceptions)
17
+ end
18
+ end
@@ -0,0 +1,4 @@
1
+ module Together
2
+ class TimeoutError < RuntimeError
3
+ end
4
+ end
@@ -0,0 +1,60 @@
1
+ module Together
2
+ class Executor
3
+ include Mandate
4
+
5
+ attr_reader :mandates, :timeout_time, :raise_exceptions,
6
+ :threads,
7
+ :results, :exceptions
8
+
9
+ # Timeout is in seconds
10
+ def initialize(mandates,
11
+ timeout: ,
12
+ raise_exceptions: )
13
+ @mandates = mandates
14
+ @timeout_time = Time.now.to_f + timeout
15
+ @raise_exceptions = raise_exceptions
16
+
17
+ @results = Results.new(mandates.size)
18
+ @threads = []
19
+ end
20
+
21
+ def call
22
+ execute_mandates
23
+ wait_for_threads
24
+ results
25
+ end
26
+
27
+ private
28
+
29
+ def execute_mandates
30
+ mandates.each.with_index do |mandate, i|
31
+ @threads << Thread.new do
32
+ begin
33
+ @results[i] = mandate.()
34
+ rescue => e
35
+ @results.exceptions[i] = e
36
+ end
37
+ end
38
+ end
39
+ end
40
+
41
+ def wait_for_threads
42
+ while Time.now.to_f < timeout_time
43
+
44
+ # It is important to check for exceptions before
45
+ # checking to see if anything is finished else we hit
46
+ # a race condition.
47
+ if raise_exceptions && !results.exceptions.all?(&:nil?)
48
+ raise results.exceptions.find{|e|!e.nil?}
49
+ end
50
+
51
+ return true unless threads.any?(&:alive?)
52
+
53
+ sleep(0.001)
54
+ end
55
+
56
+ raise TimeoutError.new
57
+ end
58
+ end
59
+ end
60
+
@@ -0,0 +1,14 @@
1
+ module Together
2
+ class Results < Array
3
+
4
+ attr_reader :exceptions
5
+ def initialize(size, *args)
6
+ super(size, *args)
7
+ @exceptions = Array.new(size)
8
+ end
9
+
10
+ def success?
11
+ @exceptions.all?(&:nil?)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ module Together
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,30 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "together/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "together"
8
+ spec.version = Together::VERSION
9
+ spec.authors = ["Jeremy Walker"]
10
+ spec.email = ["jez.walker@gmail.com"]
11
+
12
+ spec.summary = %q{A simple parallelizer for Ruby}
13
+ spec.description = %q{A simple parallelizer for Ruby}
14
+ spec.homepage = "https://github.com/iHiD/together"
15
+
16
+ # Specify which files should be added to the gem when it is released.
17
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
18
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
19
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ end
21
+ spec.bindir = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_dependency "mandate"
26
+ spec.add_development_dependency "bundler", "~> 1.16"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "minitest", "~> 5.0"
29
+ spec.add_development_dependency "mocha"
30
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: together
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jeremy Walker
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-06-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mandate
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
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: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.16'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.16'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: mocha
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: A simple parallelizer for Ruby
84
+ email:
85
+ - jez.walker@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".travis.yml"
92
+ - Gemfile
93
+ - Gemfile.lock
94
+ - README.md
95
+ - Rakefile
96
+ - bin/console
97
+ - bin/setup
98
+ - lib/together.rb
99
+ - lib/together/exceptions.rb
100
+ - lib/together/executor.rb
101
+ - lib/together/results.rb
102
+ - lib/together/version.rb
103
+ - together.gemspec
104
+ homepage: https://github.com/iHiD/together
105
+ licenses: []
106
+ metadata: {}
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubyforge_project:
123
+ rubygems_version: 2.6.13
124
+ signing_key:
125
+ specification_version: 4
126
+ summary: A simple parallelizer for Ruby
127
+ test_files: []
128
+ has_rdoc: