Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

First commit

  • Loading branch information...
commit 3127140725cdd0884d5ebae52b274b45cd569d42 1 parent 77271f3
@winton authored
View
53 README.markdown
@@ -1,36 +1,49 @@
-GemTemplate
-===========
+With PID
+========
-A gem template for new projects.
+The simple way to create and monitor a Ruby daemon.
+
+Works by generating a bash file and executing it instead of doing any Ruby process tricks.
Requirements
------------
<pre>
-sudo gem install stencil
+sudo gem install with_pid
</pre>
-Setup the template
-------------------
+Ruby script
+-----------
-You only have to do this once.
+<code>/data/my_app/current/bin/dj</code>:
<pre>
-git clone git@github.com:winton/gem_template.git
-cd gem_template
-stencil
+#!/usr/bin/env ruby
+
+require 'rubygems'
+require 'with_pid'
+
+with_pid(
+ :action => ARGV[0], # start or stop
+ :command => "/data/my_app/current/script/runner 'Delayed::Worker.new.start'",
+ :name => "dj",
+ :pid => "/var/run/dj/dj.pid",
+ :tmp => "/data/my_app/current/tmp",
+ :user => "deploy"
+)
</pre>
-Setup a new project
--------------------
+Be sure to run <code>chmod +x /data/my_app/current/bin/dj</code>.
-Do this for every new project.
+It is up to you to make sure that the command you execute runs in a continuous loop.
-<pre>
-mkdir my_project
-git init
-stencil gem_template
-rake rename
-</pre>
+Monit
+-----
-The last command does a find-replace (gem\_template -> my\_project) on files and filenames.
+<pre>
+check process delayed_job
+ with pidfile /var/run/dj/dj.pid
+ start program = "/data/my_app/current/bin/dj start"
+ stop program = "/data/my_app/current/bin/dj stop"
+ group delayed_job
+</pre>
View
23 Rakefile
@@ -1,23 +1,2 @@
require "#{File.dirname(__FILE__)}/require"
-Require.rakefile!
-
-# You can delete this after you use it
-desc "Rename project"
-task :rename do
- name = ENV['NAME'] || File.basename(Dir.pwd)
- begin
- dir = Dir['**/gem_template*']
- from = dir.pop
- if from
- rb = from.include?('.rb')
- to = File.dirname(from) + "/#{name}#{'.rb' if rb}"
- FileUtils.mv(from, to)
- end
- end while dir.length > 0
- Dir["**/*"].each do |path|
- next if path.include?('Rakefile')
- if File.file?(path)
- `sed -i "" 's/gem_template/#{name}/g' #{path}`
- end
- end
-end
+Require.rakefile!
View
4 bin/gem_template
@@ -1,4 +0,0 @@
-#!/usr/bin/env ruby
-
-require File.expand_path("#{File.dirname(__FILE__)}/../require")
-Require.bin!
View
2  lib/gem_template.rb
@@ -1,2 +0,0 @@
-require File.expand_path("#{File.dirname(__FILE__)}/../require")
-Require.lib!
View
0  lib/gem_template/gem_template.rb
No changes.
View
17 lib/with_pid.rb
@@ -0,0 +1,17 @@
+require 'rubygems'
+require 'erb'
+
+def with_pid(options)
+ options[:pid_dir] = File.dirname(options[:pid])
+ template = File.dirname(__FILE__) + '/../templates/bash.erb'
+ template = File.expand_path(template)
+ template = File.read(template)
+ template = ERB.new(template).result(binding)
+ bash_path = options[:tmp] + "/" + options[:name]
+ FileUtils.mkdir_p(options[:tmp])
+ File.open(bash_path, 'w') do |f|
+ f.write(template)
+ end
+ `chmod +x #{bash_path}`
+ `#{bash_path}`
+end
View
14 require.rb
@@ -9,19 +9,13 @@
gemspec do
author 'Winton Welsh'
- dependencies do
- gem :require
- end
email 'mail@wintoni.us'
- name 'gem_template'
+ name 'with_pid'
homepage "http://github.com/winton/#{name}"
- summary ""
+ summary "The simple way to create and monitor a Ruby daemon"
version '0.1.0'
end
- bin { require 'lib/gem_template' }
- lib { require 'lib/gem_template/gem_template' }
-
rakefile do
gem(:rake) { require 'rake/gempackagetask' }
gem(:rspec) { require 'spec/rake/spectask' }
@@ -30,7 +24,7 @@
spec_helper do
require 'require/spec_helper'
- require 'lib/gem_template'
+ require 'lib/with_pid'
require 'pp'
end
-end
+end
View
2  spec/spec_helper.rb
@@ -2,4 +2,4 @@
Require.spec_helper!
Spec::Runner.configure do |config|
-end
+end
View
26 spec/with_pid_spec.rb
@@ -0,0 +1,26 @@
+require 'spec_helper'
+
+describe :with_pid do
+
+ before(:each) do
+ @tmp = File.dirname(__FILE__) + "/tmp"
+ FileUtils.rm_rf @tmp
+ end
+
+ after(:each) do
+ FileUtils.rm_rf @tmp
+ end
+
+ it "should work" do
+ output = with_pid(
+ :action => :start, # start or stop
+ :command => "ps",
+ :name => "test",
+ :pid => @tmp + "/test.pid",
+ :tmp => @tmp,
+ :user => "winton"
+ )
+ output.include?('PID').should == true
+ File.read(@tmp + "/test.pid").to_i.should > 0
+ end
+end
View
43 templates/bash.erb
@@ -0,0 +1,43 @@
+#!/bin/sh
+
+PATH=/bin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:$PATH
+CURDIR=`pwd`
+COMMAND="<%= options[:command] %>"
+PID_FILE="<%= options[:pid] %>"
+USER="<%= options[:user] %>"
+HOME="/home/$USER" ; export HOME
+
+mkdir -p <%= options[:pid_dir] %>
+
+case "<%= options[:action] %>" in
+ start)
+ echo "Starting <%= options[:name] %>"
+ if [ -f $PID_FILE ]; then
+ PID=`cat $PID_FILE`
+ if [ -d /proc/$PID ]; then
+ echo "Already running."
+ exit 1
+ fi
+ rm -f $PID_FILE
+ fi
+ echo $$ > $PID_FILE;
+<% if RUBY_PLATFORM.include?('darwin') %>
+ exec 2<&1 sudo -u$USER "$COMMAND"
+<% else %>
+ exec 2<&1 su -c"$COMMAND" $USER
+<% end %>
+ ;;
+ stop)
+ echo "Stopping <%= options[:name] %>"
+ if [ -f $PID_FILE ]; then
+ kill -15 `cat $PID_FILE` 2>/dev/null; true
+ fi
+
+ [ -e "$PIDFILE" ] && rm -f $PIDFILE
+ ;;
+ *)
+ usage
+ ;;
+ esac
+
+cd $CURDIR
Please sign in to comment.
Something went wrong with that request. Please try again.