healthety 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ *.gem
2
+ .bundle
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+
3
+ gemspec
data/README.md ADDED
@@ -0,0 +1,26 @@
1
+ # Ruby Worker
2
+
3
+ The Ruby Worker sends JSON data via UDP packets to a given host at a defined interval.
4
+
5
+ ### Installation
6
+
7
+ gem install healthety
8
+
9
+ ### Usage
10
+
11
+ require "healthety"
12
+
13
+ Healthety.workers do
14
+ host "127.0.0.1"
15
+ port 8124
16
+
17
+ worker :load_average do
18
+ interval 0.5
19
+ value rand(10)
20
+ end
21
+
22
+ worker :memory do
23
+ interval 2
24
+ value rand(10)
25
+ end
26
+ end
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "rake/testtask"
2
+
3
+ Rake::TestTask.new(:spec) do |spec|
4
+ spec.libs << "lib" << "spec"
5
+ spec.pattern = "spec/**/*_spec.rb"
6
+ spec.verbose = true
7
+ end
8
+
9
+ task :default => :spec
data/healthety.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "healthety/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "healthety"
7
+ s.version = Healthety::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = "Martin Jagusch"
10
+ s.email = "m@martinjagusch.com"
11
+ s.homepage = "http://github.com/healthety/ruby_worker"
12
+ s.description = "The Ruby Worker sends JSON data via UDP packets to a given host."
13
+ s.summary = s.description
14
+
15
+ s.add_development_dependency("rspec", "~> 2.5.0")
16
+ s.add_development_dependency("mocha")
17
+
18
+ s.files = `git ls-files`.split("\n")
19
+ s.test_files = `git ls-files -- spec/*`.split("\n")
20
+ s.require_paths = ["lib"]
21
+ end
@@ -0,0 +1,51 @@
1
+ module Healthety
2
+ extend self
3
+
4
+ def workers(&block)
5
+ @workers = []
6
+ @threads = []
7
+
8
+ instance_eval(&block)
9
+
10
+ start
11
+ end
12
+
13
+ def host(host)
14
+ @host = host
15
+ end
16
+
17
+ def port(port)
18
+ @port = port
19
+ end
20
+
21
+ def worker(name, &block)
22
+ @workers << Worker.new(name, &block)
23
+ end
24
+
25
+ def start
26
+ puts message
27
+ transmission = Transmission.new(@host, @port)
28
+
29
+ # Catch Ctrl-C and terminate all worker threads.
30
+ trap("INT") { @threads.map(&:kill) }
31
+
32
+ @workers.each do |worker|
33
+ @threads << Thread.new do
34
+ loop do
35
+ worker.perform
36
+ transmission.send(worker.name, worker.value)
37
+ sleep worker.interval
38
+ end
39
+ end
40
+ end
41
+
42
+ @threads.map(&:join)
43
+ end
44
+
45
+ def message
46
+ <<-EOM
47
+ => Workers starting to send to #{@host}:#{@port}
48
+ => Ctrl-C to stop
49
+ EOM
50
+ end
51
+ end
@@ -0,0 +1,18 @@
1
+ require "socket"
2
+ require "json"
3
+
4
+ module Healthety
5
+ class Transmission
6
+ def initialize(host, port)
7
+ @host = host
8
+ @port = port
9
+ @socket = UDPSocket.new
10
+ end
11
+
12
+ def send(name, value)
13
+ data = {:name => name, :value => value}.to_json
14
+ @socket.send(data, 0, @host, @port)
15
+ $stdout << "#{name}: #{value}\n"
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,3 @@
1
+ module Healthety
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,22 @@
1
+ module Healthety
2
+ class Worker
3
+ attr_reader :name
4
+
5
+ def initialize(name, &block)
6
+ @name = name.to_s
7
+ @block = block
8
+ end
9
+
10
+ def interval(interval = nil)
11
+ @interval = interval || @interval
12
+ end
13
+
14
+ def value(value = nil)
15
+ @value = value || @value
16
+ end
17
+
18
+ def perform
19
+ instance_eval(&@block)
20
+ end
21
+ end
22
+ end
data/lib/healthety.rb ADDED
@@ -0,0 +1,3 @@
1
+ require "healthety/healthety"
2
+ require "healthety/transmission"
3
+ require "healthety/worker"
@@ -0,0 +1,7 @@
1
+ require "healthety"
2
+ require "rspec"
3
+
4
+ RSpec.configure do |config|
5
+ config.mock_with :mocha
6
+ config.color_enabled = true
7
+ end
@@ -0,0 +1,23 @@
1
+ require "spec_helper"
2
+
3
+ module Healthety
4
+ describe Healthety do
5
+ it "should start" do
6
+ $stdout = StringIO.new
7
+ Thread.any_instance.stubs(:join)
8
+ UDPSocket.any_instance.stubs(:send)
9
+
10
+ expect do
11
+ Healthety.workers do
12
+ host "127.0.0.1"
13
+ port 8124
14
+
15
+ worker :test do
16
+ interval 0.5
17
+ value rand(10)
18
+ end
19
+ end
20
+ end.to be_true
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,15 @@
1
+ require "spec_helper"
2
+
3
+ module Healthety
4
+ describe Transmission do
5
+ it "should send UDP packets" do
6
+ $stdout = StringIO.new
7
+ transmission = Transmission.new("127.0.0.1", 8124)
8
+
9
+ transmission.instance_variable_get(:@socket).stubs(:send)
10
+ transmission.expects(:send).at_least_once
11
+
12
+ transmission.send("test", 5)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,28 @@
1
+ require "spec_helper"
2
+
3
+ module Healthety
4
+ describe Worker do
5
+ before(:each) do
6
+ @block = Proc.new { interval 0.5; value 10 }
7
+ @worker = Worker.new(:test, &@block)
8
+ end
9
+
10
+ it "should initialize" do
11
+ @worker.name.should eq("test")
12
+ @worker.instance_variable_get(:@block).should be(@block)
13
+ end
14
+
15
+ it "should perform" do
16
+ @worker.perform
17
+
18
+ @worker.interval.should eq(0.5)
19
+ @worker.value.should eq(10)
20
+
21
+ @worker.interval 0.7
22
+ @worker.value 11
23
+
24
+ @worker.interval.should eq(0.7)
25
+ @worker.value.should eq(11)
26
+ end
27
+ end
28
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: healthety
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Martin Jagusch
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-04-13 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ prerelease: false
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ~>
22
+ - !ruby/object:Gem::Version
23
+ version: 2.5.0
24
+ type: :development
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: mocha
28
+ prerelease: false
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: "0"
35
+ type: :development
36
+ version_requirements: *id002
37
+ description: The Ruby Worker sends JSON data via UDP packets to a given host.
38
+ email: m@martinjagusch.com
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files: []
44
+
45
+ files:
46
+ - .gitignore
47
+ - Gemfile
48
+ - README.md
49
+ - Rakefile
50
+ - healthety.gemspec
51
+ - lib/healthety.rb
52
+ - lib/healthety/healthety.rb
53
+ - lib/healthety/transmission.rb
54
+ - lib/healthety/version.rb
55
+ - lib/healthety/worker.rb
56
+ - spec/spec_helper.rb
57
+ - spec/unit/healthety_spec.rb
58
+ - spec/unit/transmission_spec.rb
59
+ - spec/unit/worker_spec.rb
60
+ homepage: http://github.com/healthety/ruby_worker
61
+ licenses: []
62
+
63
+ post_install_message:
64
+ rdoc_options: []
65
+
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: "0"
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: "0"
80
+ requirements: []
81
+
82
+ rubyforge_project:
83
+ rubygems_version: 1.7.2
84
+ signing_key:
85
+ specification_version: 3
86
+ summary: The Ruby Worker sends JSON data via UDP packets to a given host.
87
+ test_files:
88
+ - spec/spec_helper.rb
89
+ - spec/unit/healthety_spec.rb
90
+ - spec/unit/transmission_spec.rb
91
+ - spec/unit/worker_spec.rb