spoon_daemon 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in spoon_daemon.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Emilien Taque
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # SpoonDaemon
2
+
3
+ A daemon tool for JRuby based on Spoon gem.
4
+
5
+ Inspiration: https://gist.github.com/910177
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'spoon_daemon'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install spoon_daemon
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ SpoonDaemon::Runner.new('path/to/my/script.rb', cmd, options)
25
+ ```
26
+
27
+ Where cmd is 'start', 'stop' or 'restart'
28
+
29
+ And options are:
30
+ * `pid_dir`: where to create the PID file
31
+
32
+
33
+ ## Contributing
34
+
35
+ 1. Fork it
36
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
37
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
38
+ 4. Push to the branch (`git push origin my-new-feature`)
39
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,3 @@
1
+ module SpoonDaemon
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,90 @@
1
+ require "spoon"
2
+
3
+ require "spoon_daemon/version"
4
+
5
+ module SpoonDaemon
6
+ class Runner
7
+ def initialize(script, cmd, options = {})
8
+ @script = script
9
+
10
+ @pid_dir = options[:pid_dir] || '.'
11
+ @name = options[:name] || File.basename(script)
12
+
13
+ @pid_path = File.join(@pid_dir, "#{@name}.pid")
14
+
15
+ case cmd
16
+ when 'start'
17
+ start
18
+ when 'stop'
19
+ stop
20
+ when 'restart'
21
+ stop
22
+ start
23
+ else
24
+ STDERR.puts "Usage: <script> <start|stop|restart>"
25
+ exit!
26
+ end
27
+ end
28
+
29
+ def create_pid(pid)
30
+ begin
31
+ open(@pid_path, 'w') do |f|
32
+ f.puts pid
33
+ end
34
+ rescue => e
35
+ STDERR.puts "Error: Unable to open #{@pid_path} for writing:\n\t" +
36
+ "(#{e.class}) #{e.message}"
37
+ exit!
38
+ end
39
+ end
40
+
41
+ def get_pid
42
+ pid = nil
43
+ open(@pid_path, 'r') do |f|
44
+ pid = f.readline.to_s.gsub(/[^0-9]/,'')
45
+ end
46
+ pid.to_i
47
+ rescue Errno::ENOENT => e
48
+ nil
49
+ end
50
+
51
+ def remove_pidfile
52
+ File.unlink(@pid_path)
53
+ rescue => e
54
+ STDERR.puts "ERROR: Unable to unlink #{path}:\n\t" +
55
+ "(#{e.class}) #{e.message}"
56
+ exit
57
+ end
58
+
59
+ def process_exists?
60
+ pid = get_pid
61
+ return false unless pid
62
+ Process.kill(0, pid)
63
+ true
64
+ rescue Errno::ESRCH, TypeError # "PID is NOT running or is zombied
65
+ false
66
+ rescue Errno::EPERM
67
+ STDERR.puts "No permission to query #{pid}!";
68
+ false
69
+ end
70
+
71
+ def stop
72
+ pid = get_pid
73
+ Process.kill("TERM", pid)
74
+ remove_pidfile
75
+ rescue Errno::ESRCH
76
+ abort "No process to kill"
77
+ end
78
+
79
+ def start
80
+ abort "Process already running!" if process_exists?
81
+ abort "Script not executable!" unless File.executable?(@script)
82
+ if pid = Spoon.spawnp(@script)
83
+ create_pid(pid)
84
+ STDOUT.puts "Process running with pid #{pid}"
85
+ else
86
+ Process.setsid
87
+ end
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'spoon_daemon/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "spoon_daemon"
8
+ gem.version = SpoonDaemon::VERSION
9
+ gem.authors = ["Emilien Taque"]
10
+ gem.email = ["e.taque@gmail.com"]
11
+ gem.description = %q{Daemon tool for JRuby based on Spoon.}
12
+ gem.summary = %q{Daemon tool for JRuby based on Spoon.}
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_runtime_dependency(%q<spoon>, [">= 0.0.1"])
21
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: spoon_daemon
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.2
6
+ platform: ruby
7
+ authors:
8
+ - Emilien Taque
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2012-12-26 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: spoon
17
+ prerelease: false
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.1
24
+ type: :runtime
25
+ version_requirements: *id001
26
+ description: Daemon tool for JRuby based on Spoon.
27
+ email:
28
+ - e.taque@gmail.com
29
+ executables: []
30
+
31
+ extensions: []
32
+
33
+ extra_rdoc_files: []
34
+
35
+ files:
36
+ - .gitignore
37
+ - Gemfile
38
+ - LICENSE.txt
39
+ - README.md
40
+ - Rakefile
41
+ - lib/spoon_daemon.rb
42
+ - lib/spoon_daemon/version.rb
43
+ - spoon_daemon.gemspec
44
+ homepage: ""
45
+ licenses: []
46
+
47
+ post_install_message:
48
+ rdoc_options: []
49
+
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: "0"
64
+ requirements: []
65
+
66
+ rubyforge_project:
67
+ rubygems_version: 1.8.24
68
+ signing_key:
69
+ specification_version: 3
70
+ summary: Daemon tool for JRuby based on Spoon.
71
+ test_files: []
72
+