gotta-run 0.1.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: fd304b4d143a068a54009c3203d6f83f067dda09657a0a420d2210f974a5ae0b
4
+ data.tar.gz: c99ef2cdb304e5a124de0a9e89b7e6c0543fb79e0efa1d146eead8ca84ffe7f8
5
+ SHA512:
6
+ metadata.gz: 8a243a515fd1e1450a90c1f5cbc3081803961496c4335a10daeb5e1876b9e4ae475e58f1cf5bc9bbcc06d1ee25091777fb7b22b4f13ea5a87f2667e764652409
7
+ data.tar.gz: 642599274c2663cb42eeadfed1c8b2d47085d1b7d2d5f21b149e1bad2fa65b7a7d70d185a3caf742a9d9ba301499e3237a822d59d25fa6c3f5253d25c2328ef4
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ *.gem
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,3 @@
1
+ AllCops:
2
+ Exclude:
3
+ - '*.gemspec'
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5.3
7
+ before_install: gem install bundler -v 2.0.1
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at paulo@parruda.net. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in gotta-run.gemspec
6
+ gemspec
@@ -0,0 +1,201 @@
1
+ Commons Clause License Condition
2
+ ================================
3
+
4
+ The Software is provided to you by the Licensor under the License,
5
+ as defined below, subject to the following condition.
6
+
7
+ Without limiting other conditions in the License, the grant of rights
8
+ under the License will not include, and the License does not grant to
9
+ you, the right to Sell the Software.
10
+
11
+ For purposes of the foregoing, "Sell" means practicing any or all of
12
+ the rights granted to you under the License to provide to third
13
+ parties, for a fee or other consideration (including without
14
+ limitation fees for hosting or consulting/ support services related
15
+ to the Software), a product or service whose value derives, entirely
16
+ or substantially, from the functionality of the Software. Any license
17
+ notice or attribution required by the License must also include this
18
+ license condition notice.
19
+
20
+ Software: gotta-run
21
+ License: Apache 2.0
22
+ Licensor: Paulo Arruda (parrudaj at gmail dot com)
23
+
24
+
25
+ Apache License
26
+ ==============
27
+
28
+ Version 2.0, January 2004
29
+ http://www.apache.org/licenses/
30
+
31
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
32
+
33
+ 1. Definitions.
34
+
35
+ "License" shall mean the terms and conditions for use, reproduction,
36
+ and distribution as defined by Sections 1 through 9 of this document.
37
+
38
+ "Licensor" shall mean the copyright owner or entity authorized by
39
+ the copyright owner that is granting the License.
40
+
41
+ "Legal Entity" shall mean the union of the acting entity and all
42
+ other entities that control, are controlled by, or are under common
43
+ control with that entity. For the purposes of this definition,
44
+ "control" means (i) the power, direct or indirect, to cause the
45
+ direction or management of such entity, whether by contract or
46
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
47
+ outstanding shares, or (iii) beneficial ownership of such entity.
48
+
49
+ "You" (or "Your") shall mean an individual or Legal Entity
50
+ exercising permissions granted by this License.
51
+
52
+ "Source" form shall mean the preferred form for making modifications,
53
+ including but not limited to software source code, documentation
54
+ source, and configuration files.
55
+
56
+ "Object" form shall mean any form resulting from mechanical
57
+ transformation or translation of a Source form, including but
58
+ not limited to compiled object code, generated documentation,
59
+ and conversions to other media types.
60
+
61
+ "Work" shall mean the work of authorship, whether in Source or
62
+ Object form, made available under the License, as indicated by a
63
+ copyright notice that is included in or attached to the work
64
+ (an example is provided in the Appendix below).
65
+
66
+ "Derivative Works" shall mean any work, whether in Source or Object
67
+ form, that is based on (or derived from) the Work and for which the
68
+ editorial revisions, annotations, elaborations, or other modifications
69
+ represent, as a whole, an original work of authorship. For the purposes
70
+ of this License, Derivative Works shall not include works that remain
71
+ separable from, or merely link (or bind by name) to the interfaces of,
72
+ the Work and Derivative Works thereof.
73
+
74
+ "Contribution" shall mean any work of authorship, including
75
+ the original version of the Work and any modifications or additions
76
+ to that Work or Derivative Works thereof, that is intentionally
77
+ submitted to Licensor for inclusion in the Work by the copyright owner
78
+ or by an individual or Legal Entity authorized to submit on behalf of
79
+ the copyright owner. For the purposes of this definition, "submitted"
80
+ means any form of electronic, verbal, or written communication sent
81
+ to the Licensor or its representatives, including but not limited to
82
+ communication on electronic mailing lists, source code control systems,
83
+ and issue tracking systems that are managed by, or on behalf of, the
84
+ Licensor for the purpose of discussing and improving the Work, but
85
+ excluding communication that is conspicuously marked or otherwise
86
+ designated in writing by the copyright owner as "Not a Contribution."
87
+
88
+ "Contributor" shall mean Licensor and any individual or Legal Entity
89
+ on behalf of whom a Contribution has been received by Licensor and
90
+ subsequently incorporated within the Work.
91
+
92
+ 2. Grant of Copyright License. Subject to the terms and conditions of
93
+ this License, each Contributor hereby grants to You a perpetual,
94
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
95
+ copyright license to reproduce, prepare Derivative Works of,
96
+ publicly display, publicly perform, sublicense, and distribute the
97
+ Work and such Derivative Works in Source or Object form.
98
+
99
+ 3. Grant of Patent License. Subject to the terms and conditions of
100
+ this License, each Contributor hereby grants to You a perpetual,
101
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
102
+ (except as stated in this section) patent license to make, have made,
103
+ use, offer to sell, sell, import, and otherwise transfer the Work,
104
+ where such license applies only to those patent claims licensable
105
+ by such Contributor that are necessarily infringed by their
106
+ Contribution(s) alone or by combination of their Contribution(s)
107
+ with the Work to which such Contribution(s) was submitted. If You
108
+ institute patent litigation against any entity (including a
109
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
110
+ or a Contribution incorporated within the Work constitutes direct
111
+ or contributory patent infringement, then any patent licenses
112
+ granted to You under this License for that Work shall terminate
113
+ as of the date such litigation is filed.
114
+
115
+ 4. Redistribution. You may reproduce and distribute copies of the
116
+ Work or Derivative Works thereof in any medium, with or without
117
+ modifications, and in Source or Object form, provided that You
118
+ meet the following conditions:
119
+
120
+ (a) You must give any other recipients of the Work or
121
+ Derivative Works a copy of this License; and
122
+
123
+ (b) You must cause any modified files to carry prominent notices
124
+ stating that You changed the files; and
125
+
126
+ (c) You must retain, in the Source form of any Derivative Works
127
+ that You distribute, all copyright, patent, trademark, and
128
+ attribution notices from the Source form of the Work,
129
+ excluding those notices that do not pertain to any part of
130
+ the Derivative Works; and
131
+
132
+ (d) If the Work includes a "NOTICE" text file as part of its
133
+ distribution, then any Derivative Works that You distribute must
134
+ include a readable copy of the attribution notices contained
135
+ within such NOTICE file, excluding those notices that do not
136
+ pertain to any part of the Derivative Works, in at least one
137
+ of the following places: within a NOTICE text file distributed
138
+ as part of the Derivative Works; within the Source form or
139
+ documentation, if provided along with the Derivative Works; or,
140
+ within a display generated by the Derivative Works, if and
141
+ wherever such third-party notices normally appear. The contents
142
+ of the NOTICE file are for informational purposes only and
143
+ do not modify the License. You may add Your own attribution
144
+ notices within Derivative Works that You distribute, alongside
145
+ or as an addendum to the NOTICE text from the Work, provided
146
+ that such additional attribution notices cannot be construed
147
+ as modifying the License.
148
+
149
+ You may add Your own copyright statement to Your modifications and
150
+ may provide additional or different license terms and conditions
151
+ for use, reproduction, or distribution of Your modifications, or
152
+ for any such Derivative Works as a whole, provided Your use,
153
+ reproduction, and distribution of the Work otherwise complies with
154
+ the conditions stated in this License.
155
+
156
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
157
+ any Contribution intentionally submitted for inclusion in the Work
158
+ by You to the Licensor shall be under the terms and conditions of
159
+ this License, without any additional terms or conditions.
160
+ Notwithstanding the above, nothing herein shall supersede or modify
161
+ the terms of any separate license agreement you may have executed
162
+ with Licensor regarding such Contributions.
163
+
164
+ 6. Trademarks. This License does not grant permission to use the trade
165
+ names, trademarks, service marks, or product names of the Licensor,
166
+ except as required for reasonable and customary use in describing the
167
+ origin of the Work and reproducing the content of the NOTICE file.
168
+
169
+ 7. Disclaimer of Warranty. Unless required by applicable law or
170
+ agreed to in writing, Licensor provides the Work (and each
171
+ Contributor provides its Contributions) on an "AS IS" BASIS,
172
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
173
+ implied, including, without limitation, any warranties or conditions
174
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
175
+ PARTICULAR PURPOSE. You are solely responsible for determining the
176
+ appropriateness of using or redistributing the Work and assume any
177
+ risks associated with Your exercise of permissions under this License.
178
+
179
+ 8. Limitation of Liability. In no event and under no legal theory,
180
+ whether in tort (including negligence), contract, or otherwise,
181
+ unless required by applicable law (such as deliberate and grossly
182
+ negligent acts) or agreed to in writing, shall any Contributor be
183
+ liable to You for damages, including any direct, indirect, special,
184
+ incidental, or consequential damages of any character arising as a
185
+ result of this License or out of the use or inability to use the
186
+ Work (including but not limited to damages for loss of goodwill,
187
+ work stoppage, computer failure or malfunction, or any and all
188
+ other commercial damages or losses), even if such Contributor
189
+ has been advised of the possibility of such damages.
190
+
191
+ 9. Accepting Warranty or Additional Liability. While redistributing
192
+ the Work or Derivative Works thereof, You may choose to offer,
193
+ and charge a fee for, acceptance of support, warranty, indemnity,
194
+ or other liability obligations and/or rights consistent with this
195
+ License. However, in accepting such obligations, You may act only
196
+ on Your own behalf and on Your sole responsibility, not on behalf
197
+ of any other Contributor, and only if You agree to indemnify,
198
+ defend, and hold each Contributor harmless for any liability
199
+ incurred by, or claims asserted against, such Contributor by reason
200
+ of your accepting any such warranty or additional liability.
201
+
@@ -0,0 +1,13 @@
1
+ Copyright from 2019 Paulo Arruda (parrudaj at gmail dot com)
2
+
3
+ Licensed under the Commons Clause + Apache License, Version 2.0
4
+ (the "License"); you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
@@ -0,0 +1,31 @@
1
+ # Gotta Run!
2
+
3
+ TODO: Delete this and the text above, and describe your gem
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'gotta-run'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install gotta-run
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ Bug reports and pull requests are welcome on GitHub at https://github.com/gotta-dev/gotta-run. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
28
+
29
+ ## Code of Conduct
30
+
31
+ Everyone interacting in the Gotta::Run project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/gotta-dev/gotta-run/blob/master/CODE_OF_CONDUCT.md).
@@ -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,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'gotta/run'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ 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,28 @@
1
+ #!/bin/bash
2
+ set -e
3
+ ! which rvm && echo "Sorry, but this script only works with RVM" && exit 1
4
+ [[ -z $GEM_HOME ]] && echo "You need to have \$GEM_HOME set before continuing." && exit 1
5
+ GEMS=$GEM_HOME/gems
6
+ echo Gems detected at: $GEMS
7
+ #which gotta-run &&
8
+ echo "Removing all installed versions" && gem uninstall -a -I -x gotta-run
9
+ echo "Cleaning up stale links"
10
+ rm -rf $GEMS/gotta-run-0*
11
+ echo "Building gem from source"
12
+ VERSION=$(gem build gotta-run.gemspec |grep Version|cut -f4 -d" ")
13
+ echo "Installing new gem"
14
+ gem install gotta-run-$VERSION.gem
15
+ SOURCECODE=$PWD
16
+ INSTALL_DIR=$GEMS/gotta-run-$VERSION
17
+ echo -e "\n\nThe directory below is about to be removed. Press ENTER to continue or CTRL+C to abort."
18
+ echo $INSTALL_DIR
19
+ read
20
+ rm -rf $INSTALL_DIR
21
+ echo "Creating symlink $INSTALL_DIR -> $SOURCECODE"
22
+ ln -s $SOURCECODE $INSTALL_DIR
23
+ echo "Running 'gotta-run -v'"
24
+ touch ".test"
25
+ [[ -e $INSTALL_DIR/.test ]] && rm .test && echo Done && exit 0
26
+ rm -f .test
27
+ echo "ERROR: The symbolik link $INSTALL_DIR is not working."
28
+ exit 1
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'gotta/run/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'gotta-run'
9
+ spec.version = Gotta::Run::VERSION
10
+ spec.authors = ['Paulo Arruda']
11
+ spec.email = ['parrudaj@gmail.com']
12
+
13
+ spec.summary = 'Gotta Run'
14
+ spec.description = 'Gotta Run'
15
+ spec.homepage = 'https://github.com/gotta-dev'
16
+ spec.license = 'Apache-2.0'
17
+
18
+ spec.add_runtime_dependency 'gotta-common', '~> 0.1'
19
+ spec.add_runtime_dependency 'gotta-queue', '~> 0.1'
20
+ spec.add_runtime_dependency 'gotta-queue-memory', '~> 0.1'
21
+ spec.add_runtime_dependency 'msgpack', '~> 1.2'
22
+
23
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
24
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
25
+ if spec.respond_to?(:metadata)
26
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
27
+
28
+ spec.metadata['homepage_uri'] = spec.homepage
29
+ # spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
30
+ # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
31
+ else
32
+ raise 'RubyGems 2.0 or newer is required to protect against ' \
33
+ 'public gem pushes.'
34
+ end
35
+
36
+ # Specify which files should be added to the gem when it is released.
37
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
38
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
39
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
40
+ end
41
+ spec.bindir = 'exe'
42
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
43
+ spec.require_paths = ['lib']
44
+
45
+ spec.add_development_dependency 'bundler', '~> 2.0'
46
+ spec.add_development_dependency 'rake', '~> 10.0'
47
+ spec.add_development_dependency 'rspec', '~> 3.0'
48
+ end
@@ -0,0 +1,139 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright from 2019 Paulo Arruda (parrudaj at gmail dot com)
4
+ #
5
+ # Licensed under the Commons Clause + Apache License, Version 2.0
6
+ # (the "License"); you may not use this file except in compliance with
7
+ # the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+
18
+ require 'msgpack'
19
+ require 'socket'
20
+
21
+ module Gotta
22
+ module Run
23
+ require 'gotta/run/version'
24
+ require 'gotta/run/errors'
25
+ require 'gotta/run/response'
26
+ require 'gotta/run/dynamic_runner'
27
+
28
+ # This class is responsible for all dynamic runners.
29
+ # It was designed to be used as a inheritable class.
30
+ # For example:
31
+ # `class RubyRunner < Gotta::Run::DynamicRunner; end`
32
+ class DynamicRunner
33
+ attr_reader :input_queue
34
+
35
+ # The initializer takes an input queue, where it should
36
+ # go fetch payloads to send to runners. The default queue
37
+ # is `Gotta::Queue::Memory::Driver`.
38
+ # It also takes an optional argument `runtime_path` that
39
+ # is the path to the runtime script for the language you
40
+ # want to run.
41
+ # You can optionally pass a block in case you need to
42
+ # set extra configuration without having to override the
43
+ # initializer.
44
+ # It will raise `RuntimeScriptNotFound` if `runtime_path`
45
+ # doesn't exist or is not a file.
46
+ def initialize(input_queue: Gotta::Queue.new('input'),
47
+ runtime_path: nil,
48
+ &block)
49
+ @input_queue = input_queue
50
+ @runtime_path = runtime_path
51
+ raise RuntimeScriptNotFound, path unless File.file?(runtime_path)
52
+
53
+ @parent_socket, @child_socket = UNIXSocket.pair
54
+ @child_output_r, @child_output_w = IO.pair
55
+ instance_eval(&block) if block_given?
56
+ end
57
+
58
+ def start
59
+ start_runtime
60
+ @child_stdout_thread = connect_to_child_stdout
61
+ start_processing
62
+ end
63
+
64
+ private
65
+
66
+ def connect_to_child_stdout
67
+ Thread.new do
68
+ loop do
69
+ puts @child_output_r.gets
70
+ end
71
+ end
72
+ end
73
+
74
+ # Sets the environment variables for the runtime process
75
+ def env
76
+ ENV.to_h.merge(
77
+ 'SOCKET_FD' => @child_socket.fileno
78
+ )
79
+ end
80
+
81
+ # Start the runtime from `path`.
82
+ # It runs in a loop to make sure the runner
83
+ # process gets restarted in case it dies.
84
+ def start_runtime(path)
85
+ cmd = [ruby,
86
+ '-E',
87
+ 'UTF-8:UTF-8',
88
+ path]
89
+ loop do
90
+ pid = spawn_process(cmd)
91
+ Process.wait(pid)
92
+ sleep 1
93
+ end
94
+ end
95
+
96
+ def spawn_process(cmd)
97
+ Process.spawn(
98
+ env,
99
+ cmd,
100
+ close_others: false,
101
+ out: @child_output_w,
102
+ err: %i[child out]
103
+ )
104
+ end
105
+
106
+ # This method is an infinite loop that pops
107
+ # the last element from the queue and passes
108
+ # the payload to the child process.
109
+ # Then while it waits for the child process to
110
+ # perform its task, this method will setup the
111
+ # response queue.
112
+ def start_processing
113
+ input_queue.process do |payload|
114
+ @parent_socket.send(payload, 0)
115
+ send_response_to(response_queue_for(payload))
116
+ end
117
+ end
118
+
119
+ # Sends the response to the response queue.
120
+ # A timeout is used here, but ideally this should be
121
+ # done using socket timeouts.
122
+ # TODO: use socket timeouts.
123
+ def send_response_to(response_queue)
124
+ Timeout.timeout(MAX_EXECUTION_TIME) do
125
+ response_queue.push(parent_socket.recv(MAX_RESPONSE_MSG_SIZE))
126
+ end
127
+ rescue Timeout::Error
128
+ response_queue.push(Response.timeout.to_msgpack)
129
+ end
130
+
131
+ # Initializes the response queue from the name
132
+ # that comes with the payload.
133
+ def response_queue_for(payload)
134
+ object = MessagePack.unpack(payload)
135
+ Gotta::Queue.new(object['response_queue_name'])
136
+ end
137
+ end
138
+ end
139
+ end
@@ -0,0 +1,139 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright from 2019 Paulo Arruda (parrudaj at gmail dot com)
4
+ #
5
+ # Licensed under the Commons Clause + Apache License, Version 2.0
6
+ # (the "License"); you may not use this file except in compliance with
7
+ # the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+
18
+ require 'msgpack'
19
+ require 'socket'
20
+ require 'open3'
21
+ require 'gotta/common/constants'
22
+ require 'gotta/run/response'
23
+ require 'gotta/run/errors'
24
+
25
+ module Gotta
26
+ module Run
27
+ # This class is responsible for all dynamic runners.
28
+ # It was designed to be used as a inheritable class.
29
+ # For example:
30
+ # `class RubyRunner < Gotta::Run::DynamicRunner; end`
31
+ class DynamicRunner
32
+ attr_reader :input_queue
33
+
34
+ # The initializer takes an input queue, where it should
35
+ # go fetch payloads to send to runners. The default queue
36
+ # is `Gotta::Queue::Memory::Driver`.
37
+ # It also takes an optional argument `runtime_path` that
38
+ # is the path to the runtime script for the language you
39
+ # want to run.
40
+ # You can optionally pass a block in case you need to
41
+ # set extra configuration without having to override the
42
+ # initializer.
43
+ # It will raise `RuntimeScriptNotFound` if `runtime_path`
44
+ # doesn't exist or is not a file.
45
+ def initialize(input_queue: Gotta::Queue.new('input'),
46
+ runtime_path: nil,
47
+ &block)
48
+ @input_queue = input_queue
49
+ @runtime_path = runtime_path
50
+ raise RuntimeScriptNotFound, runtime_path unless File.file?(runtime_path)
51
+
52
+ @parent_socket, @child_socket = UNIXSocket.pair
53
+ @child_output_r, @child_output_w = IO.pipe
54
+ instance_eval(&block) if block_given?
55
+ end
56
+
57
+ def start
58
+ start_runtime
59
+ start_processing
60
+ end
61
+
62
+ private
63
+
64
+ # Sets the environment variables for the runtime process
65
+ def env
66
+ ENV.to_h.merge(
67
+ 'SOCKET_FD' => @child_socket.fileno.to_s,
68
+ 'MAX_EXECUTION_TIME' => MAX_EXECUTION_TIME.to_s
69
+ )
70
+ end
71
+
72
+ def cmd
73
+ cmd = [
74
+ 'ruby',
75
+ '-E',
76
+ 'UTF-8:UTF-8',
77
+ @runtime_path
78
+ ].join(' ')
79
+ end
80
+
81
+ # Start the runtime from `path`.
82
+ # It runs in a loop to make sure the runner
83
+ # process gets restarted in case it dies.
84
+ def start_runtime
85
+ Thread.new do
86
+ loop do
87
+ spawn_process(cmd)
88
+ sleep 1
89
+ end
90
+ end
91
+ end
92
+
93
+ def spawn_process(cmd)
94
+ Open3.popen2(env, cmd, @child_socket.fileno => @child_socket.fileno) do |_stdin, stdout, _status_thread|
95
+ stdout.each_line do |line|
96
+ puts line
97
+ end
98
+ end
99
+ end
100
+
101
+ # This method is an infinite loop that pops
102
+ # the last element from the queue and passes
103
+ # the payload to the child process.
104
+ # Then while it waits for the child process to
105
+ # perform its task, this method will setup the
106
+ # response queue.
107
+ def start_processing
108
+ input_queue.process do |payload|
109
+ @parent_socket.send(payload, 0)
110
+ send_response_to(response_queue_for(payload))
111
+ end
112
+ end
113
+
114
+ # Sends the response to the response queue.
115
+ # A timeout is used here, but ideally this should be
116
+ # done using socket timeouts.
117
+ # TODO: use socket timeouts instead.
118
+ def send_response_to(response_queue)
119
+ Timeout.timeout(MAX_EXECUTION_TIME) do
120
+ # puts 'Waiting for response from child'
121
+ resp = @parent_socket.recv(MAX_RESPONSE_MSG_SIZE)
122
+ # puts "Got response! #{MessagePack.unpack(resp)}"
123
+ response_queue.push(
124
+ resp
125
+ )
126
+ end
127
+ rescue Timeout::Error
128
+ response_queue.push(Response.timeout.to_msgpack)
129
+ end
130
+
131
+ # Initializes the response queue from the name
132
+ # that comes with the payload.
133
+ def response_queue_for(payload)
134
+ object = MessagePack.unpack(payload)
135
+ Gotta::Queue.new(object['response_queue_name'])
136
+ end
137
+ end
138
+ end
139
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright from 2019 Paulo Arruda (parrudaj at gmail dot com)
4
+ #
5
+ # Licensed under the Commons Clause + Apache License, Version 2.0
6
+ # (the "License"); you may not use this file except in compliance with
7
+ # the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+
18
+ module Gotta
19
+ module Run
20
+ module Error
21
+ # This error is raised when a runtime script
22
+ # cannot be found in a given path.
23
+ class RuntimeScriptNotFound < StandardError
24
+ def initialize(path)
25
+ msg = "Could not find runtime script '#{path}'."
26
+ super msg
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright from 2019 Paulo Arruda (parrudaj at gmail dot com)
4
+ #
5
+ # Licensed under the Commons Clause + Apache License, Version 2.0
6
+ # (the "License"); you may not use this file except in compliance with
7
+ # the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+
18
+ require 'gotta/common/response'
19
+
20
+ module Gotta
21
+ module Run
22
+ class Response < Gotta::Common::Response
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright from 2019 Paulo Arruda (parrudaj at gmail dot com)
4
+ #
5
+ # Licensed under the Commons Clause + Apache License, Version 2.0
6
+ # (the "License"); you may not use this file except in compliance with
7
+ # the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+
18
+ module Gotta
19
+ module Run
20
+ VERSION = '0.1.0'
21
+ end
22
+ end
metadata ADDED
@@ -0,0 +1,162 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gotta-run
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Paulo Arruda
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-06-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: gotta-common
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: gotta-queue
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: gotta-queue-memory
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.1'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.1'
55
+ - !ruby/object:Gem::Dependency
56
+ name: msgpack
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.2'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.2'
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '2.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '2.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '10.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '10.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '3.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '3.0'
111
+ description: Gotta Run
112
+ email:
113
+ - parrudaj@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - ".rubocop.yml"
121
+ - ".travis.yml"
122
+ - CODE_OF_CONDUCT.md
123
+ - Gemfile
124
+ - LICENSE.txt
125
+ - NOTICE.txt
126
+ - README.md
127
+ - Rakefile
128
+ - bin/console
129
+ - bin/setup
130
+ - dev-setup.sh
131
+ - gotta-run.gemspec
132
+ - lib/gotta/run.rb
133
+ - lib/gotta/run/dynamic_runner.rb
134
+ - lib/gotta/run/errors.rb
135
+ - lib/gotta/run/response.rb
136
+ - lib/gotta/run/version.rb
137
+ homepage: https://github.com/gotta-dev
138
+ licenses:
139
+ - Apache-2.0
140
+ metadata:
141
+ homepage_uri: https://github.com/gotta-dev
142
+ post_install_message:
143
+ rdoc_options: []
144
+ require_paths:
145
+ - lib
146
+ required_ruby_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ required_rubygems_version: !ruby/object:Gem::Requirement
152
+ requirements:
153
+ - - ">="
154
+ - !ruby/object:Gem::Version
155
+ version: '0'
156
+ requirements: []
157
+ rubyforge_project:
158
+ rubygems_version: 2.7.8
159
+ signing_key:
160
+ specification_version: 4
161
+ summary: Gotta Run
162
+ test_files: []