daemonite 0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (10) hide show
  1. checksums.yaml +7 -0
  2. data/AUTHORS +1 -0
  3. data/COPYING +165 -0
  4. data/Changelog +2 -0
  5. data/README.md +59 -0
  6. data/Rakefile +19 -0
  7. data/TODO +5 -0
  8. data/daemonite.gemspec +19 -0
  9. data/lib/daemonite.rb +152 -0
  10. metadata +52 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9035bd4b47554cc494ad4d131f55a98f29658470
4
+ data.tar.gz: 2fb54ca71a405695d5cc5f2bf42ba883e52d03f8
5
+ SHA512:
6
+ metadata.gz: f18d348471836f7043264ba26cd5c963e463a6f150a4e1fc2d803d3fc89ba3aba45d7a6835e21bb90e43a4a05441ba1b81c33077ba6573a9bb654fbba6a17635
7
+ data.tar.gz: b04555a09ad5d52484d307cdf555c4e9a35bf762977a58325339a68f372678b4c1c52b979876cd27f0e90f8dc53de80b85ce6d1e36bdfbff5459a2b9da805583
data/AUTHORS ADDED
@@ -0,0 +1 @@
1
+ Jürgen Mangler <juergen.mangler@univie.ac.at>
data/COPYING ADDED
@@ -0,0 +1,165 @@
1
+ GNU LESSER GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+
9
+ This version of the GNU Lesser General Public License incorporates
10
+ the terms and conditions of version 3 of the GNU General Public
11
+ License, supplemented by the additional permissions listed below.
12
+
13
+ 0. Additional Definitions.
14
+
15
+ As used herein, "this License" refers to version 3 of the GNU Lesser
16
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
17
+ General Public License.
18
+
19
+ "The Library" refers to a covered work governed by this License,
20
+ other than an Application or a Combined Work as defined below.
21
+
22
+ An "Application" is any work that makes use of an interface provided
23
+ by the Library, but which is not otherwise based on the Library.
24
+ Defining a subclass of a class defined by the Library is deemed a mode
25
+ of using an interface provided by the Library.
26
+
27
+ A "Combined Work" is a work produced by combining or linking an
28
+ Application with the Library. The particular version of the Library
29
+ with which the Combined Work was made is also called the "Linked
30
+ Version".
31
+
32
+ The "Minimal Corresponding Source" for a Combined Work means the
33
+ Corresponding Source for the Combined Work, excluding any source code
34
+ for portions of the Combined Work that, considered in isolation, are
35
+ based on the Application, and not on the Linked Version.
36
+
37
+ The "Corresponding Application Code" for a Combined Work means the
38
+ object code and/or source code for the Application, including any data
39
+ and utility programs needed for reproducing the Combined Work from the
40
+ Application, but excluding the System Libraries of the Combined Work.
41
+
42
+ 1. Exception to Section 3 of the GNU GPL.
43
+
44
+ You may convey a covered work under sections 3 and 4 of this License
45
+ without being bound by section 3 of the GNU GPL.
46
+
47
+ 2. Conveying Modified Versions.
48
+
49
+ If you modify a copy of the Library, and, in your modifications, a
50
+ facility refers to a function or data to be supplied by an Application
51
+ that uses the facility (other than as an argument passed when the
52
+ facility is invoked), then you may convey a copy of the modified
53
+ version:
54
+
55
+ a) under this License, provided that you make a good faith effort to
56
+ ensure that, in the event an Application does not supply the
57
+ function or data, the facility still operates, and performs
58
+ whatever part of its purpose remains meaningful, or
59
+
60
+ b) under the GNU GPL, with none of the additional permissions of
61
+ this License applicable to that copy.
62
+
63
+ 3. Object Code Incorporating Material from Library Header Files.
64
+
65
+ The object code form of an Application may incorporate material from
66
+ a header file that is part of the Library. You may convey such object
67
+ code under terms of your choice, provided that, if the incorporated
68
+ material is not limited to numerical parameters, data structure
69
+ layouts and accessors, or small macros, inline functions and templates
70
+ (ten or fewer lines in length), you do both of the following:
71
+
72
+ a) Give prominent notice with each copy of the object code that the
73
+ Library is used in it and that the Library and its use are
74
+ covered by this License.
75
+
76
+ b) Accompany the object code with a copy of the GNU GPL and this license
77
+ document.
78
+
79
+ 4. Combined Works.
80
+
81
+ You may convey a Combined Work under terms of your choice that,
82
+ taken together, effectively do not restrict modification of the
83
+ portions of the Library contained in the Combined Work and reverse
84
+ engineering for debugging such modifications, if you also do each of
85
+ the following:
86
+
87
+ a) Give prominent notice with each copy of the Combined Work that
88
+ the Library is used in it and that the Library and its use are
89
+ covered by this License.
90
+
91
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
92
+ document.
93
+
94
+ c) For a Combined Work that displays copyright notices during
95
+ execution, include the copyright notice for the Library among
96
+ these notices, as well as a reference directing the user to the
97
+ copies of the GNU GPL and this license document.
98
+
99
+ d) Do one of the following:
100
+
101
+ 0) Convey the Minimal Corresponding Source under the terms of this
102
+ License, and the Corresponding Application Code in a form
103
+ suitable for, and under terms that permit, the user to
104
+ recombine or relink the Application with a modified version of
105
+ the Linked Version to produce a modified Combined Work, in the
106
+ manner specified by section 6 of the GNU GPL for conveying
107
+ Corresponding Source.
108
+
109
+ 1) Use a suitable shared library mechanism for linking with the
110
+ Library. A suitable mechanism is one that (a) uses at run time
111
+ a copy of the Library already present on the user's computer
112
+ system, and (b) will operate properly with a modified version
113
+ of the Library that is interface-compatible with the Linked
114
+ Version.
115
+
116
+ e) Provide Installation Information, but only if you would otherwise
117
+ be required to provide such information under section 6 of the
118
+ GNU GPL, and only to the extent that such information is
119
+ necessary to install and execute a modified version of the
120
+ Combined Work produced by recombining or relinking the
121
+ Application with a modified version of the Linked Version. (If
122
+ you use option 4d0, the Installation Information must accompany
123
+ the Minimal Corresponding Source and Corresponding Application
124
+ Code. If you use option 4d1, you must provide the Installation
125
+ Information in the manner specified by section 6 of the GNU GPL
126
+ for conveying Corresponding Source.)
127
+
128
+ 5. Combined Libraries.
129
+
130
+ You may place library facilities that are a work based on the
131
+ Library side by side in a single library together with other library
132
+ facilities that are not Applications and are not covered by this
133
+ License, and convey such a combined library under terms of your
134
+ choice, if you do both of the following:
135
+
136
+ a) Accompany the combined library with a copy of the same work based
137
+ on the Library, uncombined with any other library facilities,
138
+ conveyed under the terms of this License.
139
+
140
+ b) Give prominent notice with the combined library that part of it
141
+ is a work based on the Library, and explaining where to find the
142
+ accompanying uncombined form of the same work.
143
+
144
+ 6. Revised Versions of the GNU Lesser General Public License.
145
+
146
+ The Free Software Foundation may publish revised and/or new versions
147
+ of the GNU Lesser General Public License from time to time. Such new
148
+ versions will be similar in spirit to the present version, but may
149
+ differ in detail to address new problems or concerns.
150
+
151
+ Each version is given a distinguishing version number. If the
152
+ Library as you received it specifies that a certain numbered version
153
+ of the GNU Lesser General Public License "or any later version"
154
+ applies to it, you have the option of following the terms and
155
+ conditions either of that published version or of any later version
156
+ published by the Free Software Foundation. If the Library as you
157
+ received it does not specify a version number of the GNU Lesser
158
+ General Public License, you may choose any version of the GNU Lesser
159
+ General Public License ever published by the Free Software Foundation.
160
+
161
+ If the Library as you received it specifies that a proxy can decide
162
+ whether future versions of the GNU Lesser General Public License shall
163
+ apply, that proxy's public statement of acceptance of any version is
164
+ permanent authorization for you to choose that version for the
165
+ Library.
@@ -0,0 +1,2 @@
1
+ 0.1 "nothing to see"
2
+ * Initial version
@@ -0,0 +1,59 @@
1
+ # Damonite
2
+
3
+ Copyright (C) 2004-2018 Jürgen "eTM" Mangler <juergen.mangler@gmail.com>
4
+
5
+ Daemonite is freely distributable according to the terms of the GNU Lesser General
6
+ Public License 3.0 (see the file 'COPYING').
7
+
8
+ This code is distributed without any warranty. See the file 'COPYING' for
9
+ details.
10
+
11
+ ## Introduction
12
+
13
+ Deamonite is just syntactic sugar around Process.daemon and argparse, which are
14
+ part of standard ruby. An it only works on Linux because it runs 'ps ax'. I
15
+ know, thats clumsy and doesn't run on windows. Please contribute a better
16
+ solution if you have to.
17
+
18
+ ## Usage
19
+
20
+ Its fairly simple for now:
21
+
22
+ ```ruby
23
+ Daemonite.new do |opts|
24
+ opts['bla']
25
+
26
+ run do |opts|
27
+ p opts
28
+ sleep 1
29
+ end
30
+ end.loop!
31
+ ```
32
+
33
+ Everything inside the #new block is executed once. Everything inside the #run
34
+ block is executed periodically. So make sure to include sleep to not bog down
35
+ your CPU. #loop! finally starts a periodical loop.
36
+
37
+ In order to override options, or provide your own options at start:
38
+
39
+ ```ruby
40
+ Daemonite.new(opts)
41
+ ```
42
+
43
+ Also be aware that a json configuration file $PROGRAM_NAME.sub /\.rb$/, '.conf'
44
+ is automatically read, if it exists.
45
+
46
+ ## Usage - Alternative
47
+
48
+ ```ruby
49
+ daemon = Daemonite.new(opts)
50
+ daemon.run do |opts|
51
+ p opts
52
+ sleep 1
53
+ end
54
+ daemon.loop!
55
+ ```
56
+
57
+ ## TODO
58
+
59
+ 1. An example how to integrate with EventMachine (replace the #loop!).
@@ -0,0 +1,19 @@
1
+ require 'rake'
2
+ require 'rubygems/package_task'
3
+ require 'rake/testtask'
4
+
5
+ spec = eval(File.read('daemonite.gemspec'))
6
+ Gem::PackageTask.new(spec) do |pkg|
7
+ pkg.need_zip = true
8
+ pkg.need_tar = true
9
+ `rm pkg/* -rf`
10
+ `ln -sf #{pkg.name}.gem pkg/daemonite.gem`
11
+ end
12
+
13
+ task :push => :gem do |r|
14
+ `gem push pkg/daemonite.gem`
15
+ end
16
+
17
+ task :install => :gem do |r|
18
+ `sudo gem install pkg/daemonite.gem`
19
+ end
data/TODO ADDED
@@ -0,0 +1,5 @@
1
+ Todo + Ideas:
2
+
3
+ * no functional wishes so far
4
+ * create detailed documention, not only examples
5
+ * create unit test
@@ -0,0 +1,19 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "daemonite"
3
+ s.version = "0.1"
4
+ s.platform = Gem::Platform::RUBY
5
+ s.license = "LGPL-3.0"
6
+ s.summary = "Daemonite - Process.daemon and argparse wrapper."
7
+
8
+ s.description = "Daemonite - Process.daemon and argparse wrapper."
9
+
10
+ s.files = Dir['{example/**/*,lib/*}'] + %w(COPYING Changelog Rakefile daemonite.gemspec README.md AUTHORS TODO)
11
+ s.require_path = 'lib'
12
+ s.extra_rdoc_files = ['README.md']
13
+
14
+ s.required_ruby_version = '>=1.9.3'
15
+
16
+ s.authors = ['Juergen eTM Mangler']
17
+ s.email = 'juergen.mangler@gmail.com'
18
+ s.homepage = 'https://github.com/etm/daemonite.rb'
19
+ end
@@ -0,0 +1,152 @@
1
+ #!/usr/bin/ruby
2
+ # encoding: utf-8
3
+ #
4
+ # This file is part of Daemonite.
5
+ #
6
+ # Daemonite is free software: you can redistribute it and/or modify it under the terms
7
+ # of the GNU General Public License as published by the Free Software Foundation,
8
+ # either version 3 of the License, or (at your option) any later version.
9
+ #
10
+ # Daemonite is distributed in the hope that it will be useful, but WITHOUT ANY
11
+ # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
12
+ # PARTICULAR PURPOSE. See the GNU General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU General Public License along with
15
+ # Daemonite (file COPYING in the main directory). If not, see
16
+ # <http://www.gnu.org/licenses/>.
17
+
18
+ require 'optparse'
19
+ require 'psych'
20
+
21
+ class Daemonite
22
+ OPTS = {
23
+ :mode => :debug,
24
+ :verbose => false,
25
+ :basepath => File.expand_path(File.dirname($0)),
26
+ :pidfile => File.basename($0,'.rb') + '.pid',
27
+ :pidwrite => true,
28
+ :conffile => File.basename($0,'.rb') + '.conf',
29
+ :runtime_options => [],
30
+ :cmdl_parsing => true,
31
+ :cmdl_operation => 'start'
32
+ }
33
+
34
+ def initialize(opts={},&block)
35
+ @opts = OPTS.merge(opts)
36
+
37
+ if File.exists?(@opts[:basepath] + '/' + @opts[:conffile])
38
+ @opts.merge!(Psych::load_file(@opts[:basepath] + '/' + @opts[:conffile]))
39
+ end
40
+
41
+ ########################################################################################################################
42
+ # parse arguments
43
+ ########################################################################################################################
44
+ if @opts[:cmdl_parsing]
45
+ @opts[:cmdl_operation] = "start"
46
+ ARGV.options { |opt|
47
+ opt.summary_indent = ' ' * 4
48
+ opt.banner = "Usage:\n#{opt.summary_indent}ruby #{$PROGRAM_NAME} [options] start|stop|restart|info" + (@opts[:runtime_options].length > 0 ? '|' : '') + @opts[:runtime_options].map{|ro| ro[0]}.join('|') + "\n"
49
+ opt.on("--verbose", "-v", "Do not daemonize. Write ouput to console.") { @opts[:verbose] = true }
50
+ opt.on("--help", "-h", "This text.") { puts opt; exit }
51
+ opt.separator(opt.summary_indent + "start|stop|restart|info".ljust(opt.summary_width+1) + "Do operation start, stop, restart or get information.")
52
+ @opts[:runtime_options].each do |ro|
53
+ opt.separator(opt.summary_indent + ro[0].ljust(opt.summary_width+1) + ro[1])
54
+ end
55
+ opt.parse!
56
+ }
57
+ unless (%w{start stop restart info} + @opts[:runtime_options].map{|ro| ro[0] }).include?(ARGV[0])
58
+ puts ARGV.options
59
+ exit
60
+ end
61
+ @opts[:cmdl_operation] = ARGV[0]
62
+ @at_exit = nil
63
+ end
64
+ ########################################################################################################################
65
+
66
+ @opts[:repeat] = nil
67
+ instance_exec(@opts,&block) if block_given?
68
+
69
+ ########################################################################################################################
70
+ # status and info
71
+ ########################################################################################################################
72
+ pid = File.read(@opts[:basepath] + '/' + @opts[:pidfile]).to_i rescue pid = -1
73
+ status = Proc.new do
74
+ begin
75
+ Process.getpgid pid
76
+ true
77
+ rescue Errno::ESRCH
78
+ false
79
+ end
80
+ end
81
+ if @opts[:cmdl_operation] == "info" && status.call == false
82
+ puts "Server not running"
83
+ exit
84
+ end
85
+ if @opts[:cmdl_operation] == "info" && status.call == true
86
+ puts "Server running as #{pid}"
87
+ begin
88
+ stats = `ps -o "vsz,rss,lstart,time" -p #{pid}`.split("\n")[1].strip.split(/ +/)
89
+ puts "Virtual: #{"%0.2f" % (stats[0].to_f/1024)} MiB"
90
+ puts "Resident: #{"%0.2f" % (stats[1].to_f/1024)} MiB"
91
+ puts "Started: #{stats[2..-2].join(' ')}"
92
+ puts "CPU Time: #{stats.last}"
93
+ rescue
94
+ end
95
+ exit
96
+ end
97
+ if %w{start}.include?(@opts[:cmdl_operation]) && status.call == true
98
+ puts "Server already started"
99
+ exit
100
+ end
101
+
102
+ ########################################################################################################################
103
+ # stop/restart server
104
+ ########################################################################################################################
105
+ if %w{stop restart}.include?(@opts[:cmdl_operation])
106
+ if status.call == false
107
+ puts "Server maybe not started?"
108
+ else
109
+ puts "Server stopped"
110
+ puts "Waiting while server goes down ..."
111
+ while status.call
112
+ Process.kill "SIGTERM", pid
113
+ sleep 0.3
114
+ end
115
+ end
116
+ exit unless @opts[:cmdl_operation] == "restart"
117
+ end
118
+
119
+ ########################################################################################################################
120
+ # go through user defined startup thingis
121
+ ########################################################################################################################
122
+ @opts[:runtime_options].each do |ro|
123
+ ro[2].call(status.call) if @opts[:cmdl_operation] == ro[0]
124
+ end
125
+
126
+ puts "Server started as PID:#{Process.pid}"
127
+ File.write(@opts[:basepath] + '/' + @opts[:pidfile],Process.pid)
128
+ Process.daemon(@opts[:basepath]) unless @opts[:verbose]
129
+ Dir.chdir(@opts[:basepath])
130
+ ::Kernel::at_exit do
131
+ File.unlink(@opts[:basepath] + '/' + @opts[:pidfile])
132
+ @at_exit.call if @at_exit
133
+ end
134
+ end
135
+
136
+ def run(&block)
137
+ @opts[:repeat] = block
138
+ end
139
+ def at_exit(&blk)
140
+ @at_exit = blk
141
+ end
142
+
143
+ def loop!
144
+ begin
145
+ loop do
146
+ @opts[:repeat].call(@opts)
147
+ end unless @opts[:repeat].nil?
148
+ rescue => e
149
+ puts "Server stopped due to error (PID:#{Process.pid})"
150
+ end
151
+ end
152
+ end
metadata ADDED
@@ -0,0 +1,52 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: daemonite
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - Juergen eTM Mangler
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-04-04 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Daemonite - Process.daemon and argparse wrapper.
14
+ email: juergen.mangler@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files:
18
+ - README.md
19
+ files:
20
+ - AUTHORS
21
+ - COPYING
22
+ - Changelog
23
+ - README.md
24
+ - Rakefile
25
+ - TODO
26
+ - daemonite.gemspec
27
+ - lib/daemonite.rb
28
+ homepage: https://github.com/etm/daemonite.rb
29
+ licenses:
30
+ - LGPL-3.0
31
+ metadata: {}
32
+ post_install_message:
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 1.9.3
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ requirements: []
47
+ rubyforge_project:
48
+ rubygems_version: 2.6.6
49
+ signing_key:
50
+ specification_version: 4
51
+ summary: Daemonite - Process.daemon and argparse wrapper.
52
+ test_files: []