with_pid 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,18 @@
1
+ Copyright (c) 2009 Winton Welsh
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
4
+ this software and associated documentation files (the "Software"), to deal in
5
+ the Software without restriction, including without limitation the rights to
6
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
7
+ the Software, and to permit persons to whom the Software is furnished to do so,
8
+ subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ 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, FITNESS
15
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
16
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.markdown ADDED
@@ -0,0 +1,49 @@
1
+ With PID
2
+ ========
3
+
4
+ The simple way to create and monitor a Ruby daemon.
5
+
6
+ Works by generating a bash file and executing it instead of doing any Ruby process tricks.
7
+
8
+ Requirements
9
+ ------------
10
+
11
+ <pre>
12
+ sudo gem install with_pid
13
+ </pre>
14
+
15
+ Ruby script
16
+ -----------
17
+
18
+ <code>/data/my_app/current/bin/dj</code>:
19
+
20
+ <pre>
21
+ #!/usr/bin/env ruby
22
+
23
+ require 'rubygems'
24
+ require 'with_pid'
25
+
26
+ with_pid(
27
+ :action => ARGV[0], # start or stop
28
+ :command => "/data/my_app/current/script/runner 'Delayed::Worker.new.start'",
29
+ :name => "dj",
30
+ :pid => "/var/run/dj/dj.pid",
31
+ :tmp => "/data/my_app/current/tmp",
32
+ :user => "deploy"
33
+ )
34
+ </pre>
35
+
36
+ Be sure to run <code>chmod +x /data/my_app/current/bin/dj</code>.
37
+
38
+ It is up to you to make sure that the command you execute runs in a continuous loop.
39
+
40
+ Monit
41
+ -----
42
+
43
+ <pre>
44
+ check process delayed_job
45
+ with pidfile /var/run/dj/dj.pid
46
+ start program = "/data/my_app/current/bin/dj start"
47
+ stop program = "/data/my_app/current/bin/dj stop"
48
+ group delayed_job
49
+ </pre>
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "#{File.dirname(__FILE__)}/require"
2
+ Require.rakefile!
data/lib/with_pid.rb ADDED
@@ -0,0 +1,17 @@
1
+ require 'rubygems'
2
+ require 'erb'
3
+
4
+ def with_pid(options)
5
+ options[:pid_dir] = File.dirname(options[:pid])
6
+ template = File.dirname(__FILE__) + '/../templates/bash.erb'
7
+ template = File.expand_path(template)
8
+ template = File.read(template)
9
+ template = ERB.new(template).result(binding)
10
+ bash_path = options[:tmp] + "/" + options[:name]
11
+ FileUtils.mkdir_p(options[:tmp])
12
+ File.open(bash_path, 'w') do |f|
13
+ f.write(template)
14
+ end
15
+ `chmod +x #{bash_path}`
16
+ `#{bash_path}`
17
+ end
data/require.rb ADDED
@@ -0,0 +1,30 @@
1
+ require 'rubygems'
2
+ gem 'require'
3
+ require 'require'
4
+
5
+ Require do
6
+ gem :require, '=0.2.6'
7
+ gem(:rake, '=0.8.7') { require 'rake' }
8
+ gem :rspec, '=1.3.0'
9
+
10
+ gemspec do
11
+ author 'Winton Welsh'
12
+ email 'mail@wintoni.us'
13
+ name 'with_pid'
14
+ homepage "http://github.com/winton/#{name}"
15
+ summary "The simple way to create and monitor a Ruby daemon"
16
+ version '0.1.0'
17
+ end
18
+
19
+ rakefile do
20
+ gem(:rake) { require 'rake/gempackagetask' }
21
+ gem(:rspec) { require 'spec/rake/spectask' }
22
+ require 'require/tasks'
23
+ end
24
+
25
+ spec_helper do
26
+ require 'require/spec_helper'
27
+ require 'lib/with_pid'
28
+ require 'pp'
29
+ end
30
+ end
@@ -0,0 +1,5 @@
1
+ require File.expand_path("#{File.dirname(__FILE__)}/../require")
2
+ Require.spec_helper!
3
+
4
+ Spec::Runner.configure do |config|
5
+ end
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+
3
+ describe :with_pid do
4
+
5
+ before(:each) do
6
+ @tmp = File.dirname(__FILE__) + "/tmp"
7
+ FileUtils.rm_rf @tmp
8
+ end
9
+
10
+ after(:each) do
11
+ FileUtils.rm_rf @tmp
12
+ end
13
+
14
+ it "should work" do
15
+ output = with_pid(
16
+ :action => :start, # start or stop
17
+ :command => "ps",
18
+ :name => "test",
19
+ :pid => @tmp + "/test.pid",
20
+ :tmp => @tmp,
21
+ :user => "winton"
22
+ )
23
+ output.include?('PID').should == true
24
+ File.read(@tmp + "/test.pid").to_i.should > 0
25
+ end
26
+ end
@@ -0,0 +1,43 @@
1
+ #!/bin/sh
2
+
3
+ PATH=/bin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:$PATH
4
+ CURDIR=`pwd`
5
+ COMMAND="<%= options[:command] %>"
6
+ PID_FILE="<%= options[:pid] %>"
7
+ USER="<%= options[:user] %>"
8
+ HOME="/home/$USER" ; export HOME
9
+
10
+ mkdir -p <%= options[:pid_dir] %>
11
+
12
+ case "<%= options[:action] %>" in
13
+ start)
14
+ echo "Starting <%= options[:name] %>"
15
+ if [ -f $PID_FILE ]; then
16
+ PID=`cat $PID_FILE`
17
+ if [ -d /proc/$PID ]; then
18
+ echo "Already running."
19
+ exit 1
20
+ fi
21
+ rm -f $PID_FILE
22
+ fi
23
+ echo $$ > $PID_FILE;
24
+ <% if RUBY_PLATFORM.include?('darwin') %>
25
+ exec 2<&1 sudo -u$USER "$COMMAND"
26
+ <% else %>
27
+ exec 2<&1 su -c"$COMMAND" $USER
28
+ <% end %>
29
+ ;;
30
+ stop)
31
+ echo "Stopping <%= options[:name] %>"
32
+ if [ -f $PID_FILE ]; then
33
+ kill -15 `cat $PID_FILE` 2>/dev/null; true
34
+ fi
35
+
36
+ [ -e "$PIDFILE" ] && rm -f $PIDFILE
37
+ ;;
38
+ *)
39
+ usage
40
+ ;;
41
+ esac
42
+
43
+ cd $CURDIR
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: with_pid
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Winton Welsh
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-04-18 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: mail@wintoni.us
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.markdown
24
+ files:
25
+ - lib/with_pid.rb
26
+ - MIT-LICENSE
27
+ - Rakefile
28
+ - README.markdown
29
+ - require.rb
30
+ - spec/spec_helper.rb
31
+ - spec/with_pid_spec.rb
32
+ - templates/bash.erb
33
+ has_rdoc: true
34
+ homepage: http://github.com/winton/with_pid
35
+ licenses: []
36
+
37
+ post_install_message:
38
+ rdoc_options: []
39
+
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ version:
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ requirements: []
55
+
56
+ rubyforge_project:
57
+ rubygems_version: 1.3.5
58
+ signing_key:
59
+ specification_version: 3
60
+ summary: The simple way to create and monitor a Ruby daemon
61
+ test_files: []
62
+