carrot_rpc 0.2.3.pre
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +50 -0
- data/.rspec +2 -0
- data/.rubocop.yml +35 -0
- data/.travis.yml +3 -0
- data/CHANGELOG.md +149 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +9 -0
- data/LICENSE +22 -0
- data/LICENSE.txt +21 -0
- data/README.md +172 -0
- data/Rakefile +7 -0
- data/bin/carrot_rpc +19 -0
- data/bin/console +20 -0
- data/bin/setup +7 -0
- data/carrot_rpc.gemspec +49 -0
- data/circle.yml +8 -0
- data/lib/carrot_rpc/cli.rb +110 -0
- data/lib/carrot_rpc/client_server.rb +25 -0
- data/lib/carrot_rpc/configuration.rb +17 -0
- data/lib/carrot_rpc/error/code.rb +17 -0
- data/lib/carrot_rpc/error.rb +43 -0
- data/lib/carrot_rpc/hash_extensions.rb +22 -0
- data/lib/carrot_rpc/rpc_client.rb +117 -0
- data/lib/carrot_rpc/rpc_server/jsonapi_resources/actions.rb +108 -0
- data/lib/carrot_rpc/rpc_server/jsonapi_resources.rb +172 -0
- data/lib/carrot_rpc/rpc_server.rb +77 -0
- data/lib/carrot_rpc/server_runner/autoload_rails.rb +41 -0
- data/lib/carrot_rpc/server_runner/logger.rb +31 -0
- data/lib/carrot_rpc/server_runner/pid.rb +142 -0
- data/lib/carrot_rpc/server_runner/signals.rb +21 -0
- data/lib/carrot_rpc/server_runner.rb +156 -0
- data/lib/carrot_rpc/tagged_log.rb +24 -0
- data/lib/carrot_rpc/version.rb +3 -0
- data/lib/carrot_rpc.rb +46 -0
- data/logs/.gitkeep +0 -0
- metadata +182 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 8add259a5bc250a315ce9feefe6b37cf8e920d44
|
4
|
+
data.tar.gz: 94a6f21f8b5cca40e4fd040564756c4d6b173bcb
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ffae0ddaf0342282814713c47487203008239979fb14c009f21c12a436dcc23fbc6c9395dcc0ad52dd034d5997f72d716f20f04ac5e8cc8a85b08e0ef85c0c0c
|
7
|
+
data.tar.gz: a08ecc6881122adca9f7f34d77d172f5b20ef62b3590e8a5073be41dee8fc66e1678b3194cb90b9abc2335c849afa0bf7d1cf5e870373a105e62be5f94ccc6fb
|
data/.gitignore
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
/.config
|
4
|
+
/coverage/
|
5
|
+
/InstalledFiles
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/test/tmp/
|
9
|
+
/test/version_tmp/
|
10
|
+
/tmp/
|
11
|
+
|
12
|
+
## Specific to RubyMotion:
|
13
|
+
.dat*
|
14
|
+
.repl_history
|
15
|
+
build/
|
16
|
+
|
17
|
+
## Documentation cache and generated files:
|
18
|
+
/.yardoc/
|
19
|
+
/_yardoc/
|
20
|
+
/doc/
|
21
|
+
/rdoc/
|
22
|
+
/coverage/
|
23
|
+
|
24
|
+
## Environment normalisation:
|
25
|
+
/.bundle/
|
26
|
+
/vendor/bundle
|
27
|
+
/lib/bundler/man/
|
28
|
+
|
29
|
+
# for a library or gem, you might want to ignore these files since the code is
|
30
|
+
# intended to run in multiple environments; otherwise, check them in:
|
31
|
+
Gemfile.lock
|
32
|
+
.ruby-version
|
33
|
+
.ruby-gemset
|
34
|
+
|
35
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
36
|
+
.rvmrc
|
37
|
+
|
38
|
+
# Editor Artifacts
|
39
|
+
*.swo
|
40
|
+
*.swp
|
41
|
+
.idea
|
42
|
+
|
43
|
+
# Ignore Mac Dir files
|
44
|
+
.DS_Store
|
45
|
+
*./DS_Store
|
46
|
+
|
47
|
+
*.log
|
48
|
+
|
49
|
+
# RSpec failure tracker for --only-failures and --next-failure
|
50
|
+
spec/examples.txt
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
AllCops:
|
2
|
+
DisplayCopNames: true
|
3
|
+
TargetRubyVersion: 2.2
|
4
|
+
|
5
|
+
Metrics/LineLength:
|
6
|
+
Max: 120
|
7
|
+
|
8
|
+
Style/BlockDelimiters:
|
9
|
+
EnforcedStyle: semantic
|
10
|
+
Style/ClassAndModuleChildren:
|
11
|
+
Description: >-
|
12
|
+
Compact style ensures that the parent classes and modules are not reopened for each child class and
|
13
|
+
module. Reopening the parent class or module can lead to the parent class or module not being loaded
|
14
|
+
as the child already defined it.
|
15
|
+
EnforcedStyle: compact
|
16
|
+
Style/GuardClause:
|
17
|
+
Description: >-
|
18
|
+
Modifiers prevent placing break points on just the condition or just the body. Similarly, coverage of
|
19
|
+
the line cannot tell you if the body is ever executed.
|
20
|
+
Enabled: false
|
21
|
+
Style/IfUnlessModifier:
|
22
|
+
Description: >-
|
23
|
+
Modifiers prevent placing break points on just the condition or just the body. Similarly, coverage of
|
24
|
+
the line cannot tell you if the body is ever executed.
|
25
|
+
Enabled: false
|
26
|
+
Style/StringLiterals:
|
27
|
+
Description: >-
|
28
|
+
Double quotes work for Strings in both Ruby and Elixir, so developers switch languages can use
|
29
|
+
consistent quoting. (Single quotes are char lists in Elixir.)
|
30
|
+
EnforcedStyle: double_quotes
|
31
|
+
Style/WhileUntilModifier:
|
32
|
+
Description: >-
|
33
|
+
Modifiers prevent placing break points on just the condition or just the body. Similarly, coverage of
|
34
|
+
the line cannot tell you if the body is ever executed.
|
35
|
+
Enabled: false
|
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,149 @@
|
|
1
|
+
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
|
2
|
+
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
|
3
|
+
**Table of Contents** *generated with [DocToc](https://github.com/thlorenz/doctoc)*
|
4
|
+
|
5
|
+
- [Changelog](#changelog)
|
6
|
+
- [v0.2.1](#v021)
|
7
|
+
- [Bug Fixes](#bug-fixes)
|
8
|
+
- [v0.2.0](#v020)
|
9
|
+
- [Enhancements](#enhancements)
|
10
|
+
- [Bug Fixes](#bug-fixes-1)
|
11
|
+
- [Incompatible Changes](#incompatible-changes)
|
12
|
+
- [v0.1.2](#v012)
|
13
|
+
- [Enhancements](#enhancements-1)
|
14
|
+
- [Bug Fixes](#bug-fixes-2)
|
15
|
+
- [v0.1.1](#v011)
|
16
|
+
- [Enhancements](#enhancements-2)
|
17
|
+
- [Bug Fixes](#bug-fixes-3)
|
18
|
+
- [Incompatible Changes](#incompatible-changes-1)
|
19
|
+
|
20
|
+
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
|
21
|
+
|
22
|
+
# Changelog
|
23
|
+
All significant changes in the project are documented here.
|
24
|
+
|
25
|
+
## v0.2.1
|
26
|
+
|
27
|
+
### Bug Fixes
|
28
|
+
* [#6](https://github.com/C-S-D/carrot_rpc/pull/6) - [shamil614](https://github.com/shamil614)
|
29
|
+
* Error class not loaded in RpcServer
|
30
|
+
* RpcServer should not rename json keys
|
31
|
+
* RpcClient dasherizes keys before serializing hash to json. Better conformity to json property naming conventions.
|
32
|
+
* RpcClient underscores keys after receiving response from server. Better conformity to ruby naming conventions.
|
33
|
+
* [#7](https://github.com/C-S-D/carrot_rpc/pull/7) - [shamil614](https://github.com/shamil614)
|
34
|
+
* Make sure hash keys are strings before renaming
|
35
|
+
|
36
|
+
|
37
|
+
## v0.2.0
|
38
|
+
|
39
|
+
### Enhancements
|
40
|
+
* [#5](https://github.com/C-S-D/carrot_rpc/pull/5) - [KronicDeth](http://github.com/KronicDeth)
|
41
|
+
* Gems ordered and documented in gemspec and `Gemfile`
|
42
|
+
* Temorpary (`#`) files removed from git
|
43
|
+
* Rubocop is enabled and used on CircleCI
|
44
|
+
* Unused variables are prefixed with `_`
|
45
|
+
* `fail` is used instead of `raise` when first raising an exception
|
46
|
+
* Remove usage of deprecated methods
|
47
|
+
* Print error if `byebug` can't be loaded instead of failing silently, but CLI still starts
|
48
|
+
* Stop shadowing outer local variables in blocks
|
49
|
+
* Remove unused assignments
|
50
|
+
* Set and enforce max line length to 120
|
51
|
+
* Use `find` instead of `select {}.first` for better performance
|
52
|
+
* `queue_name` will retrieve the current queue name while `queue_name(new_name)` will set it.
|
53
|
+
* Align hashes
|
54
|
+
* Align parameters
|
55
|
+
* Favor symbolic `&&` over `and`. (They have different precedence too)
|
56
|
+
* Remove block comments
|
57
|
+
* Assign to variable outside conditionals instead of on each branch
|
58
|
+
* Remove extra empty lines
|
59
|
+
* Don't favor guard clauses as they prevent break pointing the body and guard separately and obscure bodies that don't have code coverage.
|
60
|
+
* Remove extra spacing
|
61
|
+
* Correct indentation
|
62
|
+
* Freeze `CarrotRpc::VERSION` so it is immutable
|
63
|
+
* Use `until` instead of negated `while`
|
64
|
+
* Use `_` to separate digits in large numerals
|
65
|
+
* Use `( )` for sigils
|
66
|
+
* Remove redundant `self.` for method calls
|
67
|
+
* Use `%r{}` instead of `//` for regexps
|
68
|
+
* Use newlines instead of `;`
|
69
|
+
* Add spacing around blocks and braces
|
70
|
+
* Enforce double quotes for all strings as double quotes work for strings in both Ruby and Elixir. (Single quotes are for Char Lists in Elixir)
|
71
|
+
* Use `&:<method>` instead of calling a non-args method in blocks
|
72
|
+
* Use `attr_reader` instead of trivial accessor methods
|
73
|
+
* Remove unneed interpolation
|
74
|
+
* Use double quotes instead of `%q`
|
75
|
+
* Use `%w` for word arrays
|
76
|
+
* Extract methods to lower to AbcSize metric and Method Length
|
77
|
+
* Extract classes and modules to lower Class Length
|
78
|
+
* Use `const_get` and `constantize` instead of security risk `eval`
|
79
|
+
* Enable all RSpec 3 recommended options
|
80
|
+
* Fix order-dependency of specs.
|
81
|
+
* Use `autoload` to delay loading
|
82
|
+
* Use compact class and module children to prevent parent from being missed when loading.
|
83
|
+
* Add `rake spec`
|
84
|
+
* Add Luke Imhoff as an author
|
85
|
+
* Set gem home page to this repository
|
86
|
+
* Semantic block delimiters, so we always think about procedural vs functional blocks to make Elixir coding easier.
|
87
|
+
|
88
|
+
### Bug Fixes
|
89
|
+
* [#5](https://github.com/C-S-D/carrot_rpc/pull/5) - [KronicDeth](http://github.com/KronicDeth)
|
90
|
+
* `ClientServer::ClassMethods` has been moved under `CarrotRpc` namespace as `CarrotRpc::ClientServer`
|
91
|
+
* `HashExtensions` has been moved under `CarrotRpc` namespace as `CarrotRpc::HashExtensions`
|
92
|
+
|
93
|
+
### Incompatible Changes
|
94
|
+
* [#5](https://github.com/C-S-D/carrot_rpc/pull/5) - [KronicDeth](http://github.com/KronicDeth)
|
95
|
+
* `ClientServer::ClassMethods` renamed to `CarrotRpc::ClientServer`
|
96
|
+
* `HashExtensions` renamed to `CarrotRpc::HashExtensions`
|
97
|
+
* `ClientServer::ClassMethods#get_queue_name` renamed to `CarrotRpc::ClientServer#queue_name()` (no args is the reader, one argument is the writer)
|
98
|
+
|
99
|
+
## v0.1.2
|
100
|
+
|
101
|
+
### Enhancements
|
102
|
+
* [#4](https://github.com/C-S-D/carrot_rpc/pull1) - [shamil614](https://github.com/shamil614)
|
103
|
+
* Rename the keys in the parsed payload from '-' to '_'
|
104
|
+
* Added integration specs to test functionality
|
105
|
+
* Logging to test.log file
|
106
|
+
* Setup for circleci integration tests to rabbitmq
|
107
|
+
|
108
|
+
### Bug Fixes
|
109
|
+
* [#4](https://github.com/C-S-D/carrot_rpc/pull1) - [shamil614](https://github.com/shamil614)
|
110
|
+
* Some require statements not properly loading modules
|
111
|
+
* Consistent use of require vs require_relative
|
112
|
+
|
113
|
+
## v0.1.1
|
114
|
+
|
115
|
+
### Enhancements
|
116
|
+
* [#1](https://github.com/C-S-D/carrot_rpc/pull1) - [shamil614](https://github.com/shamil614)
|
117
|
+
* `CarrotRpc.configuration.bunny` can be set to custom
|
118
|
+
[`Bunny` instance](http://www.rubydoc.info/gems/bunny/Bunny#new-class_method).
|
119
|
+
* `CarrotRpc::RpcClient` and `CarrotRpc::RpcServer` subclasses can set their queue name with the `queue_name` class
|
120
|
+
method. (It can be retrieved with `get_queue_name`.
|
121
|
+
* `carrot_rpc`'s `--autoload_rails` boolean flag determines whether to load Rails environment. The Rails path is
|
122
|
+
assumed to the be the current working directory.
|
123
|
+
* If a `CarrotRpc::RpcServer` method invoked from a JSON RPC `:method` raises an `CarrotRpc::Error`, then that error
|
124
|
+
is converted to a JSON RPC error and sent back to the client.
|
125
|
+
|
126
|
+
### Bug Fixes
|
127
|
+
* [#1](https://github.com/C-S-D/carrot_rpc/pull/1) - [shamil614](https://github.com/shamil614)
|
128
|
+
* Send `jsonrpc` key instead of incorrect `json_rpc` key in JSON RPC response messages
|
129
|
+
* All files under `bin` are marked as gem executables instead of just `carrot_rpc`
|
130
|
+
* Fix files not loading properly when using `carrot_rpc`
|
131
|
+
* Fix bug in logger file setup
|
132
|
+
* The logger for each `CarrotRpc::RpcServer` is set before the server is started in
|
133
|
+
`CarrotRpc::ServerRunner#run_servers` to prevent a race condition where `#start` may try to use the logger.
|
134
|
+
|
135
|
+
### Incompatible Changes
|
136
|
+
* [#1](https://github.com/C-S-D/carrot_rpc/pull/1) - [shamil614](https://github.com/shamil614)
|
137
|
+
* `CarrotRpc.configuration.bunny` **MUST** be set to a
|
138
|
+
[`Bunny` instance](http://www.rubydoc.info/gems/bunny/Bunny#new-class_method), usually using `Bunny.new`.
|
139
|
+
* `CarrotRpc::RpcClient` and `CarrotRpc::RpcServer` subclasses **MUST** set their queue name with the `queue_name`
|
140
|
+
class method.
|
141
|
+
* `:channel` keyword argument is no longer accepted in `CarrotRpc::RpcClient.new`. The channel had already been
|
142
|
+
created from the `config.bunny.create_channel`, so the keyword argument was unused.
|
143
|
+
* `CarrotRpc::RpcClient#logger` is now read-only and is set from `config.logger`.
|
144
|
+
* `CarrotRpc::RpcServer#logger` is now read-only and is set from `config.logger`.
|
145
|
+
* `CarrotRpc.configuration.logger` is set to the `CarrotRpc::ServerServer#logger`.
|
146
|
+
* `carrot_rpc`'s `--rails_path PATH` flag has been replaced with `--autoload_rails` boolean flag that automatically
|
147
|
+
assumes the Rails path is the current working directory.
|
148
|
+
* `CarrotRpc.connfiguration.rails_path` no longer exists. The Rails path is assumed to be the current working
|
149
|
+
directory.
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Communication Service For The Deaf
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
22
|
+
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Scott Hamilton
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,172 @@
|
|
1
|
+
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
|
2
|
+
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
|
3
|
+
**Table of Contents** *generated with [DocToc](https://github.com/thlorenz/doctoc)*
|
4
|
+
|
5
|
+
- [CarrotRpc](#carrotrpc)
|
6
|
+
- [Installation](#installation)
|
7
|
+
- [Configuration](#configuration)
|
8
|
+
- [Server](#server)
|
9
|
+
- [Client](#client)
|
10
|
+
- [Usage](#usage)
|
11
|
+
- [Writing Servers](#writing-servers)
|
12
|
+
- [Writing Clients](#writing-clients)
|
13
|
+
- [Development](#development)
|
14
|
+
- [Contributing](#contributing)
|
15
|
+
|
16
|
+
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
|
17
|
+
|
18
|
+
# CarrotRpc
|
19
|
+
|
20
|
+
An opinionated approach to doing Remote Procedure Call (RPC) with RabbitMQ and the bunny gem. CarrotRpc serves as a way to streamline the RPC workflow so developers can focus on the implementation and not the plumbing when working with RabbitMQ.
|
21
|
+
|
22
|
+
[![Circle CI](https://circleci.com/gh/C-S-D/carrot_rpc.svg?style=svg)](https://circleci.com/gh/C-S-D/carrot_rpc)
|
23
|
+
|
24
|
+
## Installation
|
25
|
+
|
26
|
+
Add this line to your application's Gemfile:
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
gem 'carrot_rpc'
|
30
|
+
```
|
31
|
+
|
32
|
+
And then execute:
|
33
|
+
|
34
|
+
$ bundle
|
35
|
+
|
36
|
+
Or install it yourself as:
|
37
|
+
|
38
|
+
$ gem install carrot_rpc
|
39
|
+
|
40
|
+
## Configuration
|
41
|
+
There's two modes for CarrotRpc: server and client. The server is run via command line, and the client is run in your ruby application during the request / response lifecycle (like your Rails Controller).
|
42
|
+
### Server
|
43
|
+
The server is configured via command line and run in it's own process.
|
44
|
+
|
45
|
+
Carrot is easy to run via command line:
|
46
|
+
```bash
|
47
|
+
carrot_rpc
|
48
|
+
```
|
49
|
+
By typing in `carrot_rpc -h` you will see all the command line options:
|
50
|
+
```bash
|
51
|
+
Usage: server [options]
|
52
|
+
|
53
|
+
Process options:
|
54
|
+
-d, --daemonize run daemonized in the background (default: false)
|
55
|
+
--pidfile PIDFILE the pid filename
|
56
|
+
-s, --runloop_sleep VALUE Configurable sleep time in the runloop
|
57
|
+
--autoload_rails VALUE loads rails env by default. Uses Rails Logger by default.
|
58
|
+
--logfile VALUE relative path and name for Log file. Overrides Rails logger.
|
59
|
+
--loglevel VALUE levels of loggin: DEBUG < INFO < WARN < ERROR < FATAL < UNKNOWN
|
60
|
+
--rabbitmq_url VALUE connection string to RabbitMQ 'amqp://user:pass@host:10000/vhost'
|
61
|
+
|
62
|
+
Ruby options:
|
63
|
+
-I, --include PATH an additional $LOAD_PATH
|
64
|
+
--debug set $DEBUG to true
|
65
|
+
--warn enable warnings
|
66
|
+
|
67
|
+
Common options:
|
68
|
+
-h, --help
|
69
|
+
-v, --version
|
70
|
+
```
|
71
|
+
|
72
|
+
|
73
|
+
### Client
|
74
|
+
Clients are configured by initializing `CarrotRpc::Configuration`. The most common way in Rails is to setup an initializer in `/config/initializers/carrot_rpc.rb`
|
75
|
+
|
76
|
+
```ruby
|
77
|
+
CarrotRpc.configure do |config|
|
78
|
+
# Required on the client to connect to RabbitMQ.
|
79
|
+
# Bunny defaults to connecting to ENV['RABBITMQ_URL']. See Bunny docs.
|
80
|
+
config.bunny = Bunny.new.start
|
81
|
+
# Set the log level. Ruby Logger Docs http://ruby-doc.org/stdlib-2.2.0/libdoc/logger/rdoc/Logger.html
|
82
|
+
config.loglevel = Logger::INFO
|
83
|
+
# Create a new logger or use the Rails logger.
|
84
|
+
# When using Rails, use a tagged log to make it easier to track RPC.
|
85
|
+
config.logger = CarrotRpc::TaggedLog.new(logger: Rails.logger, tags: ["Carrot RPC Client"])
|
86
|
+
|
87
|
+
# Don't use. Server implementation only. The values below are set via CLI:
|
88
|
+
# config.pidfile = nil
|
89
|
+
# config.runloop_sleep = 0
|
90
|
+
# config.logfile = nil
|
91
|
+
end
|
92
|
+
```
|
93
|
+
|
94
|
+
## Usage
|
95
|
+
### Writing Servers
|
96
|
+
Carrot CLI will look for your servers in `app/servers` directory. This directory should not be autoloaded by the host application.
|
97
|
+
|
98
|
+
|
99
|
+
Example Server: `app/servers/car_server.rb`
|
100
|
+
```ruby
|
101
|
+
class CarServer < CarrotRpc::RpcServer
|
102
|
+
def show(params)
|
103
|
+
# ...do something
|
104
|
+
Car.find(params[:id]).to_json
|
105
|
+
end
|
106
|
+
end
|
107
|
+
```
|
108
|
+
The method can return any data that can be stringified. But CarrotRPC uses [JSON RPC 2.0](http://www.jsonrpc.org/specification) as protocol for the message workflow.
|
109
|
+
|
110
|
+
With a standard Rails configuration `app/servers` will be marked as `eager_load: true` because `app` is `eager_load: true`. This is a problem because `Rails.application.eager_load!` is called when running `carrot_rpc`, which would lead to `app/servers/**/*.rb` being double loaded. To prevent the double loading, `app/servers` itself needs to be added as
|
111
|
+
a non-eager-load path, but still a load path.
|
112
|
+
|
113
|
+
In `config/application.rb`
|
114
|
+
```ruby
|
115
|
+
module MyApp
|
116
|
+
class Application < Rails::Application
|
117
|
+
config.paths.add "app/servers",
|
118
|
+
# `app/servers` MUST NOT be an eager_load path (to override the setting inherited from "app"), so
|
119
|
+
# that `carrot_rpc` does not double load `app/servers/**/*.rb` when first loading Rails and the
|
120
|
+
# servers.
|
121
|
+
eager_load: false,
|
122
|
+
# A load path so `carrot_rpc` can find load path ending in `app/servers` to scan for servers to
|
123
|
+
# load
|
124
|
+
load_path: true
|
125
|
+
end
|
126
|
+
end
|
127
|
+
```
|
128
|
+
|
129
|
+
### Writing Clients
|
130
|
+
Clients are not run in the CLI, and are typlically invoked during a request / response lifecycle in a web application. In the case of Rails, Clients would most likely be used in a controller action. Clients should be written in the `app/clients` directory of the host application, and should be autoloaded by Rails.
|
131
|
+
|
132
|
+
Example Client: `app/clients/cars_client.rb`
|
133
|
+
```ruby
|
134
|
+
class CarClient < CarrotRpc::RpcClient
|
135
|
+
# By default RpcClient defines the following Railsy inspired methods:
|
136
|
+
# def show(params)
|
137
|
+
# def index(params)
|
138
|
+
# def create(params)
|
139
|
+
# def update(params)
|
140
|
+
# You can easily add your own like so:
|
141
|
+
def foo_method(params)
|
142
|
+
remote_call('foo_method', params)
|
143
|
+
end
|
144
|
+
end
|
145
|
+
```
|
146
|
+
|
147
|
+
Example Rails Controller:
|
148
|
+
```ruby
|
149
|
+
class CarsController < ApplicationController
|
150
|
+
def show
|
151
|
+
car_client = CarClient.new
|
152
|
+
car_client.start
|
153
|
+
result = car_client.show({id: 1})
|
154
|
+
# Good idea to clean up connections when finished.
|
155
|
+
car_client.channel.close
|
156
|
+
end
|
157
|
+
end
|
158
|
+
```
|
159
|
+
|
160
|
+
## Development
|
161
|
+
|
162
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
163
|
+
|
164
|
+
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` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
165
|
+
|
166
|
+
## Contributing
|
167
|
+
|
168
|
+
1. Fork it ( https://github.com/[my-github-username]/carrot_rpc/fork )
|
169
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
170
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
171
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
172
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/bin/carrot_rpc
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require "bunny"
|
3
|
+
require_relative "../lib/carrot_rpc"
|
4
|
+
require_relative "../lib/carrot_rpc/cli"
|
5
|
+
require_relative "../lib/carrot_rpc/server_runner"
|
6
|
+
|
7
|
+
CarrotRpc::CLI.parse_options(ARGV)
|
8
|
+
|
9
|
+
CarrotRpc.configure do |config|
|
10
|
+
# Defaults to using ENV['RABBITMQ_URL'] for connection string.
|
11
|
+
config.bunny = Bunny.new if config.bunny.nil?
|
12
|
+
end
|
13
|
+
|
14
|
+
config = CarrotRpc.configuration
|
15
|
+
config.bunny.start
|
16
|
+
|
17
|
+
runner = CarrotRpc::ServerRunner.new(pidfile: config.pidfile, runloop_sleep: config.runloop_sleep,
|
18
|
+
daemonize: config.daemonize)
|
19
|
+
runner.run!
|
data/bin/console
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "carrot_rpc"
|
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
|
+
begin
|
14
|
+
require "byebug"
|
15
|
+
rescue LoadError
|
16
|
+
$stderr.puts "byebug could not be loaded"
|
17
|
+
end
|
18
|
+
|
19
|
+
require "irb"
|
20
|
+
IRB.start
|
data/bin/setup
ADDED
data/carrot_rpc.gemspec
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "carrot_rpc/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "carrot_rpc"
|
8
|
+
spec.version = CarrotRpc::VERSION
|
9
|
+
spec.authors = ["Scott Hamilton", "Luke Imhoff"]
|
10
|
+
spec.email = ["shamil614@gmail.com", "Kronic.Deth@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = "Remote Procedure Call (RPC) using the Bunny Gem over RabbitMQ"
|
13
|
+
spec.description = "Streamlined approach to setting up RPC over RabbitMQ."
|
14
|
+
spec.homepage = "https://github.com/C-S-D/carrot_rpc"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
18
|
+
# delete this section to allow pushing this gem to any host.
|
19
|
+
unless spec.respond_to?(:metadata)
|
20
|
+
fail "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
21
|
+
end
|
22
|
+
|
23
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
24
|
+
|
25
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
26
|
+
spec.require_paths = ["lib"]
|
27
|
+
|
28
|
+
# Production requirements
|
29
|
+
|
30
|
+
# Common extensions from Rails
|
31
|
+
spec.add_dependency "activesupport", "~> 4.2"
|
32
|
+
# The RabbitMQ library
|
33
|
+
spec.add_dependency "bunny", "~> 2.2"
|
34
|
+
|
35
|
+
# Development / Test Gems
|
36
|
+
|
37
|
+
# debugger
|
38
|
+
spec.add_development_dependency "byebug"
|
39
|
+
# Gemfile support for grouping gems for development-only or test-only
|
40
|
+
spec.add_development_dependency "bundler", "~> 1.9"
|
41
|
+
# Running commandline scripts
|
42
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
43
|
+
# Unit test framework
|
44
|
+
spec.add_development_dependency "rspec"
|
45
|
+
# Style-checker
|
46
|
+
spec.add_development_dependency "rubocop"
|
47
|
+
|
48
|
+
spec.required_ruby_version = "~> 2.2"
|
49
|
+
end
|