observed 0.1.1 → 0.2.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +9 -9
- data/.travis.yml +4 -0
- data/README.md +53 -78
- data/examples/observed.rb +1 -1
- data/exe/observed-oneshot +3 -1
- data/features/explicit_routing.feature +33 -0
- data/features/oneshot.feature +4 -0
- data/features/test_in_single_ruby_source.feature +4 -0
- data/integrations/observed-clockwork/features/run_observed_inside_clockwork.feature +6 -7
- data/integrations/observed-clockwork/lib/observed/clockwork/version.rb +1 -1
- data/integrations/observed-clockwork/lib/observed/clockwork.rb +0 -1
- data/integrations/observed-clockwork/observed-clockwork.gemspec +1 -1
- data/integrations/observed-eventmachine/.gitignore +17 -0
- data/integrations/observed-eventmachine/Gemfile +8 -0
- data/integrations/observed-eventmachine/LICENSE.txt +22 -0
- data/integrations/observed-eventmachine/README.md +29 -0
- data/integrations/observed-eventmachine/Rakefile +1 -0
- data/integrations/observed-eventmachine/examples/observed.rb +30 -0
- data/integrations/observed-eventmachine/features/integration_via_single_ruby_source.feature +48 -0
- data/integrations/observed-eventmachine/features/support/env.rb +8 -0
- data/integrations/observed-eventmachine/lib/observed/eventmachine/version.rb +5 -0
- data/integrations/observed-eventmachine/lib/observed/eventmachine.rb +70 -0
- data/integrations/observed-eventmachine/observed-eventmachine.gemspec +28 -0
- data/lib/observed/application/oneshot.rb +14 -37
- data/lib/observed/builtin_plugins/file.rb +5 -14
- data/lib/observed/builtin_plugins/stdout.rb +7 -14
- data/lib/observed/config.rb +4 -4
- data/lib/observed/config_builder.rb +154 -87
- data/lib/observed/config_dsl.rb +2 -8
- data/lib/observed/configurable.rb +61 -3
- data/lib/observed/context.rb +90 -0
- data/lib/observed/default/observer.rb +2 -5
- data/lib/observed/default.rb +0 -1
- data/lib/observed/event_bus.rb +31 -0
- data/lib/observed/execution_job_factory.rb +95 -0
- data/lib/observed/job.rb +163 -0
- data/lib/observed/jobbed_event_bus.rb +33 -0
- data/lib/observed/logging.rb +40 -0
- data/lib/observed/observer.rb +1 -0
- data/lib/observed/observer_helpers/timer.rb +13 -5
- data/lib/observed/pluggable.rb +11 -0
- data/lib/observed/reporter/regexp_matching.rb +2 -1
- data/lib/observed/reporter/report_formatting.rb +57 -0
- data/lib/observed/reporter.rb +0 -2
- data/lib/observed/system.rb +11 -78
- data/lib/observed/translator.rb +22 -0
- data/lib/observed/version.rb +1 -1
- data/lib/observed.rb +10 -12
- data/omnibus-observed/.gitignore +9 -0
- data/omnibus-observed/Berksfile +3 -0
- data/omnibus-observed/Berksfile.lock +52 -0
- data/omnibus-observed/Gemfile +4 -0
- data/omnibus-observed/README.md +102 -0
- data/omnibus-observed/Vagrantfile +93 -0
- data/omnibus-observed/config/projects/observed.rb +20 -0
- data/omnibus-observed/config/software/observed.rb +19 -0
- data/omnibus-observed/package-scripts/observed/makeselfinst +27 -0
- data/omnibus-observed/package-scripts/observed/postinst +17 -0
- data/omnibus-observed/package-scripts/observed/postrm +9 -0
- data/plugins/observed-fluentd/lib/observed/fluentd/version.rb +1 -1
- data/plugins/observed-gauge/README.md +5 -0
- data/plugins/observed-gauge/lib/observed/gauge/version.rb +1 -1
- data/plugins/observed-gauge/lib/observed/gauge.rb +11 -13
- data/plugins/observed-gauge/observed-gauge.gemspec +1 -1
- data/plugins/observed-gauge/spec/gauge_spec.rb +7 -7
- data/plugins/observed-growl/Gemfile +6 -0
- data/plugins/observed-growl/lib/observed/growl.rb +80 -0
- data/plugins/observed-http/lib/observed/http/version.rb +1 -1
- data/plugins/observed-http/lib/observed/http.rb +10 -8
- data/plugins/observed-http/observed-http.gemspec +1 -1
- data/plugins/observed-http/spec/http_spec.rb +62 -7
- data/plugins/observed-http/spec/integration_spec.rb +14 -0
- data/plugins/observed-shell/Gemfile +5 -0
- data/plugins/observed-shell/lib/observed/shell.rb +54 -0
- data/run-integration-tests +81 -0
- data/spec/builtin_plugins/stdout_spec.rb +7 -3
- data/spec/config_builder_spec.rb +42 -59
- data/spec/config_dsl_spec.rb +4 -0
- data/spec/configurable_spec.rb +141 -31
- data/spec/event_bus_spec.rb +16 -0
- data/spec/execution_job_factory_spec.rb +35 -0
- data/spec/job_factory_spec.rb +16 -0
- data/spec/job_spec.rb +228 -0
- data/spec/jobbed_event_bus_spec.rb +38 -0
- data/spec/observed_spec.rb +203 -0
- data/spec/observer_helpers/timer_spec.rb +187 -0
- data/spec/oneshot_spec.rb +7 -2
- data/spec/system_spec.rb +5 -39
- metadata +55 -12
- data/lib/observed/default/reporter.rb +0 -17
- data/lib/observed/reader.rb +0 -14
- data/lib/observed/writer.rb +0 -14
- data/spec/reader_spec.rb +0 -15
- data/spec/writer_spec.rb +0 -16
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
ZjAzODY3YWFkYmE3ZWM3MGFmYzkzY2Y4ZjI4MTExNmQyYjRmZTJjOQ==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
7
|
-
|
6
|
+
YzdlNTAyYWNmNzZmNWNlODNmYTA0MDY4ZjFjM2RlMWFlNTFhNjFlMw==
|
7
|
+
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
Y2UzMWZiYjliOGE0MDRiMGVhZTE1ZGExOWQzNzBjYjU5YzM4MDM3OWE0MWRh
|
10
|
+
OWJjYWRlNDc5YjhjMGRmNTY1NGI0Yzg0MWMxY2Q1NjdjZjA1MGZjNjYzODU0
|
11
|
+
MDA1ZGM4MjE5MDQzYTMxNWYwNjExYzYzMmUyOTFkYzBjNzFlYTM=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
NTZhM2U0Nzg3M2ViMjMzNDM1NTI3YTZlOTUwOWE1NDk0OGNiMGM3Y2MwNWVi
|
14
|
+
MGZhOTZkY2YxMzBkYzZhNWJhM2M2MmQyMWFjYzE2YWY3YTRmM2Y2ZGViYjEx
|
15
|
+
ZDg5YzM5YmQzZTY4MDg0NTE3YTJlNTRhNWMwNTAzOGYzYWUxNmY=
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -1,67 +1,45 @@
|
|
1
|
-
#
|
1
|
+
# Observed
|
2
2
|
|
3
|
-
|
3
|
+
[![Build Status](https://travis-ci.org/gree/observed.png?branch=master)](https://travis-ci.org/gree/observed) [![Coverage Status](https://coveralls.io/repos/gree/observed/badge.png?branch=master)](https://coveralls.io/r/gree/observed?branch=master) [![Code Climate](https://codeclimate.com/github/gree/observed.png)](https://codeclimate.com/github/gree/observed)
|
4
4
|
|
5
|
-
|
6
|
-
ones in your production environment.
|
7
|
-
It is designed with extensibility in mind.
|
5
|
+
Observed is a highly extensible framework for polling applications, middlewares and services running locally or on remote servers.
|
8
6
|
|
9
7
|
-----
|
10
8
|
|
11
|
-
Observed
|
12
|
-
Observed is open for extension which means that it is extensible via plugins to support add more services and transformations.
|
9
|
+
Observed allows you to:
|
13
10
|
|
14
|
-
|
11
|
+
1. poll server(s),
|
12
|
+
2. optionally modify the response result,
|
13
|
+
3. pass the result on to other services or trigger other tasks
|
15
14
|
|
16
|
-
|
17
|
-
2. optionally transform the result,
|
18
|
-
3. pass the result to another services or trigger something
|
15
|
+
All the above 3 operations are extensible via plugins and can be configured via configuration files.
|
19
16
|
|
20
|
-
|
21
|
-
|
22
|
-
There are known plugins for:
|
17
|
+
There are plugins available for:
|
23
18
|
|
24
19
|
- Polling HTTP-based services to detect failures and performance degradation
|
25
20
|
- Sending observed data to Fluentd
|
26
|
-
- (More to come)
|
27
|
-
|
28
|
-
Observed has has a very small code-base (only a few hundreds of lines) and it should not be too hard to understand the
|
29
|
-
code and develop plugins.
|
30
21
|
|
31
|
-
|
32
|
-
and having knowledge of other Ruby products may help understanding Observed.
|
33
|
-
(But remember that Observed is yet a highly ambitioned and experimental framework. Observed's code has more space to
|
34
|
-
improve than other great Ruby products.)
|
22
|
+
Additional plugins will be released in the future.
|
35
23
|
|
36
|
-
Observed
|
37
|
-
Specially for Observed, this means that it should work similarly when it is ran as a daemon or a _oneshot_ application like
|
38
|
-
a regular cron job as states are stored somewhere outside of the application instance.
|
24
|
+
Observed has has a very small code-base (only a few hundred lines) which makes reading and understanding it's source code and developing plugins for it a relatively simple task.
|
39
25
|
|
40
|
-
Observed is
|
26
|
+
Observed's extensible design through plugins is inspired by other Ruby projects such as Fluentd. People familiar with other Ruby products should feel right at home with Observed. As of now it is a highly ambitious and experimental framework with big potential for growth and improvements.
|
41
27
|
|
42
|
-
|
28
|
+
Observed is (and its plugins should be) stateless.
|
29
|
+
This means that it should work in the same manner whether it is run as a daemon or a _oneshot_ application like
|
30
|
+
a regular cron job, as states should be stored outside of the application instance with regards to Observed.
|
43
31
|
|
44
|
-
Observed is
|
32
|
+
Observed is intended to be run on Ruby 1.9.3 but should work on Ruby 2.0+ too.
|
45
33
|
|
46
|
-
|
47
|
-
-
|
48
|
-
|
49
|
-
|
50
|
-
- Not a log collector like Fluentd.
|
51
|
-
Observed can be used to emit event logs to the log collector of your choice, but it does not replace or act like that.
|
52
|
-
- Not a [job scheduler](http://en.wikipedia.org/wiki/Job_scheduler) like cron, though it can be integrated to those schedulers to make them trigger Observed to do the
|
53
|
-
job.
|
34
|
+
## Observed is not:
|
35
|
+
- a monitoring and reporting tool such as New Relic etc. However, Observed can be used for collecting simple metrics via plugins, then pass them on to another full-fledged monitoring or reporting tool.
|
36
|
+
- a log collector like Fluentd. Observed can be used to emit event logs to the log collector of your choice, but it does not replace the log collector.
|
37
|
+
- a [job scheduler](http://en.wikipedia.org/wiki/Job_scheduler) (e.g. cron), though it can be integrated with job schedulers to make them trigger Observed to perform jobs.
|
54
38
|
|
55
39
|
## Similar products
|
56
40
|
|
57
|
-
- [vacuumetrix](https://github.com/99designs/vacuumetrix)
|
58
|
-
|
59
|
-
On the other hand, Observed scope itself to be used just for `polling` and also it outputs to not only monitoring
|
60
|
-
systems but any system.
|
61
|
-
- [metrics-sampler](https://github.com/dimovelev/metrics-sampler) is a java program which queries metrics from various
|
62
|
-
sources and sends them to outputs like the console or Graphite. It supports inputs such as JMX, JDBC, apache-status,
|
63
|
-
Oracle NoSQL, Redis, webmethods, or regular process which is runnable via the command-line and is able to communiate
|
64
|
-
via standard input/output.
|
41
|
+
- [vacuumetrix](https://github.com/99designs/vacuumetrix) - Collects metrics from various sources and outputs them to various destinations (e.g Graphite). Observed only covers the `polling` part, but can output to not just monitoring systems but any system.
|
42
|
+
- [metrics-sampler](https://github.com/dimovelev/metrics-sampler) - A Java program which queries metrics from various sources and outputs to various destinations such as Graphite or consoles. It supports various methods of input such as JMX, JDBC, apache-status, Oracle NoSQL, Redis, web API, or regular processes which are executable via the command-line, and is able to communicate via standard input/output.
|
65
43
|
|
66
44
|
## Installation
|
67
45
|
|
@@ -73,7 +51,7 @@ And then execute:
|
|
73
51
|
|
74
52
|
$ bundle
|
75
53
|
|
76
|
-
Or install it yourself
|
54
|
+
Or install it yourself with:
|
77
55
|
|
78
56
|
$ gem install observed
|
79
57
|
|
@@ -83,24 +61,23 @@ Or `git clone` the sources and install by using rake:
|
|
83
61
|
|
84
62
|
## Usage
|
85
63
|
|
86
|
-
|
64
|
+
Simple example: _use Observed to observe and report the healthiness of Google_.
|
87
65
|
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
We start with installing some observed plugins:
|
66
|
+
Assume that we want Observed to poll a HTTP-based web service (Google) and report the results.
|
67
|
+
|
68
|
+
Since Observed by itself is just a framework and by default does not have built-in support for any service, we start by installing some Observed plugins:
|
92
69
|
|
93
70
|
$ gem install observed
|
94
71
|
$ gem install observed-clockwork
|
95
72
|
$ gem install observed-http
|
96
73
|
|
97
|
-
|
98
|
-
|
99
|
-
file. Clockwork is a
|
100
|
-
|
101
|
-
Observed with cron) but we
|
74
|
+
For this example, we will use observed-clockwork to trigger polls through [Clockwork](https://github.com/tomykaira/clockwork).
|
75
|
+
|
76
|
+
observed-clockwork is a library to be used with Clockwork's configuration file. Clockwork is a cron replacement for a scheduler process that is written in Ruby.
|
77
|
+
|
78
|
+
Note that we can also use cron, which is supported via observed-cron plugin (even without the plugin it is really simple to use Observed with cron) but we will use Clockwork for this example.
|
102
79
|
|
103
|
-
|
80
|
+
Edit the contents of `clockwork.rb`:
|
104
81
|
|
105
82
|
```ruby
|
106
83
|
require 'pathname'
|
@@ -110,18 +87,17 @@ require 'observed/clockwork'
|
|
110
87
|
|
111
88
|
include Clockwork
|
112
89
|
|
113
|
-
# Below two lines are specific to Observed's Clockwork support.
|
114
|
-
# Others lines are just standard `clockwork.rb`
|
115
|
-
include Observed::Clockwork
|
116
|
-
|
117
90
|
the_dir = Pathname.new(File.dirname(__FILE__))
|
118
91
|
|
92
|
+
# The following two lines are specific to Observed's Clockwork support.
|
93
|
+
include Observed::Clockwork
|
94
|
+
|
119
95
|
register_observed_handler :config_file => the_dir + 'observed.rb'
|
120
96
|
|
121
97
|
every(10.seconds, 'google.health')
|
122
98
|
```
|
123
99
|
|
124
|
-
|
100
|
+
Edit the contents of `observed.rb` as follows:
|
125
101
|
|
126
102
|
```ruby
|
127
103
|
require 'observed/http'
|
@@ -143,10 +119,10 @@ report /google.health/, via: 'stdout', with: {
|
|
143
119
|
}
|
144
120
|
```
|
145
121
|
|
146
|
-
As you see, `observed.
|
147
|
-
You can
|
122
|
+
As you see, `observed.rb` is just a Ruby source file that describes Observed's configuration.
|
123
|
+
You can use Ruby language's features, gems, etc.
|
148
124
|
|
149
|
-
|
125
|
+
That sums up the configuration part of things.
|
150
126
|
|
151
127
|
Now, run:
|
152
128
|
|
@@ -161,35 +137,34 @@ Google is healthy! (^o^)
|
|
161
137
|
...
|
162
138
|
```
|
163
139
|
|
164
|
-
|
165
|
-
You just saw that Google's healthiness is printed to the standard output every 10 seconds.
|
166
|
-
In other words, you did observed Google's healthiness with easily with Observed.
|
140
|
+
And we're done! That's all it takes to print Google's health to standard output every 10 seconds with Observed.
|
167
141
|
|
168
|
-
The example described here is fairly simple and
|
169
|
-
|
170
|
-
If you want to monitor
|
171
|
-
|
172
|
-
We
|
173
|
-
result to other services. Things like monitoring, watching, alerting can be done there.
|
142
|
+
The example described here is fairly simple and may look useless at first glance, but keep in mind that Observed is just a framework.
|
143
|
+
|
144
|
+
If you want to monitor performance or get statistics on performance on your service, you can redirect the results to Fluentd or other service to take advantage of their features and plugins.
|
145
|
+
|
146
|
+
We prefer to not reinvent the wheel and encourage people to use Observed for polling and output results to other services for other tasks like monitoring or alerts.
|
174
147
|
|
175
148
|
## Documentation
|
176
149
|
|
177
150
|
Observed is documented with [YARD](https://github.com/lsegal/yard).
|
178
|
-
The documentation is not currently hosted on any server and we have to build the document before reading it.
|
179
151
|
|
180
|
-
|
152
|
+
The latest documentation for the master branch of Observed [is available at rubydoc.info](http://rubydoc.info/github/gree/observed).
|
153
|
+
|
154
|
+
You can also generate the document locally.
|
155
|
+
To do so, install YARD:
|
181
156
|
|
182
157
|
```
|
183
158
|
$ gem install yard
|
184
159
|
```
|
185
160
|
|
186
|
-
And then run `yardoc`
|
161
|
+
And then run `yardoc` as follows:
|
187
162
|
|
188
163
|
```
|
189
164
|
$ yardoc 'lib/**/*.rb'
|
190
165
|
```
|
191
166
|
|
192
|
-
|
167
|
+
You can then open the generated documentation:
|
193
168
|
|
194
169
|
```
|
195
170
|
$ open doc/index.html
|
@@ -201,4 +176,4 @@ $ open doc/index.html
|
|
201
176
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
202
177
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
203
178
|
4. Push to the branch (`git push origin my-new-feature`)
|
204
|
-
5. Create new
|
179
|
+
5. Create a new pull request
|
data/examples/observed.rb
CHANGED
data/exe/observed-oneshot
CHANGED
@@ -4,7 +4,9 @@ require 'observed'
|
|
4
4
|
require 'observed/application/oneshot'
|
5
5
|
|
6
6
|
begin
|
7
|
-
|
7
|
+
argv = ARGV.dup
|
8
|
+
args = Observed::Application::Oneshot.parse_argv!(argv)
|
9
|
+
Observed::Application::Oneshot.create(args).run *argv
|
8
10
|
rescue Observed::Application::Oneshot::InvalidArgumentError => e
|
9
11
|
$stderr.puts 'Missing the path to a Observed config file'
|
10
12
|
$stderr.puts 'Usage: observed-oneshot [-d|--debug] [-l LOG_FILE|--log_file LOG_FILE] <observed.rb>'
|
@@ -0,0 +1,33 @@
|
|
1
|
+
Feature: Routing observed data without matching tags
|
2
|
+
|
3
|
+
In order to use Observed when the user's user-case is too simple that even tags are not needed
|
4
|
+
I want to write configs with reporters which have no regexp patterns to match observed data
|
5
|
+
|
6
|
+
Scenario: Create a .rb file containing Observed configuration and run it with the observed-oneshot command
|
7
|
+
Given a file named "test.rb" with:
|
8
|
+
"""
|
9
|
+
require 'observed'
|
10
|
+
|
11
|
+
include Observed
|
12
|
+
|
13
|
+
class Test < Observed::Observer
|
14
|
+
plugin_name 'test'
|
15
|
+
def observe
|
16
|
+
system.report(tag, {foo:1})
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
observe_then_report = (observe 'foo', via: 'test')
|
21
|
+
.then(report via: 'stdout')
|
22
|
+
|
23
|
+
observe_then_report.now
|
24
|
+
"""
|
25
|
+
When I run `ruby test.rb`
|
26
|
+
Then the output should contain:
|
27
|
+
"""
|
28
|
+
{:foo=>1}
|
29
|
+
"""
|
30
|
+
Then the output should not contain:
|
31
|
+
"""
|
32
|
+
Error
|
33
|
+
"""
|
data/features/oneshot.feature
CHANGED
@@ -26,12 +26,11 @@ Feature: Running Observed inside Clockwork
|
|
26
26
|
def observe
|
27
27
|
sleep_duration = rand / 20
|
28
28
|
sleep sleep_duration
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
exit
|
29
|
+
::Thread.start {
|
30
|
+
sleep 1
|
31
|
+
exit
|
32
|
+
}
|
33
|
+
system.report(tag, {text: "Foo #{sleep_duration}"})
|
35
34
|
end
|
36
35
|
|
37
36
|
def logger
|
@@ -55,5 +54,5 @@ Feature: Running Observed inside Clockwork
|
|
55
54
|
When I run `clockwork clockwork.rb`
|
56
55
|
Then the output should contain:
|
57
56
|
"""
|
58
|
-
foo_1 Foo
|
57
|
+
00 foo_1 {:text=>"Foo
|
59
58
|
"""
|
@@ -21,7 +21,7 @@ Gem::Specification.new do |spec|
|
|
21
21
|
|
22
22
|
spec.add_dependency 'clockwork'
|
23
23
|
spec.add_dependency 'daemons'
|
24
|
-
spec.add_dependency 'observed', "~> 0.
|
24
|
+
spec.add_dependency 'observed', "~> 0.2.0.rc1"
|
25
25
|
spec.add_development_dependency "bundler", "~> 1.3"
|
26
26
|
spec.add_development_dependency "rake"
|
27
27
|
spec.add_development_dependency "cucumber"
|
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 GREE, Inc.
|
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.
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# Observed::Eventmachine
|
2
|
+
|
3
|
+
A simple, experimental integration of Observed and EventMachine.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'observed-eventmachine'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install observed-eventmachine
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
See examples under "examples/" and the cucumber tests under "features".
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'observed'
|
2
|
+
require 'observed/eventmachine'
|
3
|
+
|
4
|
+
include Observed
|
5
|
+
|
6
|
+
# This is a observed-eventmachine specific code to extend the DSL to use EventMachine
|
7
|
+
extend Observed::EM
|
8
|
+
|
9
|
+
class Test < Observed::Observer
|
10
|
+
plugin_name 'test'
|
11
|
+
def observe
|
12
|
+
puts "Sleeping 10 seconds"
|
13
|
+
sleep 10.0
|
14
|
+
puts "Slept 10 seconds"
|
15
|
+
[tag, {foo:1}]
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
observe 'foo', via: 'test'
|
20
|
+
|
21
|
+
report /foo/, via: 'stdout', with: {
|
22
|
+
format: -> _, _, data { "#{data}" }
|
23
|
+
}
|
24
|
+
|
25
|
+
# This is a observed-eventmachine specific code to schedule the observation on 'foo'
|
26
|
+
every 1, run: 'foo'
|
27
|
+
|
28
|
+
# This is a observed-eventmachine specific code to start running observations periodically, until we stop that by
|
29
|
+
# sending signals
|
30
|
+
start
|
@@ -0,0 +1,48 @@
|
|
1
|
+
Feature: Integration via single Ruby source
|
2
|
+
|
3
|
+
In order to easily integrate Observed with EventMachine,
|
4
|
+
I want to extend Observed's DSL with words specific to the EventMachine integration,
|
5
|
+
and make it runnable and testable in single Ruby source.
|
6
|
+
|
7
|
+
Scenario: Create a .rb file containing Observed code and run it with the ruby command
|
8
|
+
Given a file named "test.rb" with:
|
9
|
+
"""
|
10
|
+
require 'observed'
|
11
|
+
require 'observed/eventmachine'
|
12
|
+
|
13
|
+
include Observed
|
14
|
+
|
15
|
+
extend Observed::EM
|
16
|
+
|
17
|
+
$count = 0
|
18
|
+
|
19
|
+
class Test < Observed::Observer
|
20
|
+
plugin_name 'test'
|
21
|
+
def observe
|
22
|
+
$count += 1
|
23
|
+
system.report(tag, {foo:1})
|
24
|
+
if $count >= 2
|
25
|
+
Thread.start {
|
26
|
+
sleep 1
|
27
|
+
exit
|
28
|
+
}
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
observe 'foo', via: 'test'
|
34
|
+
|
35
|
+
report /foo/, via: 'stdout', with: {
|
36
|
+
format: -> tag, _, data { "#{tag} #{$count} #{data}" }
|
37
|
+
}
|
38
|
+
|
39
|
+
every 1, run: 'foo'
|
40
|
+
|
41
|
+
start
|
42
|
+
"""
|
43
|
+
When I run `ruby test.rb`
|
44
|
+
Then the output should contain:
|
45
|
+
"""
|
46
|
+
foo 1 {:foo=>1}
|
47
|
+
foo 2 {:foo=>1}
|
48
|
+
"""
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require "eventmachine"
|
2
|
+
require "observed/eventmachine/version"
|
3
|
+
|
4
|
+
module Observed
|
5
|
+
module EM
|
6
|
+
|
7
|
+
# Schedule the observation to run it periodically
|
8
|
+
# @param [Float|Fixnum] seconds
|
9
|
+
# @param [Hash] args
|
10
|
+
# @option args [String] :run The tag of the observation to schedule running. It is the one registered via the code
|
11
|
+
# `observe tag, via: 'observer_plugin_name'`.
|
12
|
+
def every(seconds, args)
|
13
|
+
@builder.every seconds, args
|
14
|
+
end
|
15
|
+
|
16
|
+
# Start EventMachine and run scheduled observations.
|
17
|
+
def start
|
18
|
+
::EM.run do
|
19
|
+
Signal.trap("INT") { ::EventMachine.stop }
|
20
|
+
Signal.trap("TERM") { ::EventMachine.stop }
|
21
|
+
@builder.periodic_tasks.each do |periodic_task|
|
22
|
+
::EM.add_periodic_timer(periodic_task.seconds) do
|
23
|
+
::EM.defer { run(periodic_task.tag) }
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# Automatically called on `extend Observed::EM`
|
30
|
+
def init_observed_em_builder
|
31
|
+
@builder = Builder.new
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.extended(klass)
|
35
|
+
klass.init_observed_em_builder
|
36
|
+
end
|
37
|
+
|
38
|
+
class Builder
|
39
|
+
|
40
|
+
# Schedule the observation to run it periodically.
|
41
|
+
# Scheduled observations can be obtained later by calling the method `periodic_tasks`.
|
42
|
+
# @param [Float|Fixnum] seconds The interval to run the observation, in seconds.
|
43
|
+
# @param [Hash] args
|
44
|
+
# @option args [String] :run The tag of the observation to schedule running. It is the one registered via the code
|
45
|
+
# `observe tag, via: 'observer_plugin_name'`.
|
46
|
+
def every(seconds, args)
|
47
|
+
periodic_tasks << PeriodicTask.new(seconds, args)
|
48
|
+
end
|
49
|
+
|
50
|
+
def periodic_tasks
|
51
|
+
@periodic_tasks ||= []
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
class PeriodicTask
|
56
|
+
attr_reader :seconds
|
57
|
+
attr_reader :args
|
58
|
+
|
59
|
+
def initialize(seconds, args)
|
60
|
+
@seconds = seconds
|
61
|
+
@args = args
|
62
|
+
end
|
63
|
+
|
64
|
+
def tag
|
65
|
+
args[:run]
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
end
|