fallen 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/README.md +86 -0
- data/UNLICENSE +24 -0
- data/examples/azazel.rb +14 -0
- data/examples/azazel_cli.rb +27 -0
- data/fallen.gemspec +13 -0
- data/lib/fallen.rb +86 -0
- data/lib/fallen/cli.rb +27 -0
- metadata +53 -0
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
*.gem
|
data/README.md
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
# Fallen
|
2
|
+
A simpler daemon library for Ruby processes.
|
3
|
+
|
4
|
+
## Usage
|
5
|
+
|
6
|
+
```
|
7
|
+
require "fallen"
|
8
|
+
|
9
|
+
module Azazel
|
10
|
+
extend Fallen
|
11
|
+
|
12
|
+
def self.run
|
13
|
+
while running?
|
14
|
+
puts "My name is Legion"
|
15
|
+
sleep 666
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
Azazel.start!
|
21
|
+
```
|
22
|
+
|
23
|
+
This will print _My name is Legion_ every 666 seconds on `STDOUT`. You can stop the daemon by pressing `CTRL+c`.
|
24
|
+
|
25
|
+
## Control your daemon
|
26
|
+
`Fallen` accepts the following methods:
|
27
|
+
|
28
|
+
* `daemonize!`: detaches the process and keep running it on background;
|
29
|
+
* `chdir!`: changes the current working directory of the process (useful for log and pid files);
|
30
|
+
* `pid_file`: allows to indicate a file path where the daemon `PID` will be stored; could be either an absolute path or a relative path to the current working directory (see `chdir!`);
|
31
|
+
* `stdout`, `stderr` & `stdin`: redirects the process `STDOUT`, `STDERR` and `STDIN` to either an absolute or relative file path; note that when a process is daemonized by default all these streams are redirected to `/dev/null`.
|
32
|
+
|
33
|
+
For example, the previous example could have the following lines before `Azazel.start!` to store the `PID` and log to a file:
|
34
|
+
|
35
|
+
```
|
36
|
+
Azazel.pid_file "/var/run/azazel.pid"
|
37
|
+
Azazel.stdout "/var/log/azazel.log"
|
38
|
+
Azazel.daemonize!
|
39
|
+
Azazel.start!
|
40
|
+
```
|
41
|
+
|
42
|
+
## CLI support
|
43
|
+
`Fallen` supports command line parameters, by default using the [`clap`](https://github.com/soveran/clap) gem. In order to enable command line support you need to do the following:
|
44
|
+
|
45
|
+
```
|
46
|
+
require "fallen"
|
47
|
+
require "fallen/cli"
|
48
|
+
|
49
|
+
module Azazel
|
50
|
+
extend Fallen
|
51
|
+
extend Fallen::CLI
|
52
|
+
|
53
|
+
# ...
|
54
|
+
|
55
|
+
def usage
|
56
|
+
puts "..." # Your usage message
|
57
|
+
# Default Fallen usage options
|
58
|
+
puts fallen_usage
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
case Clap.run(ARGV, Azazel.cli).first
|
63
|
+
when "start"
|
64
|
+
Azazel.start!
|
65
|
+
when "stop"
|
66
|
+
Azazel.stop!
|
67
|
+
else
|
68
|
+
Azazel.usage
|
69
|
+
end
|
70
|
+
```
|
71
|
+
|
72
|
+
`Azazel.usage` will print the following:
|
73
|
+
|
74
|
+
```
|
75
|
+
Fallen introduced command line arguments:
|
76
|
+
|
77
|
+
-D Daemonize this process
|
78
|
+
-C Change directory.
|
79
|
+
-P Path to PID file. Only used in daemonized process.
|
80
|
+
-out Path to redirect STDOUT.
|
81
|
+
-err Path to redirect STDERR.
|
82
|
+
-in Path to redirect STDIN.
|
83
|
+
```
|
84
|
+
|
85
|
+
## License
|
86
|
+
See the `UNLICENSE` file included with this gem distribution.
|
data/UNLICENSE
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
This is free and unencumbered software released into the public domain.
|
2
|
+
|
3
|
+
Anyone is free to copy, modify, publish, use, compile, sell, or
|
4
|
+
distribute this software, either in source code form or as a compiled
|
5
|
+
binary, for any purpose, commercial or non-commercial, and by any
|
6
|
+
means.
|
7
|
+
|
8
|
+
In jurisdictions that recognize copyright laws, the author or authors
|
9
|
+
of this software dedicate any and all copyright interest in the
|
10
|
+
software to the public domain. We make this dedication for the benefit
|
11
|
+
of the public at large and to the detriment of our heirs and
|
12
|
+
successors. We intend this dedication to be an overt act of
|
13
|
+
relinquishment in perpetuity of all present and future rights to this
|
14
|
+
software under copyright law.
|
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 NONINFRINGEMENT.
|
19
|
+
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
20
|
+
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
21
|
+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
23
|
+
|
24
|
+
For more information, please refer to <http://unlicense.org/>
|
data/examples/azazel.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require_relative "../lib/fallen"
|
2
|
+
require_relative "../lib/fallen/cli"
|
3
|
+
|
4
|
+
module Azazel
|
5
|
+
extend Fallen
|
6
|
+
extend Fallen::CLI
|
7
|
+
|
8
|
+
def self.run
|
9
|
+
while running?
|
10
|
+
puts "My name is Legion"
|
11
|
+
sleep 666
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.usage
|
16
|
+
puts fallen_usage
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
case Clap.run(ARGV, Azazel.cli).first
|
21
|
+
when "start"
|
22
|
+
Azazel.start!
|
23
|
+
when "stop"
|
24
|
+
Azazel.stop!
|
25
|
+
else
|
26
|
+
Azazel.usage
|
27
|
+
end
|
data/fallen.gemspec
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = "fallen"
|
5
|
+
s.version = "0.0.1"
|
6
|
+
s.summary = "A simpler daemon library"
|
7
|
+
s.description = "A simpler way to create Ruby fallen angels, better known as daemons"
|
8
|
+
s.authors = ["Leandro López"]
|
9
|
+
s.email = ["inkel.ar@gmail.com"]
|
10
|
+
s.homepage = "https://github.com/inkel/fallen"
|
11
|
+
|
12
|
+
s.files = `git ls-files`.split("\n")
|
13
|
+
end
|
data/lib/fallen.rb
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
module Fallen
|
2
|
+
@running = false
|
3
|
+
@pidfile = nil
|
4
|
+
@stdin = nil
|
5
|
+
@stdout = nil
|
6
|
+
@stderr = nil
|
7
|
+
|
8
|
+
def daemonize!
|
9
|
+
Process.daemon true
|
10
|
+
end
|
11
|
+
|
12
|
+
def chdir! path
|
13
|
+
Dir.chdir File.absolute_path(path)
|
14
|
+
end
|
15
|
+
|
16
|
+
def pid_file path
|
17
|
+
@pid_file = File.absolute_path(path)
|
18
|
+
end
|
19
|
+
|
20
|
+
def stdin path
|
21
|
+
@stdin = File.absolute_path(path)
|
22
|
+
STDIN.reopen @stdin
|
23
|
+
end
|
24
|
+
|
25
|
+
def stdout path
|
26
|
+
@stdout = File.absolute_path(path)
|
27
|
+
STDOUT.reopen @stdout, "a"
|
28
|
+
end
|
29
|
+
|
30
|
+
def stderr path
|
31
|
+
@stderr = File.absolute_path(path)
|
32
|
+
STDERR.reopen @stderr, "a"
|
33
|
+
end
|
34
|
+
|
35
|
+
def running?
|
36
|
+
@running
|
37
|
+
end
|
38
|
+
|
39
|
+
def start!
|
40
|
+
if @pid_file && File.exists?(@pid_file)
|
41
|
+
pid = File.read(@pid_file).strip
|
42
|
+
begin
|
43
|
+
Process.kill 0, pid.to_i
|
44
|
+
STDERR.puts "Daemon is already running with PID #{pid}"
|
45
|
+
exit 2
|
46
|
+
rescue Errno::ESRCH
|
47
|
+
run!
|
48
|
+
end
|
49
|
+
else
|
50
|
+
run!
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def run!
|
55
|
+
save_pid_file
|
56
|
+
@running = true
|
57
|
+
trap(:INT) { @running = false; stop }
|
58
|
+
trap(:TERM) { @running = false; stop }
|
59
|
+
run
|
60
|
+
end
|
61
|
+
|
62
|
+
def stop!
|
63
|
+
if @pid_file && File.exists?(@pid_file)
|
64
|
+
pid = File.read(@pid_file).strip
|
65
|
+
begin
|
66
|
+
Process.kill :INT, pid.to_i
|
67
|
+
rescue Errno::ESRCH
|
68
|
+
STDERR.puts "No daemon is running with PID #{pid}"
|
69
|
+
exit 3
|
70
|
+
end
|
71
|
+
else
|
72
|
+
STDERR.puts "Couldn't find a PID file"
|
73
|
+
exit 1
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def stop; end
|
78
|
+
|
79
|
+
private
|
80
|
+
|
81
|
+
def save_pid_file
|
82
|
+
File.open(@pid_file, "w") do |fp|
|
83
|
+
fp.write(Process.pid)
|
84
|
+
end if @pid_file
|
85
|
+
end
|
86
|
+
end
|
data/lib/fallen/cli.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require "clap"
|
2
|
+
|
3
|
+
module Fallen::CLI
|
4
|
+
def cli
|
5
|
+
{
|
6
|
+
"-D" => self.method(:daemonize!),
|
7
|
+
"-C" => self.method(:chdir!),
|
8
|
+
"-P" => self.method(:pid_file),
|
9
|
+
"-out" => self.method(:stdout),
|
10
|
+
"-err" => self.method(:stderr),
|
11
|
+
"-in" => self.method(:stdin)
|
12
|
+
}
|
13
|
+
end
|
14
|
+
|
15
|
+
def fallen_usage
|
16
|
+
<<-USAGE
|
17
|
+
Fallen introduced command line arguments:
|
18
|
+
|
19
|
+
-D Daemonize this process
|
20
|
+
-C Change directory.
|
21
|
+
-P Path to PID file. Only used in daemonized process.
|
22
|
+
-out Path to redirect STDOUT.
|
23
|
+
-err Path to redirect STDERR.
|
24
|
+
-in Path to redirect STDIN.
|
25
|
+
USAGE
|
26
|
+
end
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fallen
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Leandro López
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-08-29 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: A simpler way to create Ruby fallen angels, better known as daemons
|
15
|
+
email:
|
16
|
+
- inkel.ar@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- README.md
|
23
|
+
- UNLICENSE
|
24
|
+
- examples/azazel.rb
|
25
|
+
- examples/azazel_cli.rb
|
26
|
+
- fallen.gemspec
|
27
|
+
- lib/fallen.rb
|
28
|
+
- lib/fallen/cli.rb
|
29
|
+
homepage: https://github.com/inkel/fallen
|
30
|
+
licenses: []
|
31
|
+
post_install_message:
|
32
|
+
rdoc_options: []
|
33
|
+
require_paths:
|
34
|
+
- lib
|
35
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
36
|
+
none: false
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ! '>='
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
requirements: []
|
48
|
+
rubyforge_project:
|
49
|
+
rubygems_version: 1.8.23
|
50
|
+
signing_key:
|
51
|
+
specification_version: 3
|
52
|
+
summary: A simpler daemon library
|
53
|
+
test_files: []
|