pegasus 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +17 -0
- data/Rakefile +41 -0
- data/VERSION +1 -0
- data/bin/pegasus +4 -0
- data/lib/pegasus/service/class_methods.rb +68 -0
- data/lib/pegasus/service/instance_methods.rb +45 -0
- data/lib/pegasus/service.rb +26 -0
- data/lib/pegasus.rb +3 -0
- data/test/helper.rb +10 -0
- data/test/test_pegasus.rb +7 -0
- metadata +99 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Joe Edelman
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
= pegasus
|
2
|
+
|
3
|
+
Description goes here.
|
4
|
+
|
5
|
+
== Note on Patches/Pull Requests
|
6
|
+
|
7
|
+
* Fork the project.
|
8
|
+
* Make your feature addition or bug fix.
|
9
|
+
* Add tests for it. This is important so I don't break it in a
|
10
|
+
future version unintentionally.
|
11
|
+
* Commit, do not mess with rakefile, version, or history.
|
12
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
13
|
+
* Send me a pull request. Bonus points for topic branches.
|
14
|
+
|
15
|
+
== Copyright
|
16
|
+
|
17
|
+
Copyright (c) 2010 Joe Edelman. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "pegasus"
|
8
|
+
gem.summary = %Q{pegasus: unicorn-steady, redis-backed, shared-nothing worker processes}
|
9
|
+
gem.description = %Q{pegasus: unicorn-steady, redis-backed, shared-nothing worker processes}
|
10
|
+
gem.email = "joe@citizenlogistics.com"
|
11
|
+
gem.homepage = "http://github.com/citizenlogistics/pegasus"
|
12
|
+
gem.authors = ["Joe Edelman"]
|
13
|
+
gem.add_dependency 'configurer'
|
14
|
+
gem.add_dependency 'unicorn_horn'
|
15
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
16
|
+
end
|
17
|
+
Jeweler::GemcutterTasks.new
|
18
|
+
rescue LoadError
|
19
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
20
|
+
end
|
21
|
+
|
22
|
+
require 'rake/testtask'
|
23
|
+
Rake::TestTask.new(:test) do |test|
|
24
|
+
test.libs << 'lib' << 'test'
|
25
|
+
test.pattern = 'test/**/test_*.rb'
|
26
|
+
test.verbose = true
|
27
|
+
end
|
28
|
+
|
29
|
+
task :test => :check_dependencies
|
30
|
+
|
31
|
+
task :default => :test
|
32
|
+
|
33
|
+
require 'rake/rdoctask'
|
34
|
+
Rake::RDocTask.new do |rdoc|
|
35
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
36
|
+
|
37
|
+
rdoc.rdoc_dir = 'rdoc'
|
38
|
+
rdoc.title = "pegasus #{version}"
|
39
|
+
rdoc.rdoc_files.include('README*')
|
40
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
41
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
data/bin/pegasus
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
module Pegasus
|
2
|
+
module ClassMethods
|
3
|
+
def instance; @instance ||= new; end
|
4
|
+
|
5
|
+
extend Forwardable
|
6
|
+
def_delegators :redis, :rpush, :llen, :lindex, :get, :set, :del, :lpop, :incrby
|
7
|
+
|
8
|
+
def playout svcs = nil
|
9
|
+
@svcs = svcs.map{ |s| s.instance } if svcs
|
10
|
+
true while @svcs.any?{ |s| s.play }
|
11
|
+
end
|
12
|
+
|
13
|
+
def blpop key, timeout = 0
|
14
|
+
true while (x = blocking_redis.blpop(key, 10)).blank?
|
15
|
+
x
|
16
|
+
end
|
17
|
+
|
18
|
+
def queue_with_ticket(ticket, *a)
|
19
|
+
a[0] = "do_#{a[0]}"
|
20
|
+
logger.debug "queueing #{a.inspect}"
|
21
|
+
rpush wait_queue_key, "#{Time.unix} #{ticket || '-'} #{serializer.dump(a)}"
|
22
|
+
trouble?
|
23
|
+
end
|
24
|
+
|
25
|
+
def next_ticket_no
|
26
|
+
if !@max or @current && @current >= @max
|
27
|
+
@max = incrby tickets_key, 1000
|
28
|
+
@min = @max - 1000
|
29
|
+
@current = @min
|
30
|
+
end
|
31
|
+
@current += 1
|
32
|
+
"#{ticket_prefix}#{@current}"
|
33
|
+
end
|
34
|
+
|
35
|
+
def wait ticket_no
|
36
|
+
@instance.playout if @instance
|
37
|
+
logger.debug "waiting on ticket #{ticket_no}"
|
38
|
+
blpop ticket_no, 0
|
39
|
+
del ticket_no
|
40
|
+
end
|
41
|
+
|
42
|
+
def track *a
|
43
|
+
ticket_no = next_ticket_no
|
44
|
+
queue_with_ticket(ticket_no, *a)
|
45
|
+
ticket_no
|
46
|
+
end
|
47
|
+
|
48
|
+
def method_missing(*a)
|
49
|
+
super unless method_defined? "do_#{a.first}"
|
50
|
+
raise "blocks are not allowed for queued methods" if block_given?
|
51
|
+
queue_with_ticket(nil, *a)
|
52
|
+
end
|
53
|
+
|
54
|
+
def trouble?
|
55
|
+
return false unless next_task = lindex(wait_queue_key, 0)
|
56
|
+
seconds_behind = Time.unix - next_task.split(' ', 2)[0].to_i
|
57
|
+
return false unless seconds_behind > 5
|
58
|
+
logger.warn "#{wait_queue_key} is running #{seconds_behind}s behind"
|
59
|
+
return false unless seconds_behind > 20
|
60
|
+
return false unless current_task = get(processing_key)
|
61
|
+
t, etc = current_task.split(' ', 2)
|
62
|
+
seconds = Time.unix - t.to_i
|
63
|
+
return false unless seconds > 20
|
64
|
+
logger.warn "has been #{etc ? "running task #{etc.inspect}" : "idle"} for #{seconds}s"
|
65
|
+
return true
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module Pegasus
|
2
|
+
class Service
|
3
|
+
module InstanceMethods
|
4
|
+
|
5
|
+
def run_task taskspec = nil
|
6
|
+
taskspec or begin
|
7
|
+
return unless pair = blpop(wait_queue_key, 10)
|
8
|
+
taskspec = pair[1]
|
9
|
+
end
|
10
|
+
set processing_key, "#{Time.unix} #{taskspec}"
|
11
|
+
queued_at, ticket_no, task_marshal = taskspec.split(' ', 3)
|
12
|
+
args = serializer.load(task_marshal)
|
13
|
+
logger.debug "running task #{args.inspect}"
|
14
|
+
result = "-"
|
15
|
+
begin
|
16
|
+
send(*args)
|
17
|
+
rescue Exception => e
|
18
|
+
logger.error("ERROR running task #{taskspec.inspect}: #{e.message}:: #{e.backtrace.join("\n\t")}")
|
19
|
+
result = serializer.dump(e)
|
20
|
+
raise e if raise_errors
|
21
|
+
end
|
22
|
+
unless ticket_no == '-'
|
23
|
+
logger.debug "posting ticket #{ticket_no}"
|
24
|
+
rpush ticket_no, result
|
25
|
+
end
|
26
|
+
set processing_key, Time.unix
|
27
|
+
end
|
28
|
+
|
29
|
+
alias_method :call, :run_task
|
30
|
+
|
31
|
+
def do_noop
|
32
|
+
logger.info "#{self.class.name} NOOP"
|
33
|
+
end
|
34
|
+
|
35
|
+
def play
|
36
|
+
while next_task = lpop(wait_queue_key)
|
37
|
+
run_task next_task
|
38
|
+
did_something = true
|
39
|
+
end
|
40
|
+
did_something
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'thread'
|
2
|
+
require 'pegasus/service/class_methods'
|
3
|
+
require 'pegasus/service/instance_methods'
|
4
|
+
|
5
|
+
module Pegasus
|
6
|
+
class Service
|
7
|
+
extend Configurer
|
8
|
+
config :redis do Thread.current[:redis] ||= ::Redis.new; end
|
9
|
+
config :blocking_redis do Thread.current[:blocking_redis] ||= ::Redis.new(:timeout=>0); end
|
10
|
+
config :logger do Logger.new(STDERR); end
|
11
|
+
config :serializer do Marshal; end
|
12
|
+
config :redis_prefix do "redis_svc_" end
|
13
|
+
config :wait_queue_key do "#{redis_prefix}wait_#{name}"; end
|
14
|
+
config :processing_key do "#{redis_prefix}process_#{name}"; end
|
15
|
+
config :tickets_key do "#{redis_prefix}tickets_#{name}"; end
|
16
|
+
config :ticket_prefix do "#{redis_prefix}ticket_#{name}_"; end
|
17
|
+
config :raise_errors do true; end
|
18
|
+
|
19
|
+
extend Forwardable
|
20
|
+
def_delegators :redis, :rpush, :llen, :lindex, :get, :set, :del, :lpop, :incrby, :blpop
|
21
|
+
|
22
|
+
extend ClassMethods
|
23
|
+
def_delegators 'self.class', :track, :wait
|
24
|
+
include InstanceMethods
|
25
|
+
end
|
26
|
+
end
|
data/lib/pegasus.rb
ADDED
data/test/helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pegasus
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: 0.0.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Joe Edelman
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-09-09 00:00:00 -04:00
|
18
|
+
default_executable: pegasus
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: configurer
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 0
|
29
|
+
version: "0"
|
30
|
+
type: :runtime
|
31
|
+
version_requirements: *id001
|
32
|
+
- !ruby/object:Gem::Dependency
|
33
|
+
name: unicorn_horn
|
34
|
+
prerelease: false
|
35
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
segments:
|
40
|
+
- 0
|
41
|
+
version: "0"
|
42
|
+
type: :runtime
|
43
|
+
version_requirements: *id002
|
44
|
+
description: "pegasus: unicorn-steady, redis-backed, shared-nothing worker processes"
|
45
|
+
email: joe@citizenlogistics.com
|
46
|
+
executables:
|
47
|
+
- pegasus
|
48
|
+
extensions: []
|
49
|
+
|
50
|
+
extra_rdoc_files:
|
51
|
+
- LICENSE
|
52
|
+
- README.rdoc
|
53
|
+
files:
|
54
|
+
- .document
|
55
|
+
- .gitignore
|
56
|
+
- LICENSE
|
57
|
+
- README.rdoc
|
58
|
+
- Rakefile
|
59
|
+
- VERSION
|
60
|
+
- bin/pegasus
|
61
|
+
- lib/pegasus.rb
|
62
|
+
- lib/pegasus/service.rb
|
63
|
+
- lib/pegasus/service/class_methods.rb
|
64
|
+
- lib/pegasus/service/instance_methods.rb
|
65
|
+
- test/helper.rb
|
66
|
+
- test/test_pegasus.rb
|
67
|
+
has_rdoc: true
|
68
|
+
homepage: http://github.com/citizenlogistics/pegasus
|
69
|
+
licenses: []
|
70
|
+
|
71
|
+
post_install_message:
|
72
|
+
rdoc_options:
|
73
|
+
- --charset=UTF-8
|
74
|
+
require_paths:
|
75
|
+
- lib
|
76
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
segments:
|
81
|
+
- 0
|
82
|
+
version: "0"
|
83
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
segments:
|
88
|
+
- 0
|
89
|
+
version: "0"
|
90
|
+
requirements: []
|
91
|
+
|
92
|
+
rubyforge_project:
|
93
|
+
rubygems_version: 1.3.6
|
94
|
+
signing_key:
|
95
|
+
specification_version: 3
|
96
|
+
summary: "pegasus: unicorn-steady, redis-backed, shared-nothing worker processes"
|
97
|
+
test_files:
|
98
|
+
- test/helper.rb
|
99
|
+
- test/test_pegasus.rb
|