forker 1.0.2 → 1.1.0

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/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
data/README.md CHANGED
@@ -1,3 +1,5 @@
1
+ [![Build Status](http://travis-ci.org/bmarini/forker.png)](http://travis-ci.org/bmarini/forker)
2
+
1
3
  # Goals
2
4
 
3
5
  * Simplest code possible to daemonize code, redirect output and manage the
@@ -6,7 +8,7 @@
6
8
  ## Usage
7
9
 
8
10
  require 'forker'
9
- Forker.fork(
11
+ Forker.fork!(
10
12
  :log => "/dev/null", # Default value
11
13
  :pid => "/var/run/#{File.basename($0)}.pid", # Default value
12
14
  :chdir => false, # Default value
@@ -0,0 +1,14 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rake/testtask'
5
+
6
+ task :default => [:test]
7
+
8
+ task :test do
9
+ Rake::TestTask.new do |t|
10
+ t.libs << "spec"
11
+ t.pattern = 'spec/*_spec.rb'
12
+ t.verbose = true
13
+ end
14
+ end
@@ -0,0 +1,18 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "forker/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "forker"
7
+ s.version = Forker::VERSION
8
+ s.date = "2010-09-27"
9
+ s.summary = "Fork your ruby code with confidence"
10
+ s.email = "bmarini@gmail.com"
11
+ s.homepage = "http://github.com/bmarini/forker"
12
+ s.description = "Fork your ruby code with confidence"
13
+ s.authors = ["Ben Marini"]
14
+ s.files = Dir.glob("lib/**/*") + %w(forker.gemspec Gemfile Rakefile README.md)
15
+ s.test_files = Dir.glob("spec/*")
16
+ s.add_dependency "SystemTimer", "~> 1.2" if RUBY_VERSION < "1.9"
17
+ s.add_development_dependency "minitest", "~> 2.0.2"
18
+ end
@@ -14,7 +14,13 @@
14
14
  # $$ = Process.pid
15
15
 
16
16
  require 'thread'
17
- require 'system_timer'
17
+
18
+ if RUBY_VERSION < '1.9'
19
+ require 'system_timer'
20
+ module Forker; Timeout = SystemTimer; end
21
+ else
22
+ require 'timeout'
23
+ end
18
24
 
19
25
  module Forker
20
26
  module CLI
@@ -100,7 +106,7 @@ module Forker
100
106
  Process.kill("TERM", pid)
101
107
 
102
108
  begin
103
- SystemTimer.timeout(sec) do
109
+ Timeout.timeout(sec) do
104
110
  loop do
105
111
  puts "waiting #{sec} seconds for #{pid} before sending KILL"
106
112
  Process.kill(0, pid) # See if proc exists
@@ -1,3 +1,3 @@
1
1
  module Forker
2
- VERSION = "1.0.2"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -0,0 +1,93 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'minitest/autorun'
4
+ require 'minitest/spec'
5
+ require 'fileutils'
6
+ require 'forker'
7
+
8
+ class Harness
9
+ attr_accessor :output
10
+
11
+ def run(args="")
12
+ @output = `spec/harness #{args}`
13
+ end
14
+
15
+ def start(args="")
16
+ run("start #{args}")
17
+ end
18
+
19
+ def stop(args="")
20
+ run("stop #{args}")
21
+ end
22
+
23
+ def pid
24
+ @output[/forked process is (\d+)/, 1]
25
+ end
26
+
27
+ def lsof
28
+ `lsof -p #{pid} | awk '{print $4, $9}'`.split("\n").grep(/^(0r|1w|2w) /)
29
+ end
30
+
31
+ FD = Struct.new(:name, :path)
32
+ def open_file_descriptors
33
+ @open_file_descriptors = lsof.map { |l| FD.new(*l.split) }
34
+ end
35
+ end
36
+
37
+ class ForkerSpec < MiniTest::Spec
38
+
39
+ describe "Forker::CLI" do
40
+ before { @harness = Harness.new }
41
+
42
+ it "should provide a helpful usage banner" do
43
+ @harness.run
44
+
45
+ @harness.output.must_equal <<-EOS.lstrip
46
+ Usage: harness [start|stop] -p /path/to/pidfile.pid
47
+ -l, --logfile LOGFILE redirect output to this location
48
+ -p, --pidfile PIDFILE save pidfile to this location
49
+ EOS
50
+ end
51
+ end
52
+
53
+ describe "forking" do
54
+ before do
55
+ @harness = Harness.new
56
+ end
57
+
58
+ after do
59
+ @harness.stop "-p spec/harness.pid"
60
+ FileUtils.rm_f "spec/harness.log"
61
+ end
62
+
63
+ it "should announce the pid and logfile location" do
64
+ @harness.start "-p spec/harness.pid"
65
+
66
+ line1, line2 = @harness.output.split("\n")
67
+ line1.must_match /forked process is (\d+)/
68
+ line2.must_equal "output redirected to /dev/null"
69
+ end
70
+
71
+ it "should redirect stdin, stdout and stderr to dev/null by default" do
72
+ @harness.start "-p spec/harness.pid"
73
+
74
+ @harness.open_file_descriptors.each do |fd|
75
+ fd.path.must_equal "/dev/null"
76
+ end
77
+ end
78
+
79
+ it "should allow you to specify a logfile" do
80
+ @harness.start "-l spec/harness.log -p spec/harness.pid"
81
+
82
+ sleep 0.1
83
+ File.exist?("spec/harness.log").must_equal true
84
+ File.read("spec/harness.log").must_match /I love tests/
85
+ end
86
+
87
+ it "should allow you to specify a pidfile" do
88
+ @harness.start "-p spec/harness.pid"
89
+ File.exist?("spec/harness.pid").must_equal true
90
+ @harness.pid.must_equal File.read("spec/harness.pid")
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems'
3
+ require 'bundler/setup'
4
+ require 'forker'
5
+
6
+ Forker::CLI.run(ARGV)
7
+ loop do
8
+ puts "I love tests"
9
+ sleep 0.1
10
+ end
metadata CHANGED
@@ -2,12 +2,12 @@
2
2
  name: forker
3
3
  version: !ruby/object:Gem::Version
4
4
  hash: 19
5
- prerelease:
5
+ prerelease: false
6
6
  segments:
7
7
  - 1
8
+ - 1
8
9
  - 0
9
- - 2
10
- version: 1.0.2
10
+ version: 1.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Ben Marini
@@ -60,7 +60,12 @@ extra_rdoc_files: []
60
60
  files:
61
61
  - lib/forker/version.rb
62
62
  - lib/forker.rb
63
+ - forker.gemspec
64
+ - Gemfile
65
+ - Rakefile
63
66
  - README.md
67
+ - spec/forker_spec.rb
68
+ - spec/harness
64
69
  has_rdoc: true
65
70
  homepage: http://github.com/bmarini/forker
66
71
  licenses: []
@@ -91,9 +96,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
91
96
  requirements: []
92
97
 
93
98
  rubyforge_project:
94
- rubygems_version: 1.6.1
99
+ rubygems_version: 1.3.7
95
100
  signing_key:
96
101
  specification_version: 3
97
102
  summary: Fork your ruby code with confidence
98
- test_files: []
99
-
103
+ test_files:
104
+ - spec/forker_spec.rb
105
+ - spec/harness