README.md 4.31 KB
Newer Older
1
# Examples
John E. Vincent's avatar
John E. Vincent committed
2
The following is a list of notes regarding the examples in this directory.
John E. Vincent's avatar
John E. Vincent committed
3

John E. Vincent's avatar
John E. Vincent committed
4
## General Requirements
John E. Vincent's avatar
John E. Vincent committed
5 6 7

You'll need a few additional gems

John E. Vincent's avatar
John E. Vincent committed
8
* [em-hiredis](https://github.com/mloughran/em-hiredis)
John E. Vincent's avatar
John E. Vincent committed
9
 You'll have to compile/install from source. Sorry. Should pull in the `hiredis` native ext.
John E. Vincent's avatar
John E. Vincent committed
10
* [em-http-request](https://github.com/igrigorik/em-http-request)
John E. Vincent's avatar
John E. Vincent committed
11
 Available via rubygems
John E. Vincent's avatar
John E. Vincent committed
12 13 14 15 16 17 18 19 20 21
* [em-websocket](https://github.com/igrigorik/em-websocket)
 Available via rubygems

## custom-watcher.rb
This is an idea I'm tossing around for allowing easy custom watchers to be written.
Essentially the idea is that you tap into the Redis subscription with a defined pattern and a destination.

### Example

	require './watcher-idea.rb'
22 23

	Noah::Watcher.watch do
24
	  pattern "//noah/configuration/*"
25 26
	  destination Proc.new {|x| something_with(x)}
	  run!
John E. Vincent's avatar
John E. Vincent committed
27 28 29 30 31 32 33 34
	end

## logger.rb
An example using logger as a watcher. Pretty straighforward.


## httpclient.rb/httpclient-server.rb
This is an example of how the Webhook system would work
John E. Vincent's avatar
John E. Vincent committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68

### Running

To get the maximum effect, start with a clean Redis database

* Start the webhook reciever

	noah/examples$ ruby httpclient-server.rb 
	== Sinatra/1.1.2 has taken the stage on 4567 for development with backup from Thin
	>> Thin web server (v1.2.7 codename No Hup)
	>> Maximum connections set to 1024
	>> Listening on 0.0.0.0:4567, CTRL+C to stop

* Start the webhook publisher

	noah/examples$ ruby httpclient.rb

* Run the rake sample script

In the publisher window, you should see some messages like so:

	Got message for noah.Host[localhost].create
	Got message for noah.Host[localhost].save
	Got message for noah.Host[localhost].save
	Got message for noah.Host[localhost].update

In the server window, you should see the following:

	"{\"id\":\"1\",\"name\":\"localhost\",\"status\":\"up\",\"created_at\":\"2011-02-15 05:19:05 UTC\",\"updated_at\":\"2011-02-15 05:19:05 UTC\",\"services\":[]}"
	127.0.0.1 - - [15/Feb/2011 00:19:05] "POST /webhook HTTP/1.1" 200 135 0.0024
	"{\"id\":\"1\",\"name\":\"localhost\",\"status\":\"up\",\"created_at\":\"2011-02-15 05:19:05 UTC\",\"updated_at\":\"2011-02-15 05:19:05 UTC\",\"services\":[]}"
	127.0.0.1 - - [15/Feb/2011 00:19:05] "POST /webhook HTTP/1.1" 200 135 0.0004
	"{\"id\":\"1\",\"name\":\"localhost\",\"status\":\"up\",\"created_at\":\"2011-02-15 05:19:05 UTC\",\"updated_at\":\"2011-02-15 05:19:05 UTC\",\"services\":[]}"

69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116

## websocket.rb
This is an example of using Websockets, EventMachine and Redis PubSub to provide a "status" console of sorts.

### Running

To get the maximum effect, start with a clean Redis database.

* Start the server:

	~/development/noah/examples$ ./websocket.rb 
	>> Thin web server (v1.2.7 codename No Hup)
	>> Maximum connections set to 1024
	>> Listening on 0.0.0.0:3000, CTRL+C to stop

You should be able to load up the "normal" Noah sample page on [http://localhost:3000].

* Load the "websocket" file

In another browser window, open the `websocket.html` file.

* Send a message

From another terminal window send the following:

	curl -X PUT -d '{"name":"testhost2","status":"down"}' http://localhost:3000/h/testhost2

You should see the message come across in the browser window like so:

	connected...

	2 connected and waiting....

	(noah.Host[testhost2].create) {"id":"1","name":"testhost2","status":"down","created_at":"2011-02-14 20:58:04 UTC","updated_at":"2011-02-14 20:58:04 UTC","services":[]}

	(noah.Host[testhost2].save) {"id":"1","name":"testhost2","status":"down","created_at":"2011-02-14 20:58:04 UTC","updated_at":"2011-02-14 20:58:04 UTC","services":[]}

	(noah.Host[testhost2].save) {"id":"1","name":"testhost2","status":"down","created_at":"2011-02-14 20:58:04 UTC","updated_at":"2011-02-14 20:58:04 UTC","services":[]}

	(noah.Host[testhost2].update) {"id":"1","name":"testhost2","status":"down","created_at":"2011-02-14 20:58:04 UTC","updated_at":"2011-02-14 20:58:04 UTC","services":[]}

You can see the Watcher pattern in the parenthesis and then the JSON message body.

For fun, refresh the page to clear it and then run the sample data population rake task.

### Known issues
When I started working on the Watcher stuff, I realized that I'm sending A LOT of extranous messages. These are mostly the result of the way I'm creating new objects with Ohm (i.e. via `.create`).
I'll be cleaning that up and trying to get down to a single message per operation.