eventhub-processor2 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +5 -0
- data/.rspec +2 -0
- data/.travis.yml +36 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +76 -0
- data/LICENSE.txt +22 -0
- data/README.md +38 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/docker/Dockerfile +6 -0
- data/docker/README.md +14 -0
- data/docker/definitions.json +87 -0
- data/docker/rabbitmq.config +16 -0
- data/eventhub-processor2.gemspec +34 -0
- data/example/README.md +19 -0
- data/example/config/example.json +19 -0
- data/example/crasher.rb +57 -0
- data/example/example.rb +25 -0
- data/example/publisher.rb +95 -0
- data/lib/eventhub/actor_heartbeat.rb +121 -0
- data/lib/eventhub/actor_listener.rb +161 -0
- data/lib/eventhub/actor_watchdog.rb +41 -0
- data/lib/eventhub/configuration.rb +114 -0
- data/lib/eventhub/constant.rb +35 -0
- data/lib/eventhub/consumer.rb +9 -0
- data/lib/eventhub/hash_extensions.rb +49 -0
- data/lib/eventhub/helper.rb +55 -0
- data/lib/eventhub/logger.rb +14 -0
- data/lib/eventhub/message.rb +158 -0
- data/lib/eventhub/processor2.rb +121 -0
- data/lib/eventhub/statistics.rb +53 -0
- data/lib/eventhub/version.rb +3 -0
- data/lib/eventhub.rb +25 -0
- metadata +190 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 341f8d37ae52dd08f16ba0db17f49cc795b0f502
|
4
|
+
data.tar.gz: c6585473340abe1d26ef115518c951749b079bc8
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1a0e68f65673ef2b6feb271a02161320c747b9b419c3f130c5449a9f3bb7ee54f5b81bc1de4af52fbe9b27f5d9c422f7507d6403c9b0c33c53fe2909164004e4
|
7
|
+
data.tar.gz: f7fbabfe99aa117d1c70001286420e674849709ea255248dee67cb3f4fad5a7e95c563a781b60a0a26927ff36903807446baccc301dbaecc915b414db9adc860
|
data/.rspec
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
sudo: required
|
2
|
+
|
3
|
+
language: ruby
|
4
|
+
|
5
|
+
services:
|
6
|
+
- docker
|
7
|
+
|
8
|
+
os:
|
9
|
+
- linux
|
10
|
+
|
11
|
+
rvm:
|
12
|
+
- 2.4.2
|
13
|
+
- 2.4.1
|
14
|
+
- 2.3.1
|
15
|
+
- 2.1.5
|
16
|
+
|
17
|
+
env:
|
18
|
+
- secure: R0rBtWClmnJKO+zyAncF6+1qDpZ6JQR+Z6VXzUmWw8MuxUXv732XTNYulY/tTeDW876GofAsOF9QfX/jhF+2ubV8SzM6rKTZS09feATuOWRPtECEaBOPcb0JGYouBNg/Lv4onuXb8KS/9U093sRqiJMfib82iY7QK9bnDPsYoQXtMnI7KBeQNrTV0H4nby3lvbjcpMDhmyg+anOmCP5yepZeS53Qq+C5GHguYivisvhSPmCfONrT303XxFLDo/aOkMDlceeZBDhtcpm5Px3GIwybSI9xRtAnpT73oL1Piazda6mRN7VHoT7YAHKqcJNZDB48HYuTj3Pt3oVVw0v78Cf25caBqXttXflXfB8U3qfVt+94uF0icH34NAnpW9l1B8GXHi6CTr27Hg2ZatWkQCN1rCpcCaW0eM/NoAG6snwTIwr1VvjLWpSW5wwhQgHPyMl+MRlTmCn/UM+eQSwPVhflKpMS1+ah6cbhDrQQF1jDt3dRaLqSnVE4APrafdT90SA3SCG2MGOE+pp+DX528gEsRB34UdX2V67dlwXeRu1eU5vZBR7jZrqwuwTAgSvxEF0eJWC0kgvEfouzuVHhVmmSAcNNHlg2kd0/oyQELyXTsklJghwAupcy4X/GEAyfLW9GAxItjATyV7XyfjmaBmdzHl/5JWeXicoyZ/4+8iE=
|
19
|
+
|
20
|
+
before_install:
|
21
|
+
- cd docker && docker build -t processor-rabbitmq .
|
22
|
+
- docker run -d -p 5672:5672 -p 15672:15672 --name processor-rabbitmq processor-rabbitmq
|
23
|
+
- cd ..
|
24
|
+
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64
|
25
|
+
> ./cc-test-reporter
|
26
|
+
- chmod +x ./cc-test-reporter
|
27
|
+
- gem install bundler --version '~> 1.16.0'
|
28
|
+
|
29
|
+
before_script:
|
30
|
+
- "./cc-test-reporter before-build"
|
31
|
+
|
32
|
+
script:
|
33
|
+
- bundle exec rspec
|
34
|
+
|
35
|
+
after_script:
|
36
|
+
- "./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT"
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
eventhub-processor2 (1.0.0)
|
5
|
+
bunny (~> 2.7)
|
6
|
+
celluloid (~> 0.17)
|
7
|
+
eventhub-components (~> 0.2)
|
8
|
+
uuidtools (~> 2.1)
|
9
|
+
|
10
|
+
GEM
|
11
|
+
remote: https://rubygems.org/
|
12
|
+
specs:
|
13
|
+
amq-protocol (2.2.0)
|
14
|
+
bunny (2.7.2)
|
15
|
+
amq-protocol (>= 2.2.0)
|
16
|
+
celluloid (0.17.3)
|
17
|
+
celluloid-essentials
|
18
|
+
celluloid-extras
|
19
|
+
celluloid-fsm
|
20
|
+
celluloid-pool
|
21
|
+
celluloid-supervision
|
22
|
+
timers (>= 4.1.1)
|
23
|
+
celluloid-essentials (0.20.5)
|
24
|
+
timers (>= 4.1.1)
|
25
|
+
celluloid-extras (0.20.5)
|
26
|
+
timers (>= 4.1.1)
|
27
|
+
celluloid-fsm (0.20.5)
|
28
|
+
timers (>= 4.1.1)
|
29
|
+
celluloid-pool (0.20.5)
|
30
|
+
timers (>= 4.1.1)
|
31
|
+
celluloid-supervision (0.20.6)
|
32
|
+
timers (>= 4.1.1)
|
33
|
+
diff-lcs (1.3)
|
34
|
+
docile (1.1.5)
|
35
|
+
eventhub-components (0.2.0)
|
36
|
+
logstash-logger (~> 0.25)
|
37
|
+
hitimes (1.2.6)
|
38
|
+
json (2.1.0)
|
39
|
+
logstash-event (1.2.02)
|
40
|
+
logstash-logger (0.25.1)
|
41
|
+
logstash-event (~> 1.2)
|
42
|
+
rake (12.3.0)
|
43
|
+
rspec (3.7.0)
|
44
|
+
rspec-core (~> 3.7.0)
|
45
|
+
rspec-expectations (~> 3.7.0)
|
46
|
+
rspec-mocks (~> 3.7.0)
|
47
|
+
rspec-core (3.7.0)
|
48
|
+
rspec-support (~> 3.7.0)
|
49
|
+
rspec-expectations (3.7.0)
|
50
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
51
|
+
rspec-support (~> 3.7.0)
|
52
|
+
rspec-mocks (3.7.0)
|
53
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
54
|
+
rspec-support (~> 3.7.0)
|
55
|
+
rspec-support (3.7.0)
|
56
|
+
simplecov (0.15.1)
|
57
|
+
docile (~> 1.1.0)
|
58
|
+
json (>= 1.8, < 3)
|
59
|
+
simplecov-html (~> 0.10.0)
|
60
|
+
simplecov-html (0.10.2)
|
61
|
+
timers (4.1.2)
|
62
|
+
hitimes
|
63
|
+
uuidtools (2.1.5)
|
64
|
+
|
65
|
+
PLATFORMS
|
66
|
+
ruby
|
67
|
+
|
68
|
+
DEPENDENCIES
|
69
|
+
bundler (~> 1.16)
|
70
|
+
eventhub-processor2!
|
71
|
+
rake (~> 12.2)
|
72
|
+
rspec (~> 3.7)
|
73
|
+
simplecov (~> 0.15)
|
74
|
+
|
75
|
+
BUNDLED WITH
|
76
|
+
1.16.0
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2017 thomis
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
[![Dependency Status](https://gemnasium.com/badges/github.com/thomis/eventhub-processor2.svg)](https://gemnasium.com/github.com/thomis/eventhub-processor2)
|
2
|
+
[![Maintainability](https://api.codeclimate.com/v1/badges/9112358562f0614e0e02/maintainability)](https://codeclimate.com/github/thomis/eventhub-processor2/maintainability)
|
3
|
+
[![Test Coverage](https://api.codeclimate.com/v1/badges/9112358562f0614e0e02/test_coverage)](https://codeclimate.com/github/thomis/eventhub-processor2/test_coverage)
|
4
|
+
[![Build Status](https://travis-ci.org/thomis/eventhub-processor2.svg?branch=master)](https://travis-ci.org/thomis/eventhub-processor2)
|
5
|
+
|
6
|
+
# EventHub::Processor2
|
7
|
+
|
8
|
+
Next generation gem to build ruby based eventhub processor.
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
gem 'eventhub-processor2'
|
16
|
+
```
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
$ bundle
|
21
|
+
|
22
|
+
Or install it yourself as:
|
23
|
+
|
24
|
+
$ gem install eventhub-processor2
|
25
|
+
|
26
|
+
|
27
|
+
## Usage
|
28
|
+
|
29
|
+
|
30
|
+
## Development
|
31
|
+
|
32
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
33
|
+
|
34
|
+
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).
|
35
|
+
|
36
|
+
## Contributing
|
37
|
+
|
38
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/thomis/eventhub-processor2.
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'eventhub/processor2'
|
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__)
|
data/bin/setup
ADDED
data/docker/Dockerfile
ADDED
data/docker/README.md
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
## Docker Container
|
2
|
+
|
3
|
+
eventhub-processor2 interacts with RabbitMQ. For processor development and rspec test's is required to have a running RabbitMQ environment.
|
4
|
+
Please follow these steps to build and run a docker container with a predefined virtual host, exchanges, and queues.
|
5
|
+
|
6
|
+
* Have latest docker community edition installed (https://www.docker.com)
|
7
|
+
* cd into the docker folder
|
8
|
+
* docker build -t processor-rabbitmq .
|
9
|
+
* docker run -d -p 5672:5672 -p 15672:15672 --name processor-rabbitmq processor-rabbitmq
|
10
|
+
|
11
|
+
Is the docker container running fine?
|
12
|
+
Go to http://localhost:15672 and login to RabbitMQ Management Console with guest/guest
|
13
|
+
|
14
|
+
Additional information for RabbitMQ docker image: https://hub.docker.com/r/_/rabbitmq/
|
@@ -0,0 +1,87 @@
|
|
1
|
+
{
|
2
|
+
"rabbit_version": "3.6.14",
|
3
|
+
"users": [
|
4
|
+
{
|
5
|
+
"name": "guest",
|
6
|
+
"password_hash": "Xvy2EUToV+QoQDNOypgOwwnnjrN2PYSR2s29TAy0BrSsP1i+",
|
7
|
+
"hashing_algorithm": "rabbit_password_hashing_sha256",
|
8
|
+
"tags": "administrator"
|
9
|
+
}
|
10
|
+
],
|
11
|
+
"vhosts": [
|
12
|
+
{
|
13
|
+
"name": "event_hub"
|
14
|
+
}
|
15
|
+
],
|
16
|
+
"permissions": [
|
17
|
+
{
|
18
|
+
"user": "guest",
|
19
|
+
"vhost": "event_hub",
|
20
|
+
"configure": ".*",
|
21
|
+
"write": ".*",
|
22
|
+
"read": ".*"
|
23
|
+
}
|
24
|
+
],
|
25
|
+
"parameters": [],
|
26
|
+
"global_parameters": [
|
27
|
+
{
|
28
|
+
"name": "cluster_name",
|
29
|
+
"value": "rabbit@54d839ff16c7"
|
30
|
+
}
|
31
|
+
],
|
32
|
+
"policies": [],
|
33
|
+
"queues": [
|
34
|
+
{
|
35
|
+
"name": "event_hub.inbound",
|
36
|
+
"vhost": "event_hub",
|
37
|
+
"durable": true,
|
38
|
+
"auto_delete": false,
|
39
|
+
"arguments": {}
|
40
|
+
},
|
41
|
+
{
|
42
|
+
"name": "example",
|
43
|
+
"vhost": "event_hub",
|
44
|
+
"durable": true,
|
45
|
+
"auto_delete": false,
|
46
|
+
"arguments": {}
|
47
|
+
}
|
48
|
+
],
|
49
|
+
"exchanges": [
|
50
|
+
{
|
51
|
+
"name": "event_hub.inbound",
|
52
|
+
"vhost": "event_hub",
|
53
|
+
"type": "direct",
|
54
|
+
"durable": true,
|
55
|
+
"auto_delete": false,
|
56
|
+
"internal": false,
|
57
|
+
"arguments": {}
|
58
|
+
},
|
59
|
+
{
|
60
|
+
"name": "example",
|
61
|
+
"vhost": "event_hub",
|
62
|
+
"type": "direct",
|
63
|
+
"durable": true,
|
64
|
+
"auto_delete": false,
|
65
|
+
"internal": false,
|
66
|
+
"arguments": {}
|
67
|
+
}
|
68
|
+
],
|
69
|
+
"bindings": [
|
70
|
+
{
|
71
|
+
"source": "event_hub.inbound",
|
72
|
+
"vhost": "event_hub",
|
73
|
+
"destination": "event_hub.inbound",
|
74
|
+
"destination_type": "queue",
|
75
|
+
"routing_key": "",
|
76
|
+
"arguments": {}
|
77
|
+
},
|
78
|
+
{
|
79
|
+
"source": "example",
|
80
|
+
"vhost": "event_hub",
|
81
|
+
"destination": "example",
|
82
|
+
"destination_type": "queue",
|
83
|
+
"routing_key": "",
|
84
|
+
"arguments": {}
|
85
|
+
}
|
86
|
+
]
|
87
|
+
}
|
@@ -0,0 +1,16 @@
|
|
1
|
+
[
|
2
|
+
{ rabbit, [
|
3
|
+
{ loopback_users, [ ] },
|
4
|
+
{ tcp_listeners, [ 5672 ] },
|
5
|
+
{ ssl_listeners, [ ] },
|
6
|
+
{ default_vhost, <<"event_hub">> },
|
7
|
+
{ hipe_compile, false }
|
8
|
+
] },
|
9
|
+
{ rabbitmq_management, [
|
10
|
+
{ load_definitions, "/etc/rabbitmq/definitions.json"},
|
11
|
+
{ listener, [
|
12
|
+
{ port, 15672 },
|
13
|
+
{ ssl, false }
|
14
|
+
]}
|
15
|
+
]}
|
16
|
+
].
|
@@ -0,0 +1,34 @@
|
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'eventhub/version'
|
4
|
+
|
5
|
+
note = 'Next generation gem to build ruby based eventhub processor'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'eventhub-processor2'
|
9
|
+
spec.version = EventHub::VERSION
|
10
|
+
spec.authors = ['Steiner, Thomas']
|
11
|
+
spec.email = ['thomas.steiner@ikey.ch']
|
12
|
+
|
13
|
+
spec.summary = note
|
14
|
+
spec.description = note
|
15
|
+
spec.homepage = 'https://github.com/thomis/eventhub-processor2'
|
16
|
+
spec.license = 'MIT'
|
17
|
+
|
18
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
19
|
+
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 'celluloid', '~> 0.17'
|
26
|
+
spec.add_dependency 'bunny', '~> 2.7'
|
27
|
+
spec.add_dependency 'eventhub-components', '~> 0.2'
|
28
|
+
spec.add_dependency 'uuidtools', '~> 2.1'
|
29
|
+
|
30
|
+
spec.add_development_dependency 'bundler', '~> 1.16'
|
31
|
+
spec.add_development_dependency 'rake', '~> 12.2'
|
32
|
+
spec.add_development_dependency 'rspec', '~> 3.7'
|
33
|
+
spec.add_development_dependency 'simplecov', '~> 0.15'
|
34
|
+
end
|
data/example/README.md
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
## Example Application
|
2
|
+
|
3
|
+
### Components
|
4
|
+
* example.rb - is listening to the example queue, reads id from message and deletes file with id in data folder
|
5
|
+
* publisher.rb - creates a file with name id.json (id=guid) and publishes a json messages with id as content to the example exchange
|
6
|
+
* crasher.rb - randomly restarts example processes or docker container (processor-rabbitmq)
|
7
|
+
|
8
|
+
### How to use
|
9
|
+
* Make sure docker container (process-rabbitmq) is running
|
10
|
+
* Start 1 or more example processes (I did 3)
|
11
|
+
* Start 1 or more publisher processes ( I did 1)
|
12
|
+
* Start one crasher.rb
|
13
|
+
|
14
|
+
### What is the goal
|
15
|
+
* See how the components work under various conditions. Feel free to manually interact (Exp. kill -9 PID)
|
16
|
+
* There should be no files left in the data folder when all example messages are consumed
|
17
|
+
|
18
|
+
### Note
|
19
|
+
It can happen that a file gets deleted before message is acknowledged. This message will be processed again and will just log a warning about missing file. Due to the nature of 2 independent processes it can not garanteed that both process transaction are all done or not done at all, but with message acknowledgement and and publisher confirms we can mitigate risk of lost messages.
|
@@ -0,0 +1,19 @@
|
|
1
|
+
{
|
2
|
+
"development": {
|
3
|
+
"server": {
|
4
|
+
"user": "guest",
|
5
|
+
"password": "guest",
|
6
|
+
"host": "localhost",
|
7
|
+
"vhost": "event_hub",
|
8
|
+
"port": 5672,
|
9
|
+
"tls": false
|
10
|
+
},
|
11
|
+
"processor": {
|
12
|
+
"heartbeat_cycle_in_s": 300,
|
13
|
+
"watchdog_cycle_in_s": 15,
|
14
|
+
"listener_queues": [
|
15
|
+
"example"
|
16
|
+
]
|
17
|
+
}
|
18
|
+
}
|
19
|
+
}
|
data/example/crasher.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
# MyProcess
|
2
|
+
class MyProcess
|
3
|
+
attr_reader :id
|
4
|
+
def initialize(id)
|
5
|
+
@id = id
|
6
|
+
end
|
7
|
+
|
8
|
+
def restart
|
9
|
+
puts "Sending Signal HUP to process [#{@id}]"
|
10
|
+
Process.kill('HUP', @id)
|
11
|
+
rescue Errno::ESRCH
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.all
|
15
|
+
processes = []
|
16
|
+
data = `ps | grep example.rb | grep ruby`
|
17
|
+
data.lines[0..-2].each do |line|
|
18
|
+
processes << MyProcess.new(line.split(' ')[0].to_i)
|
19
|
+
end
|
20
|
+
|
21
|
+
puts "Found ids: #{processes.map{ |pr| pr.id}.join(', ')}"
|
22
|
+
processes
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
# Docker
|
27
|
+
class Docker
|
28
|
+
def initialize(name, time = 10)
|
29
|
+
@name = name
|
30
|
+
@time = time
|
31
|
+
end
|
32
|
+
|
33
|
+
def restart
|
34
|
+
puts "Restart (#{@time}) [#{@name}]"
|
35
|
+
`docker restart -t #{@time} #{@name}`
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def items
|
40
|
+
a = []
|
41
|
+
a << Docker.new('processor-rabbitmq')
|
42
|
+
a << Docker.new('processor-rabbitmq', 0)
|
43
|
+
a << MyProcess.all
|
44
|
+
a.flatten!
|
45
|
+
end
|
46
|
+
|
47
|
+
run = true
|
48
|
+
Signal.trap('INT') { run = false }
|
49
|
+
|
50
|
+
while run
|
51
|
+
to_sleep = rand(3600)
|
52
|
+
puts "Waiting [#{to_sleep}]..."
|
53
|
+
sleep to_sleep
|
54
|
+
items.sample.restart
|
55
|
+
end
|
56
|
+
|
57
|
+
puts 'Done'
|
data/example/example.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require_relative '../lib/eventhub'
|
2
|
+
|
3
|
+
module EventHub
|
4
|
+
# Demo class
|
5
|
+
class Example < Processor2
|
6
|
+
def handle_message(message, args = {})
|
7
|
+
id = message.body['id']
|
8
|
+
name = "data/#{id}.json"
|
9
|
+
|
10
|
+
begin
|
11
|
+
File.delete(name)
|
12
|
+
rescue => ex
|
13
|
+
EventHub.logger.warn("File [#{name}]: #{ex}")
|
14
|
+
end
|
15
|
+
|
16
|
+
{ body: { id: id, message: 'has been done'}}
|
17
|
+
end
|
18
|
+
|
19
|
+
def version
|
20
|
+
'10.1.1'
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
EventHub::Example.new.start
|
@@ -0,0 +1,95 @@
|
|
1
|
+
require 'bunny'
|
2
|
+
require 'celluloid/current'
|
3
|
+
require 'json'
|
4
|
+
require 'securerandom'
|
5
|
+
|
6
|
+
Celluloid.logger = nil
|
7
|
+
Celluloid.exception_handler { |ex| puts "Exception occured: #{ex}" }
|
8
|
+
|
9
|
+
# Publisher
|
10
|
+
class Publisher
|
11
|
+
include Celluloid
|
12
|
+
|
13
|
+
def initialize
|
14
|
+
async.start
|
15
|
+
end
|
16
|
+
|
17
|
+
def start
|
18
|
+
connect
|
19
|
+
count = 1
|
20
|
+
loop do
|
21
|
+
do_the_work
|
22
|
+
|
23
|
+
sleep 0.001
|
24
|
+
print '.'
|
25
|
+
puts '' if (count % 80).zero?
|
26
|
+
count += 1
|
27
|
+
end
|
28
|
+
ensure
|
29
|
+
@connection.close if @connection
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def connect
|
35
|
+
@connection = Bunny.new(vhost: 'event_hub',
|
36
|
+
automatic_recovery: false,
|
37
|
+
logger: Logger.new('/dev/null'))
|
38
|
+
@connection.start
|
39
|
+
@channel = @connection.create_channel
|
40
|
+
@channel.confirm_select
|
41
|
+
@exchange = @channel.direct('example', durable: true)
|
42
|
+
end
|
43
|
+
|
44
|
+
def do_the_work
|
45
|
+
id = SecureRandom.uuid
|
46
|
+
data = { body: { id: id } }.to_json
|
47
|
+
|
48
|
+
file = File.open("data/#{id}.json", 'w')
|
49
|
+
file.write(data)
|
50
|
+
file.close
|
51
|
+
|
52
|
+
@exchange.publish(data, persistent: true)
|
53
|
+
success = @channel.wait_for_confirms
|
54
|
+
|
55
|
+
raise 'Published message not confirmed' unless success
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
# Application
|
60
|
+
class Application
|
61
|
+
def initialize
|
62
|
+
@run = true
|
63
|
+
@config = Celluloid::Supervision::Configuration.define(
|
64
|
+
[
|
65
|
+
{ type: Publisher, as: :publisher }
|
66
|
+
]
|
67
|
+
)
|
68
|
+
|
69
|
+
@config.injection!(:before_restart, proc do
|
70
|
+
puts 'Restarting in 5 seconds...'
|
71
|
+
sleep 5
|
72
|
+
end)
|
73
|
+
end
|
74
|
+
|
75
|
+
def start
|
76
|
+
puts 'Publisher has been started'
|
77
|
+
@config.deploy
|
78
|
+
main_event_loop
|
79
|
+
cleanup
|
80
|
+
puts 'Publisher has been stopped'
|
81
|
+
end
|
82
|
+
|
83
|
+
private
|
84
|
+
|
85
|
+
def main_event_loop
|
86
|
+
Signal.trap(:INT) { @run = false }
|
87
|
+
sleep 0.5 while @run
|
88
|
+
end
|
89
|
+
|
90
|
+
def cleanup
|
91
|
+
Celluloid.shutdown
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
Application.new.start
|