sevenwire-simple-daemon 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/History.txt ADDED
@@ -0,0 +1,42 @@
1
+ == 0.1.4 2009-03-03
2
+
3
+ * 1 minor enhancement:
4
+ * Separated WORKING_DIRECTORY into LOG_DIRECTORY and PID_DIRECTORY
5
+
6
+ == 0.1.3 2008-12-23
7
+
8
+ * 2 minor enhancements:
9
+ * Fixes bug where the start command will start two instances of the daemon
10
+ * GitHub compatible gemspec
11
+
12
+ == 0.1.2.x 2008-06-17
13
+
14
+ * 1 minor enhancement:
15
+ * Lowercase pid and log filenames
16
+
17
+ == 0.1.2 2007-07-08
18
+
19
+ * 2 minor enhancements:
20
+ * Renamed WorkingDirectory constant to WORKING_DIRECTORY
21
+ * Renamed logfile and PIDfile to ignore namespacing
22
+ (e.g. log/Job.pid, not log/Foo::Job.pid)
23
+
24
+ == 0.1.1 2007-07-08
25
+
26
+ * 1 minor enhancements:
27
+ * Documentation fix
28
+
29
+ == 0.1.0 2007-07-08
30
+
31
+ * 1 minor enhancements:
32
+ * Documentation and website
33
+
34
+ == 0.0.3 2007-07-08
35
+
36
+ * 1 major enhancement:
37
+ * Added logging
38
+
39
+ == 0.0.2 2007-07-07
40
+
41
+ * 1 major enhancement:
42
+ * Initial release
data/License.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2007 Jonathan Dahl
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/Manifest.txt ADDED
@@ -0,0 +1,17 @@
1
+ History.txt
2
+ License.txt
3
+ Manifest.txt
4
+ README.markdown
5
+ Rakefile
6
+ lib/simple-daemon.rb
7
+ lib/simple-daemon/version.rb
8
+ scripts/txt2html
9
+ setup.rb
10
+ spec/simple-daemon_spec.rb
11
+ spec/spec.opts
12
+ spec/spec_helper.rb
13
+ website/index.html
14
+ website/index.txt
15
+ website/javascripts/rounded_corners_lite.inc.js
16
+ website/stylesheets/screen.css
17
+ website/template.rhtml
data/README.markdown ADDED
@@ -0,0 +1,4 @@
1
+ simple-daemon
2
+ =============
3
+
4
+ A simple, 78-line daemon module for Ruby. Adds daemon functionality to any Ruby class (start, stop, restart; pid file; logging). Built on a module by Sharon Rosner released to DZone Snippets.
data/Rakefile ADDED
@@ -0,0 +1,147 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/clean'
4
+ require 'rake/testtask'
5
+ require 'rake/packagetask'
6
+ require 'rake/gempackagetask'
7
+ require 'rake/rdoctask'
8
+ require 'rake/contrib/rubyforgepublisher'
9
+ require 'fileutils'
10
+ require 'hoe'
11
+ begin
12
+ require 'spec/rake/spectask'
13
+ rescue LoadError
14
+ puts 'To use rspec for testing you must install rspec gem:'
15
+ puts '$ sudo gem install rspec'
16
+ exit
17
+ end
18
+
19
+ include FileUtils
20
+ require File.join(File.dirname(__FILE__), 'lib', 'simple-daemon', 'version')
21
+
22
+ AUTHOR = 'Jonathan Dahl' # can also be an array of Authors
23
+ EMAIL = "jon@slantwisedesign.com"
24
+ DESCRIPTION = "Simple module that adds daemon functionality to a Ruby script."
25
+ GEM_NAME = 'simple-daemon' # what ppl will type to install your gem
26
+
27
+ @config_file = "~/.rubyforge/user-config.yml"
28
+ @config = nil
29
+ def rubyforge_username
30
+ unless @config
31
+ begin
32
+ @config = YAML.load(File.read(File.expand_path(@config_file)))
33
+ rescue
34
+ puts <<-EOS
35
+ ERROR: No rubyforge config file found: #{@config_file}"
36
+ Run 'rubyforge setup' to prepare your env for access to Rubyforge
37
+ - See http://newgem.rubyforge.org/rubyforge.html for more details
38
+ EOS
39
+ exit
40
+ end
41
+ end
42
+ @rubyforge_username ||= @config["username"]
43
+ end
44
+
45
+ RUBYFORGE_PROJECT = 'simple-daemon' # The unix name for your project
46
+ HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
47
+ DOWNLOAD_PATH = "http://rubyforge.org/projects/#{RUBYFORGE_PROJECT}"
48
+
49
+ NAME = "simple-daemon"
50
+ REV = nil
51
+ # UNCOMMENT IF REQUIRED:
52
+ # REV = `svn info`.each {|line| if line =~ /^Revision:/ then k,v = line.split(': '); break v.chomp; else next; end} rescue nil
53
+ VERS = SimpleDaemon::VERSION::STRING + (REV ? ".#{REV}" : "")
54
+ CLEAN.include ['**/.*.sw?', '*.gem', '.config', '**/.DS_Store']
55
+ RDOC_OPTS = ['--quiet', '--title', 'simple-daemon documentation',
56
+ "--opname", "index.html",
57
+ "--line-numbers",
58
+ "--main", "README",
59
+ "--inline-source"]
60
+
61
+ class Hoe
62
+ def extra_deps
63
+ @extra_deps.reject { |x| Array(x).first == 'hoe' }
64
+ end
65
+ end
66
+
67
+ # Generate all the Rake tasks
68
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
69
+ hoe = Hoe.new(GEM_NAME, VERS) do |p|
70
+ p.author = AUTHOR
71
+ p.description = DESCRIPTION
72
+ p.email = EMAIL
73
+ p.summary = DESCRIPTION
74
+ p.url = HOMEPATH
75
+ p.rubyforge_name = RUBYFORGE_PROJECT if RUBYFORGE_PROJECT
76
+ p.test_globs = ["test/**/test_*.rb"]
77
+ p.clean_globs |= CLEAN #An array of file patterns to delete on clean.
78
+
79
+ # == Optional
80
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
81
+ #p.extra_deps = [] # An array of rubygem dependencies [name, version], e.g. [ ['active_support', '>= 1.3.1'] ]
82
+ #p.spec_extras = {} # A hash of extra values to set in the gemspec.
83
+ end
84
+
85
+ CHANGES = hoe.paragraphs_of('History.txt', 0..1).join("\n\n")
86
+ PATH = (RUBYFORGE_PROJECT == GEM_NAME) ? RUBYFORGE_PROJECT : "#{RUBYFORGE_PROJECT}/#{GEM_NAME}"
87
+ hoe.remote_rdoc_dir = File.join(PATH.gsub(/^#{RUBYFORGE_PROJECT}\/?/,''), 'rdoc')
88
+
89
+ desc 'Generate updated gemspec with unique version, which will cause gem to be auto-built on github.'
90
+ task :update_gemspec do
91
+ spec = hoe.spec
92
+ spec.version = VERS + '.' + Time.now.strftime('%Y%m%d%H%M%S')
93
+ File.open('simple-daemon.gemspec', 'w') do |output|
94
+ output << spec.to_ruby
95
+ end
96
+ end
97
+
98
+ desc 'Generate website files'
99
+ task :website_generate do
100
+ Dir['website/**/*.txt'].each do |txt|
101
+ sh %{ ruby scripts/txt2html #{txt} > #{txt.gsub(/txt$/,'html')} }
102
+ end
103
+ end
104
+
105
+ desc 'Upload website files to rubyforge'
106
+ task :website_upload do
107
+ host = "#{rubyforge_username}@rubyforge.org"
108
+ remote_dir = "/var/www/gforge-projects/#{PATH}/"
109
+ local_dir = 'website'
110
+ sh %{rsync -aCv #{local_dir}/ #{host}:#{remote_dir}}
111
+ end
112
+
113
+ desc 'Generate and upload website files'
114
+ task :website => [:website_generate, :website_upload, :publish_docs]
115
+
116
+ desc 'Release the website and new gem version'
117
+ task :deploy => [:check_version, :website, :release] do
118
+ puts "Remember to create SVN tag:"
119
+ puts "svn copy svn+ssh://#{rubyforge_username}@rubyforge.org/var/svn/#{PATH}/trunk " +
120
+ "svn+ssh://#{rubyforge_username}@rubyforge.org/var/svn/#{PATH}/tags/REL-#{VERS} "
121
+ puts "Suggested comment:"
122
+ puts "Tagging release #{CHANGES}"
123
+ end
124
+
125
+ desc 'Runs tasks website_generate and install_gem as a local deployment of the gem'
126
+ task :local_deploy => [:website_generate, :install_gem]
127
+
128
+ task :check_version do
129
+ unless ENV['VERSION']
130
+ puts 'Must pass a VERSION=x.y.z release version'
131
+ exit
132
+ end
133
+ unless ENV['VERSION'] == VERS
134
+ puts "Please update your version.rb to match the release version, currently #{VERS}"
135
+ exit
136
+ end
137
+ end
138
+
139
+ desc "Run the specs under spec/models"
140
+ Spec::Rake::SpecTask.new do |t|
141
+ t.spec_opts = ['--options', "spec/spec.opts"]
142
+ t.spec_files = FileList['spec/*_spec.rb']
143
+ end
144
+
145
+ desc "Default task is to run specs"
146
+ task :default => :spec
147
+
@@ -0,0 +1,9 @@
1
+ module SimpleDaemon #:nodoc:
2
+ module VERSION #:nodoc:
3
+ MAJOR = 0
4
+ MINOR = 1
5
+ TINY = 4
6
+
7
+ STRING = [MAJOR, MINOR, TINY].join('.')
8
+ end
9
+ end
@@ -0,0 +1,93 @@
1
+ require 'simple-daemon/version'
2
+ require 'fileutils'
3
+
4
+ module SimpleDaemon
5
+
6
+ class Base
7
+ def self.classname
8
+ underscore(name.split("::").last)
9
+ end
10
+
11
+ def self.pid_fn
12
+ File.join(SimpleDaemon::PID_DIRECTORY, "#{classname}.pid")
13
+ end
14
+
15
+ def self.daemonize
16
+ Controller.daemonize(self)
17
+ end
18
+
19
+
20
+ private
21
+
22
+ def self.underscore(camel_cased_word)
23
+ camel_cased_word.to_s.
24
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
25
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
26
+ tr("-", "_").
27
+ downcase
28
+ end
29
+ end
30
+
31
+
32
+ module PidFile
33
+ def self.store(daemon, pid)
34
+ File.open(daemon.pid_fn, 'w') {|f| f << pid}
35
+ end
36
+
37
+ def self.recall(daemon)
38
+ IO.read(daemon.pid_fn).to_i rescue nil
39
+ end
40
+ end
41
+
42
+
43
+ module Controller
44
+ def self.daemonize(daemon)
45
+ case !ARGV.empty? && ARGV[0]
46
+ when 'start'
47
+ start(daemon)
48
+ when 'stop'
49
+ stop(daemon)
50
+ when 'restart'
51
+ stop(daemon)
52
+ start(daemon)
53
+ else
54
+ puts "Invalid command. Please specify start, stop or restart."
55
+ exit
56
+ end
57
+ end
58
+
59
+ def self.start(daemon)
60
+ fork do
61
+ Process.setsid
62
+ exit if fork
63
+ if File.file?(daemon.pid_fn)
64
+ puts "Pid file #{daemon.pid_fn} already exists. Not starting."
65
+ exit 1
66
+ end
67
+ PidFile.store(daemon, Process.pid)
68
+ Dir.chdir SimpleDaemon::LOG_DIRECTORY
69
+ File.umask 0000
70
+ log = File.new("#{daemon.classname}.log", "a")
71
+ STDIN.reopen "/dev/null"
72
+ STDOUT.reopen log
73
+ STDERR.reopen STDOUT
74
+ trap("TERM") {daemon.stop; exit}
75
+ daemon.start
76
+ end
77
+ puts "Daemon started."
78
+ end
79
+
80
+ def self.stop(daemon)
81
+ if !File.file?(daemon.pid_fn)
82
+ puts "Pid file not found. Is the daemon started?"
83
+ exit
84
+ end
85
+ pid = PidFile.recall(daemon)
86
+ FileUtils.rm(daemon.pid_fn)
87
+ pid && Process.kill("TERM", pid)
88
+ rescue Errno::ESRCH
89
+ puts "Pid file found, but process was not running. The daemon may have died."
90
+ end
91
+ end
92
+
93
+ end
data/scripts/txt2html ADDED
@@ -0,0 +1,67 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'redcloth'
5
+ require 'syntax/convertors/html'
6
+ require 'erb'
7
+ require File.dirname(__FILE__) + '/../lib/simple-daemon/version.rb'
8
+
9
+ version = SimpleDaemon::VERSION::STRING
10
+ download = 'http://rubyforge.org/projects/simple-daemon'
11
+
12
+ class Fixnum
13
+ def ordinal
14
+ # teens
15
+ return 'th' if (10..19).include?(self % 100)
16
+ # others
17
+ case self % 10
18
+ when 1: return 'st'
19
+ when 2: return 'nd'
20
+ when 3: return 'rd'
21
+ else return 'th'
22
+ end
23
+ end
24
+ end
25
+
26
+ class Time
27
+ def pretty
28
+ return "#{mday}#{mday.ordinal} #{strftime('%B')} #{year}"
29
+ end
30
+ end
31
+
32
+ def convert_syntax(syntax, source)
33
+ return Syntax::Convertors::HTML.for_syntax(syntax).convert(source).gsub(%r!^<pre>|</pre>$!,'')
34
+ end
35
+
36
+ if ARGV.length >= 1
37
+ src, template = ARGV
38
+ template ||= File.dirname(__FILE__) + '/../website/template.rhtml'
39
+
40
+ else
41
+ puts("Usage: #{File.split($0).last} source.txt [template.rhtml] > output.html")
42
+ exit!
43
+ end
44
+
45
+ template = ERB.new(File.open(template).read)
46
+
47
+ title = nil
48
+ body = nil
49
+ File.open(src) do |fsrc|
50
+ title_text = fsrc.readline
51
+ body_text = fsrc.read
52
+ syntax_items = []
53
+ body_text.gsub!(%r!<(pre|code)[^>]*?syntax=['"]([^'"]+)[^>]*>(.*?)</>!m){
54
+ ident = syntax_items.length
55
+ element, syntax, source = $1, $2, $3
56
+ syntax_items << "<#{element} class='syntax'>#{convert_syntax(syntax, source)}</#{element}>"
57
+ "syntax-temp-#{ident}"
58
+ }
59
+ title = RedCloth.new(title_text).to_html.gsub(%r!<.*?>!,'').strip
60
+ body = RedCloth.new(body_text).to_html
61
+ body.gsub!(%r!(?:<pre><code>)?syntax-temp-(d+)(?:</code></pre>)?!){ syntax_items[$1.to_i] }
62
+ end
63
+ stat = File.stat(src)
64
+ created = stat.ctime
65
+ modified = stat.mtime
66
+
67
+ $stdout << template.result(binding)