insidious 0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +21 -0
  3. data/README.md +36 -0
  4. data/Rakefile +6 -0
  5. data/lib/insidious.rb +166 -0
  6. metadata +48 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a06590a6526782e830ee32da0b2a75062e1d6135
4
+ data.tar.gz: e1f942f31bf0a442b8d2eecb7be8652e3e5447d4
5
+ SHA512:
6
+ metadata.gz: d755c4f24aaa7c2ca630308e6902f63edb07c1388a9b85a2b02ad6f3f0f80c4337337b8eb06ae3d1e888fec478971743d7273ec213c6c09894bb99622276ead9
7
+ data.tar.gz: 17095faa0d8e2243e0ee67cc6f55538d784c301a1a568dab425c06cedeaec56afe95853c307c50f1f524c973ec6d7ba3e970615dadddc1fc19e3115bf5f15415
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 James White
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,36 @@
1
+ Insidious [![Gem Version](http://img.shields.io/gem/v/insidious.svg)](https://rubygems.org/gems/insidious) [![Code Climate](http://img.shields.io/codeclimate/github/jamesrwhite/insidious.svg)](https://codeclimate.com/github/jamesrwhite/insidious)
2
+ ----------
3
+
4
+ A simple and flexible ruby gem for managing daemons.
5
+
6
+ ### Usage
7
+
8
+ Coming soon but for now check out [examples/insidious.rb](https://github.com/jamesrwhite/insidious/blob/master/examples/insidious.rb).
9
+
10
+ ### Credit
11
+
12
+ insidious is a fork of [fallen](https://github.com/inkel/fallen) by [@inkel](https://github.com/inkel) and a lot of credit for this goes to him.
13
+
14
+ ### License
15
+
16
+ The MIT License (MIT)
17
+
18
+ Copyright (c) 2014 James White
19
+
20
+ Permission is hereby granted, free of charge, to any person obtaining a copy
21
+ of this software and associated documentation files (the "Software"), to deal
22
+ in the Software without restriction, including without limitation the rights
23
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
24
+ copies of the Software, and to permit persons to whom the Software is
25
+ furnished to do so, subject to the following conditions:
26
+
27
+ The above copyright notice and this permission notice shall be included in all
28
+ copies or substantial portions of the Software.
29
+
30
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
31
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
32
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
33
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
34
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
35
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36
+ SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ # Show a list of tasks by default
4
+ task :default do
5
+ puts `rake --tasks`
6
+ end
data/lib/insidious.rb ADDED
@@ -0,0 +1,166 @@
1
+ class Insidious
2
+ attr_accessor :pid_file
3
+ attr_accessor :pid
4
+ attr_accessor :stdin
5
+ attr_accessor :stdout
6
+ attr_accessor :stderr
7
+
8
+ def initialize(options = {})
9
+ @daemonize = options[:daemonize].nil? ? true : options[:daemonize]
10
+ @pid_file = options[:pid_file]
11
+ @stdin = options[:stdin]
12
+ @stdout = options[:stdout]
13
+ @stderr = options[:stderr]
14
+ end
15
+
16
+ # Runs the daemon
17
+ #
18
+ # This will set up `INT` & `TERM` signal handlers to stop execution
19
+ # properly. When this signal handlers are called it will also call
20
+ # the #interrupt method and delete the pid file
21
+ def run!(&block)
22
+ begin
23
+ if @daemonize
24
+ Process.daemon(true, (stdin || stdout || stderr))
25
+ end
26
+
27
+ save_pid_file
28
+
29
+ block.call
30
+ rescue Interrupt, SignalException
31
+ interrupt
32
+ end
33
+ end
34
+
35
+ # Handles an interrupt (`SIGINT` or `SIGTERM`) properly as it
36
+ # deletes the pid file and calles the `stop` method.
37
+ def interrupt
38
+ File.delete(pid_file) if pid_file && File.exists?(pid_file)
39
+ end
40
+
41
+ # Starts the daemon
42
+ #
43
+ # If a PID file was provided it will try to store the current
44
+ # PID. If this files exists it will try to check if the stored PID
45
+ # is already running, in which case insidious will exit with an error
46
+ # code.
47
+ def start!(&block)
48
+ if pid_file && File.exists?(pid_file)
49
+ begin
50
+ Process.kill(0, pid)
51
+ STDERR.puts("Daemon is already running with PID #{pid}")
52
+ exit 2
53
+ rescue Errno::ESRCH
54
+ run!(&block)
55
+ end
56
+ else
57
+ if (pid_file.nil? && daemonize)
58
+ STDERR.puts('No PID file is set but daemonize is set to true')
59
+ exit 1
60
+ end
61
+
62
+ run!(&block)
63
+ end
64
+ end
65
+
66
+ # Stops the daemon execution
67
+ #
68
+ # This method only works when a PID file is given, otherwise it will
69
+ # exit with an error.
70
+ def stop!
71
+ if pid_file && File.exists?(pid_file)
72
+ begin
73
+ Process.kill(:INT, pid)
74
+ File.delete(pid_file)
75
+ rescue Errno::ESRCH
76
+ STDERR.puts("No daemon is running with PID #{pid}")
77
+ exit 3
78
+ end
79
+ else
80
+ STDERR.puts("Couldn't find the PID file: '#{pid_file}'")
81
+ exit 1
82
+ end
83
+ end
84
+
85
+ # Restarts the daemon
86
+ def restart!(&block)
87
+ stop!
88
+ start!(&block)
89
+ end
90
+
91
+ # Get the pid from the pid_file
92
+ def pid
93
+ File.read(@pid_file).strip.to_i
94
+ end
95
+
96
+ # Returns `true` if the daemon is running
97
+ def running?
98
+ # First check if we have a pid file and if it exists
99
+ if pid_file.nil? || !File.exists?(pid_file)
100
+ return false
101
+ end
102
+
103
+ begin
104
+ Process.getpgid(pid)
105
+ true
106
+ rescue Errno::ESRCH
107
+ false
108
+ end
109
+ end
110
+
111
+ # Returns true if insidious is running as a daemon which is the default
112
+ def daemon?
113
+ @daemonize
114
+ end
115
+
116
+ # Changes the working directory
117
+ #
118
+ # All paths will be relative to the working directory unless they're
119
+ # specified as absolute paths.
120
+ #
121
+ # @param [String] path of the new workng directory
122
+ def chdir!(path)
123
+ Dir.chdir(File.absolute_path(path))
124
+ end
125
+
126
+ # Set the path where the PID file will be created
127
+ def pid_file=(path)
128
+ @pid_file = File.absolute_path(path)
129
+ end
130
+
131
+ # Reopens `STDIN` for reading from `path`
132
+ #
133
+ # This path is relative to the working directory unless an absolute
134
+ # path is given.
135
+ def stdin=(path)
136
+ @stdin = File.absolute_path(path)
137
+ STDIN.reopen(@stdin)
138
+ end
139
+
140
+ # Reopens `STDOUT` for writing to `path`
141
+ #
142
+ # This path is relative to the working directory unless an absolute
143
+ # path is given.
144
+ def stdout=(path)
145
+ @stdout = File.absolute_path(path)
146
+ STDOUT.reopen(@stdout, 'a')
147
+ end
148
+
149
+ # Reopens `STDERR` for writing to `path`
150
+ #
151
+ # This path is relative to the working directory unless an absolute
152
+ # path is given.
153
+ def stderr=(path)
154
+ @stderr = File.absolute_path(path)
155
+ STDERR.reopen(stderr, 'a')
156
+ end
157
+
158
+ private
159
+
160
+ # Save the falen angel PID to the PID file specified in `pid_file`
161
+ def save_pid_file
162
+ File.open(pid_file, 'w') do |fp|
163
+ fp.write(Process.pid)
164
+ end if pid_file
165
+ end
166
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: insidious
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - James White
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-02 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - dev.jameswhite@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - LICENSE
21
+ - README.md
22
+ - Rakefile
23
+ - lib/insidious.rb
24
+ homepage: https://github.com/jamesrwhite/insidious
25
+ licenses:
26
+ - MIT
27
+ metadata: {}
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - '>='
35
+ - !ruby/object:Gem::Version
36
+ version: 1.9.3g
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 2.2.2
45
+ signing_key:
46
+ specification_version: 4
47
+ summary: A simple and flexible ruby gem for managing daemons.
48
+ test_files: []