immortalize 0.1.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 +52 -0
- data/VERSION +1 -0
- data/bin/immortalize +204 -0
- data/immortalize.gemspec +47 -0
- metadata +70 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 BehindLogic
|
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
|
+
= immortalize
|
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 BehindLogic. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "immortalize"
|
8
|
+
gem.summary = %Q{Restarts a specified process if it dies.}
|
9
|
+
gem.description = %Q{Watch a specific process, restart it if it dies.}
|
10
|
+
gem.email = "gems@behindlogic.com"
|
11
|
+
gem.homepage = "http://github.com/dcparker/immortalize"
|
12
|
+
gem.authors = ["BehindLogic"]
|
13
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
14
|
+
end
|
15
|
+
Jeweler::GemcutterTasks.new
|
16
|
+
rescue LoadError
|
17
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
18
|
+
end
|
19
|
+
|
20
|
+
require 'rake/testtask'
|
21
|
+
Rake::TestTask.new(:test) do |test|
|
22
|
+
test.libs << 'lib' << 'test'
|
23
|
+
test.pattern = 'test/**/test_*.rb'
|
24
|
+
test.verbose = true
|
25
|
+
end
|
26
|
+
|
27
|
+
begin
|
28
|
+
require 'rcov/rcovtask'
|
29
|
+
Rcov::RcovTask.new do |test|
|
30
|
+
test.libs << 'test'
|
31
|
+
test.pattern = 'test/**/test_*.rb'
|
32
|
+
test.verbose = true
|
33
|
+
end
|
34
|
+
rescue LoadError
|
35
|
+
task :rcov do
|
36
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
task :test => :check_dependencies
|
41
|
+
|
42
|
+
task :default => :test
|
43
|
+
|
44
|
+
require 'rake/rdoctask'
|
45
|
+
Rake::RDocTask.new do |rdoc|
|
46
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
47
|
+
|
48
|
+
rdoc.rdoc_dir = 'rdoc'
|
49
|
+
rdoc.title = "immortalize #{version}"
|
50
|
+
rdoc.rdoc_files.include('README*')
|
51
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
52
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.1
|
data/bin/immortalize
ADDED
@@ -0,0 +1,204 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
# Usage:
|
4
|
+
# immortalize run "command --with-args --etc"
|
5
|
+
# immortalize remove "command --with-args --etc"
|
6
|
+
# immortalize inspect
|
7
|
+
# immortalize # << run this from a cron job every minute to check and restart failed processes.
|
8
|
+
# * starts process
|
9
|
+
# * logs the start command with pid
|
10
|
+
# * when run from cron (without args), check all logged start commands and currently logged pid to see if it's running
|
11
|
+
# * if not running, immediately remove pid from log, but leave command; start process again via: immortalize "command --with-args --etc"
|
12
|
+
require 'time'
|
13
|
+
require 'yaml'
|
14
|
+
require 'sha1'
|
15
|
+
require 'optparse'
|
16
|
+
$options = {}
|
17
|
+
|
18
|
+
require 'rubygems'
|
19
|
+
require 'merb-core'
|
20
|
+
require 'merb-mailer'
|
21
|
+
Merb::Mailer.delivery_method = :sendmail
|
22
|
+
|
23
|
+
$log_location = "/var/log/immortalize"
|
24
|
+
`mkdir -p "#{$log_location}"` unless File.directory?($log_location)
|
25
|
+
$registry_filename = "#{$log_location}/registry.yaml"
|
26
|
+
File.open($registry_filename, 'w'){|f| f << {}.to_yaml} unless File.exists?($registry_filename)
|
27
|
+
$registry = YAML.load_file($registry_filename)
|
28
|
+
|
29
|
+
optparse = OptionParser.new do |opts|
|
30
|
+
opts.banner = <<-ENDBANNER
|
31
|
+
Usage: #{$0} [run|remove|inspect] [options]
|
32
|
+
|
33
|
+
To add (and start) a command:
|
34
|
+
#{$0} run "command" --notification_recipient admin@email.com --max_failures 5
|
35
|
+
To change a command's options, just re-add it.
|
36
|
+
|
37
|
+
To remove a command:
|
38
|
+
#{$0} remove "command"
|
39
|
+
|
40
|
+
To inspect the current list of immortal commands:
|
41
|
+
#{$0} list
|
42
|
+
|
43
|
+
Run this command with no arguments as a cron job, to run every minute:
|
44
|
+
* * * * * immortalize
|
45
|
+
ENDBANNER
|
46
|
+
|
47
|
+
# $options[:notification_recipient] = nil
|
48
|
+
opts.on( '--notification_recipient=[EMAIL]', "The email address to which failure notifications should be sent." ) do |email|
|
49
|
+
$options[:notification_recipient] = email
|
50
|
+
end
|
51
|
+
|
52
|
+
$options[:max_failures] = 5
|
53
|
+
opts.on('--max_failures=[NUM]', "Notify on NUM or more failures within an hour (default 5)") do |num|
|
54
|
+
$options[:max_failures] = num.to_i
|
55
|
+
end
|
56
|
+
|
57
|
+
opts.on( '-h', '--help', 'Display this screen' ) do
|
58
|
+
puts opts
|
59
|
+
exit
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
optparse.parse!
|
64
|
+
$action = ARGV[0]
|
65
|
+
|
66
|
+
def notify(immortal, message)
|
67
|
+
m = Merb::Mailer.new(
|
68
|
+
:to => immortal[:notification_recipient],
|
69
|
+
:from => "immortalize@video.iremix.org",
|
70
|
+
:subject => "ImmortalCommand `#{immortal[:command]}' keeps dying!",
|
71
|
+
:text => message
|
72
|
+
)
|
73
|
+
m.deliver!
|
74
|
+
end
|
75
|
+
|
76
|
+
class Time
|
77
|
+
def beginning_of_day
|
78
|
+
Time.mktime(year, month, day).send(gmt? ? :gmt : :localtime)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
class Immortal
|
83
|
+
attr_reader :identifier
|
84
|
+
def initialize(identifier)
|
85
|
+
@identifier = identifier
|
86
|
+
@reg = $registry[identifier]
|
87
|
+
end
|
88
|
+
|
89
|
+
def [](key)
|
90
|
+
@reg[key]
|
91
|
+
end
|
92
|
+
|
93
|
+
def running?
|
94
|
+
@reg[:pid] && `ps #{@reg[:pid]} | grep "#{@reg[:pid]}" | grep -v "grep"` =~ /#{@reg[:pid]}/
|
95
|
+
end
|
96
|
+
|
97
|
+
def failures
|
98
|
+
@failures ||= File.exists?(failure_log_file) ? File.readlines(failure_log_file).map {|t| Time.parse(t) } : []
|
99
|
+
end
|
100
|
+
|
101
|
+
def start!
|
102
|
+
# Run the command and gather the pid
|
103
|
+
pid = nil
|
104
|
+
open("|#{@reg[:command]} & echo $!") do |f|
|
105
|
+
pid = f.sysread(5).chomp.to_i
|
106
|
+
end
|
107
|
+
# Log the pid
|
108
|
+
puts "pid #{pid}"
|
109
|
+
$registry[identifier][:pid] = pid
|
110
|
+
end
|
111
|
+
|
112
|
+
def failed!
|
113
|
+
failures << Time.now
|
114
|
+
File.open(failure_log_file, 'a') do |log|
|
115
|
+
log << "#{Time.now}\n"
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
def frequent_failures?
|
120
|
+
# If it failed :max_failures times or more within the last hour.
|
121
|
+
failures.length >= self[:max_failures] && failures[0-self[:max_failures]] > Time.now - 3600
|
122
|
+
end
|
123
|
+
def failures_today
|
124
|
+
failures.select {|f| f > Time.now.beginning_of_day}
|
125
|
+
end
|
126
|
+
def failures_this_hour
|
127
|
+
failures_today.select {|f| f > Time.now - 3600}
|
128
|
+
end
|
129
|
+
|
130
|
+
def inspect
|
131
|
+
self[:command] + (failures.length >= self[:max_failures].to_i ? "\n\tLast #{self[:max_failures]} failures: #{failures[-5..1]}" : '')
|
132
|
+
end
|
133
|
+
|
134
|
+
private
|
135
|
+
def failure_log_file
|
136
|
+
"#{$log_location}/#{@identifier}"
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
# Main logic
|
141
|
+
unless ::Object.const_defined?(:IRB)
|
142
|
+
case $action
|
143
|
+
when 'list'
|
144
|
+
puts $registry.inspect
|
145
|
+
puts "Immortalized:"
|
146
|
+
$registry.each_key do |identifier|
|
147
|
+
puts "\t+ " + Immortal.new(identifier).inspect
|
148
|
+
end
|
149
|
+
exit
|
150
|
+
|
151
|
+
when 'run'
|
152
|
+
# Running with a given command.
|
153
|
+
command_string = ARGV[1]
|
154
|
+
identifier = SHA1.hexdigest(command_string)
|
155
|
+
|
156
|
+
# Create the command
|
157
|
+
$registry[identifier] ||= {
|
158
|
+
:command => command_string
|
159
|
+
}
|
160
|
+
$registry[identifier].merge!($options)
|
161
|
+
|
162
|
+
immortal = Immortal.new(identifier)
|
163
|
+
# Start the process if it isn't already running
|
164
|
+
if immortal.running?
|
165
|
+
puts "`#{immortal[:command]}' is already running with pid #{immortal[:pid]}"
|
166
|
+
else
|
167
|
+
print "Starting `#{immortal[:command]}'... "
|
168
|
+
immortal.start!
|
169
|
+
end
|
170
|
+
|
171
|
+
when 'remove'
|
172
|
+
command_string = ARGV[1]
|
173
|
+
identifier = SHA1.hexdigest(command_string)
|
174
|
+
$registry.delete(identifier)
|
175
|
+
|
176
|
+
when nil
|
177
|
+
# Running bare from cron.
|
178
|
+
# Check all logged commands with pids.
|
179
|
+
$registry.each do |identifier,info|
|
180
|
+
immortal = Immortal.new(identifier)
|
181
|
+
|
182
|
+
# Check if running
|
183
|
+
if immortal.running?
|
184
|
+
puts "`#{immortal[:command]}' is running fine..."
|
185
|
+
else
|
186
|
+
puts "`#{immortal[:command]}' HAS DIED! Reviving..."
|
187
|
+
# Mark the failure
|
188
|
+
immortal.failed!
|
189
|
+
# Notify if failures have been frequent
|
190
|
+
if immortal.frequent_failures?
|
191
|
+
puts "! FREQUENT FAILURE ON #{identifier} (`#{immortal[:command]}')"
|
192
|
+
notify(immortal, "ImmortalCommand failure!\n\nCommand `#{immortal[:command]}' failed, threshold is #{immortal[:max_failures]} / hour.\n\n#{immortal.failures_today.size} failures so far today, #{immortal.failures_this_hour.size} in the past hour.")
|
193
|
+
end
|
194
|
+
# Start it
|
195
|
+
immortal.start!
|
196
|
+
end
|
197
|
+
end
|
198
|
+
end
|
199
|
+
end
|
200
|
+
|
201
|
+
# Save the registry
|
202
|
+
File.open("#{$log_location}/registry.yaml", 'w') do |r|
|
203
|
+
r << $registry.to_yaml
|
204
|
+
end
|
data/immortalize.gemspec
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{immortalize}
|
8
|
+
s.version = "0.1.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["BehindLogic"]
|
12
|
+
s.date = %q{2010-03-09}
|
13
|
+
s.default_executable = %q{immortalize}
|
14
|
+
s.description = %q{Watch a specific process, restart it if it dies.}
|
15
|
+
s.email = %q{gems@behindlogic.com}
|
16
|
+
s.executables = ["immortalize"]
|
17
|
+
s.extra_rdoc_files = [
|
18
|
+
"LICENSE",
|
19
|
+
"README.rdoc"
|
20
|
+
]
|
21
|
+
s.files = [
|
22
|
+
".document",
|
23
|
+
".gitignore",
|
24
|
+
"LICENSE",
|
25
|
+
"README.rdoc",
|
26
|
+
"Rakefile",
|
27
|
+
"VERSION",
|
28
|
+
"bin/immortalize",
|
29
|
+
"immortalize.gemspec"
|
30
|
+
]
|
31
|
+
s.homepage = %q{http://github.com/dcparker/immortalize}
|
32
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
33
|
+
s.require_paths = ["lib"]
|
34
|
+
s.rubygems_version = %q{1.3.6}
|
35
|
+
s.summary = %q{Restarts a specified process if it dies.}
|
36
|
+
|
37
|
+
if s.respond_to? :specification_version then
|
38
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
39
|
+
s.specification_version = 3
|
40
|
+
|
41
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
42
|
+
else
|
43
|
+
end
|
44
|
+
else
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
metadata
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: immortalize
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 1
|
9
|
+
version: 0.1.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- BehindLogic
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-03-09 00:00:00 -05:00
|
18
|
+
default_executable: immortalize
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: Watch a specific process, restart it if it dies.
|
22
|
+
email: gems@behindlogic.com
|
23
|
+
executables:
|
24
|
+
- immortalize
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files:
|
28
|
+
- LICENSE
|
29
|
+
- README.rdoc
|
30
|
+
files:
|
31
|
+
- .document
|
32
|
+
- .gitignore
|
33
|
+
- LICENSE
|
34
|
+
- README.rdoc
|
35
|
+
- Rakefile
|
36
|
+
- VERSION
|
37
|
+
- bin/immortalize
|
38
|
+
- immortalize.gemspec
|
39
|
+
has_rdoc: true
|
40
|
+
homepage: http://github.com/dcparker/immortalize
|
41
|
+
licenses: []
|
42
|
+
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options:
|
45
|
+
- --charset=UTF-8
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
segments:
|
53
|
+
- 0
|
54
|
+
version: "0"
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
segments:
|
60
|
+
- 0
|
61
|
+
version: "0"
|
62
|
+
requirements: []
|
63
|
+
|
64
|
+
rubyforge_project:
|
65
|
+
rubygems_version: 1.3.6
|
66
|
+
signing_key:
|
67
|
+
specification_version: 3
|
68
|
+
summary: Restarts a specified process if it dies.
|
69
|
+
test_files: []
|
70
|
+
|