runciter 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
File without changes
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2012 Chris Kite
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
File without changes
@@ -0,0 +1,26 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rspec/core/rake_task'
4
+
5
+ desc "Run all specs"
6
+ RSpec::Core::RakeTask.new(:rspec) do |spec|
7
+ spec.pattern = 'spec/**/*_spec.rb'
8
+ end
9
+
10
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
11
+ spec.pattern = 'spec/**/*_spec.rb'
12
+ spec.rcov = true
13
+ end
14
+
15
+ task :default => :rspec
16
+
17
+ require 'rdoc/task'
18
+
19
+ Rake::RDocTask.new do |rdoc|
20
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
21
+
22
+ rdoc.rdoc_dir = 'rdoc'
23
+ rdoc.title = "runciter #{version}"
24
+ rdoc.rdoc_files.include('README*')
25
+ rdoc.rdoc_files.include('lib/**/*.rb')
26
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,4 @@
1
+ require 'jimson'
2
+ require 'runciter/app'
3
+ require 'runciter/task'
4
+ require 'runciter/run'
@@ -0,0 +1,34 @@
1
+ module Runciter
2
+ class App
3
+ attr_reader :id,
4
+ :api
5
+
6
+ def initialize(name, endpoint, opts = {})
7
+ @name = name
8
+ @endpoint = URI(endpoint)
9
+ @opts = opts
10
+
11
+ @api = Jimson::Client.new(endpoint)
12
+
13
+ register!
14
+ end
15
+
16
+ def task(name, opts = {}, &block)
17
+ task = Task.new(self, name, opts)
18
+ task.run!(&block)
19
+ end
20
+
21
+ protected
22
+
23
+ def register!
24
+ doc = @api[:apps].create(@name)
25
+ @id = doc['_id']
26
+ if cron_str = @opts.delete(:cron)
27
+ @api[:apps].cron(@id, cron_str)
28
+ end
29
+ rescue
30
+ warn $!
31
+ end
32
+
33
+ end
34
+ end
@@ -0,0 +1,55 @@
1
+ require 'thread'
2
+
3
+ module Runciter
4
+ class Run
5
+ attr_reader :id
6
+
7
+ def initialize(app, task, opts = {})
8
+ @app, @task = app, task
9
+ @opts = opts
10
+ @opts[:heartbeat_interval] ||= 30
11
+
12
+ register!
13
+ run_heartbeat_thread!
14
+ end
15
+
16
+ def step!(num = 1)
17
+ @app.api[:runs].incr_step(@id, num)
18
+ rescue
19
+ warn $!
20
+ end
21
+
22
+ def finish!
23
+ @app.api[:runs].finish(@id)
24
+ @heart.terminate
25
+ rescue
26
+ warn $!
27
+ end
28
+
29
+ def die!(e)
30
+ @app.api[:runs].die(@id, [e.message, e.backtrace].join("\n"))
31
+ @heart.terminate
32
+ rescue
33
+ warn $!
34
+ end
35
+
36
+ protected
37
+
38
+ def register!
39
+ doc = @app.api[:runs].start(@task.id, @opts)
40
+ @id = doc['_id']
41
+ rescue
42
+ warn $!
43
+ end
44
+
45
+ def run_heartbeat_thread!
46
+ @heart = Thread.new do
47
+ @app.api[:runs].heartbeat(@id)
48
+ sleep @opts[:heartbeat_interval]
49
+ end
50
+ rescue
51
+ warn $!
52
+ end
53
+
54
+ end
55
+ end
@@ -0,0 +1,35 @@
1
+ module Runciter
2
+ class Task
3
+ attr_reader :name
4
+ attr_reader :id
5
+
6
+ def initialize(app, name, opts = {})
7
+ @app = app
8
+ @name = name
9
+ @opts = opts
10
+
11
+ register!
12
+ end
13
+
14
+ def run!(&block)
15
+ run = Run.new(@app, self, @opts)
16
+
17
+ begin
18
+ block.call(run)
19
+ run.finish! # finish! rescues its own exceptions
20
+ rescue
21
+ run.die!($!) # die! rescues its own exceptions
22
+ end
23
+ end
24
+
25
+ protected
26
+
27
+ def register!
28
+ doc = @app.api[:tasks].create(@name, @app.id)
29
+ @id = doc['_id']
30
+ rescue
31
+ warn $!
32
+ end
33
+
34
+ end
35
+ end
@@ -0,0 +1,12 @@
1
+ require 'spec_helper'
2
+
3
+ module Runciter
4
+ describe App do
5
+ it 'works' do
6
+ app = App.new('Spec App', SPEC_URL, :cron => '30 * * * *')
7
+ app.task('Try Things', :steps => 10) do |r|
8
+ 10.times { sleep 1; r.step! }
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,6 @@
1
+ require 'rubygems'
2
+ $:.unshift(File.dirname(__FILE__) + '/../lib/')
3
+ require 'bundler/setup'
4
+ require 'runciter'
5
+
6
+ SPEC_URL = 'http://localhost:3000/api'
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: runciter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Chris Kite
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: jimson
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 0.9.1
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 0.9.1
30
+ description:
31
+ email:
32
+ executables: []
33
+ extensions: []
34
+ extra_rdoc_files:
35
+ - README.md
36
+ files:
37
+ - VERSION
38
+ - LICENSE.txt
39
+ - CHANGELOG.md
40
+ - README.md
41
+ - Rakefile
42
+ - lib/runciter/task.rb
43
+ - lib/runciter/run.rb
44
+ - lib/runciter/app.rb
45
+ - lib/runciter.rb
46
+ - spec/spec_helper.rb
47
+ - spec/app_spec.rb
48
+ homepage: http://www.github.com/chriskite/runciter
49
+ licenses: []
50
+ post_install_message:
51
+ rdoc_options: []
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ! '>='
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubyforge_project:
68
+ rubygems_version: 1.8.21
69
+ signing_key:
70
+ specification_version: 3
71
+ summary: Ruby client for the Runciter task monitor
72
+ test_files:
73
+ - spec/spec_helper.rb
74
+ - spec/app_spec.rb