daemonite 0.1.2 → 0.2.0

This diff has not been reviewed by any users.
Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +1 -1
  3. data/daemonite.gemspec +1 -1
  4. data/lib/daemonite.rb +56 -39
  5. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 65942d0e1766aaaefd265802b2da578889f1f23e
4
- data.tar.gz: 621e08d9f66f3c7f771d442550ac528434916020
3
+ metadata.gz: 110be5d9b8265d48d494af41bbf77147f4e3888d
4
+ data.tar.gz: 3e5a0e19ce757b0ef8b9c0ed330c730b210825da
5
5
  SHA512:
6
- metadata.gz: b010e060ec3fcd4f45724f7a3104cf633be36b6a624b36f1d4ca57db198e8a433986e86e1890602e4444ac1b79095261bfc3da9c415e42a2be7384cc65cfbb74
7
- data.tar.gz: 6d5ab3d54e5ff67d233c069094d7491245859a80b1256b4ecc48418f641e1c97d8874ca2dd8eb4cb19ac455c9953bc0c9ca2044032234f83ee05e59ddc7471c4
6
+ metadata.gz: 1fc91737dab032004750c06f03c7da6df133657ecda5a88a0b52e431999e86ea60d3b92e914bc6fba74ee68af11da5eb39b8902a23f3a9d5c8993e04a5573bc4
7
+ data.tar.gz: 25df84c14bd7d4f429be0e37faffe4bee2262d86d87f86d445b8a41747a845825457ee37738d19055c2168f6207d519bb1b73f84c03000fd0d0a6481cca97ab4
data/Rakefile CHANGED
@@ -15,5 +15,5 @@ task :push => :gem do |r|
15
15
  end
16
16
 
17
17
  task :install => :gem do |r|
18
- `sudo gem install pkg/daemonite.gem`
18
+ `gem install pkg/daemonite.gem`
19
19
  end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "daemonite"
3
- s.version = "0.1.2"
3
+ s.version = "0.2.0"
4
4
  s.platform = Gem::Platform::RUBY
5
5
  s.license = "LGPL-3.0"
6
6
  s.summary = "Daemonite - Process.daemon and argparse wrapper for loopies."
@@ -18,58 +18,65 @@
18
18
  require 'optparse'
19
19
  require 'psych'
20
20
 
21
- class Daemonite
22
- OPTS = {
21
+ module Daemonism
22
+ DAEMONISM_DEFAULT_OPTS = {
23
23
  :mode => :debug,
24
24
  :verbose => false,
25
25
  :basepath => File.expand_path(File.dirname($0)),
26
26
  :pidfile => File.basename($0,'.rb') + '.pid',
27
27
  :pidwrite => true,
28
28
  :conffile => File.basename($0,'.rb') + '.conf',
29
- :runtime_options => [],
29
+ :runtime_cmds => [],
30
+ :runtime_opts => [],
31
+ :runtime_proc => nil,
32
+ :cmdl_info => nil,
30
33
  :cmdl_parsing => true,
31
34
  :cmdl_operation => 'start'
32
35
  }
33
36
 
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]))
37
+ def daemonism(opts={},&block)
38
+ if File.exists?(opts[:basepath] + '/' + opts[:conffile])
39
+ opts.merge!(Psych::load_file(opts[:basepath] + '/' + opts[:conffile]))
39
40
  end
41
+ Dir.chdir(opts[:basepath])
42
+
43
+ # set more default options and do other stuff
44
+ instance_exec(opts,&block) if block_given?
40
45
 
41
46
  ########################################################################################################################
42
47
  # parse arguments
43
48
  ########################################################################################################################
44
- if @opts[:cmdl_parsing]
45
- @opts[:cmdl_operation] = "start"
49
+ if opts[:cmdl_parsing]
50
+ opts[:cmdl_operation] = "start"
46
51
  ARGV.options { |opt|
47
52
  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 }
53
+ opt.banner = "Usage:\n#{opt.summary_indent}ruby #{$PROGRAM_NAME} [options] start|stop|restart|info" + (opts[:runtime_cmds].length > 0 ? '|' : '') + opts[:runtime_cmds].map{|ro| ro[0]}.join('|') + "\n"
54
+ opts[:runtime_opts].each do |ro|
55
+ opt.on(*ro)
56
+ end
57
+ opt.on("--verbose", "-v", "Do not daemonize. Write ouput to console.") { opts[:verbose] = true }
50
58
  opt.on("--help", "-h", "This text.") { puts opt; exit }
51
59
  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|
60
+ opts[:runtime_cmds].each do |ro|
53
61
  opt.separator(opt.summary_indent + ro[0].ljust(opt.summary_width+1) + ro[1])
54
62
  end
55
63
  opt.parse!
56
64
  }
57
- unless (%w{start stop restart info} + @opts[:runtime_options].map{|ro| ro[0] }).include?(ARGV[0])
65
+ unless (%w{start stop restart info} + opts[:runtime_cmds].map{|ro| ro[0] }).include?(ARGV[0])
58
66
  puts ARGV.options
59
67
  exit
60
68
  end
61
- @opts[:cmdl_operation] = ARGV[0]
69
+ opts[:cmdl_operation] = ARGV[0]
62
70
  @at_exit = nil
63
71
  end
64
72
  ########################################################################################################################
65
-
66
- @opts[:repeat] = nil
67
- instance_exec(@opts,&block) if block_given?
73
+ opts[:repeat] = nil
74
+ opts[:runtime_proc].call(opts) unless opts[:runtime_proc].nil?
68
75
 
69
76
  ########################################################################################################################
70
77
  # status and info
71
78
  ########################################################################################################################
72
- pid = File.read(@opts[:basepath] + '/' + @opts[:pidfile]).to_i rescue pid = -1
79
+ pid = File.read(opts[:basepath] + '/' + opts[:pidfile]).to_i rescue pid = -1
73
80
  status = Proc.new do
74
81
  begin
75
82
  Process.getpgid pid
@@ -78,12 +85,12 @@ class Daemonite
78
85
  false
79
86
  end
80
87
  end
81
- if @opts[:cmdl_operation] == "info" && status.call == false
82
- puts "Server not running"
88
+ if opts[:cmdl_operation] == "info" && status.call == false
89
+ puts "Server #{opts[:cmdl_info].nil? ? ' ' : '(' + opts[:cmdl_info].to_s + ') '}not running"
83
90
  exit
84
91
  end
85
- if @opts[:cmdl_operation] == "info" && status.call == true
86
- puts "Server running as #{pid}"
92
+ if opts[:cmdl_operation] == "info" && status.call == true
93
+ puts "Server #{opts[:cmdl_info].nil? ? ' ' : '(' + opts[:cmdl_info].to_s + ') '}running as #{pid}"
87
94
  begin
88
95
  stats = `ps -o "vsz,rss,lstart,time" -p #{pid}`.split("\n")[1].strip.split(/ +/)
89
96
  puts "Virtual: #{"%0.2f" % (stats[0].to_f/1024)} MiB"
@@ -94,51 +101,61 @@ class Daemonite
94
101
  end
95
102
  exit
96
103
  end
97
- if %w{start}.include?(@opts[:cmdl_operation]) && status.call == true
98
- puts "Server already started"
104
+ if %w{start}.include?(opts[:cmdl_operation]) && status.call == true
105
+ puts "Server #{opts[:cmdl_info].nil? ? ' ' : '(' + opts[:cmdl_info].to_s + ') '}already started"
99
106
  exit
100
107
  end
101
108
 
102
109
  ########################################################################################################################
103
110
  # stop/restart server
104
111
  ########################################################################################################################
105
- if %w{stop restart}.include?(@opts[:cmdl_operation])
112
+ if %w{stop restart}.include?(opts[:cmdl_operation])
106
113
  if status.call == false
107
- puts "Server maybe not started?"
114
+ puts "Server #{opts[:cmdl_info].nil? ? ' ' : '(' + opts[:cmdl_info].to_s + ') '}maybe not started?"
108
115
  else
109
- puts "Server stopped"
116
+ puts "Server #{opts[:cmdl_info].nil? ? ' ' : '(' + opts[:cmdl_info].to_s + ') '}stopped"
110
117
  puts "Waiting while server goes down ..."
111
118
  while status.call
112
119
  Process.kill "SIGTERM", pid
113
120
  sleep 0.3
114
121
  end
115
122
  end
116
- exit unless @opts[:cmdl_operation] == "restart"
123
+ exit unless opts[:cmdl_operation] == "restart"
117
124
  end
118
125
 
119
126
  ########################################################################################################################
120
127
  # go through user defined startup thingis
121
128
  ########################################################################################################################
122
- @opts[:runtime_options].each do |ro|
123
- ro[2].call(status.call) if @opts[:cmdl_operation] == ro[0]
129
+ opts[:runtime_cmds].each do |ro|
130
+ ro[2].call(status.call) if opts[:cmdl_operation] == ro[0]
124
131
  end
125
132
 
126
- puts "Server started as PID:#{Process.pid}"
127
- Process.daemon(@opts[:basepath]) unless @opts[:verbose]
128
- File.write(@opts[:basepath] + '/' + @opts[:pidfile],Process.pid) # after daemon, so that we get the forked pid
129
- Dir.chdir(@opts[:basepath])
133
+ puts "Server #{opts[:cmdl_info].nil? ? ' ' : '(' + opts[:cmdl_info].to_s + ') '}started as PID:#{Process.pid}"
134
+ Process.daemon(opts[:basepath]) unless opts[:verbose]
135
+ File.write(opts[:basepath] + '/' + opts[:pidfile],Process.pid) # after daemon, so that we get the forked pid
136
+ Dir.chdir(opts[:basepath])
130
137
  ::Kernel::at_exit do
131
- File.unlink(@opts[:basepath] + '/' + @opts[:pidfile])
138
+ File.unlink(opts[:basepath] + '/' + opts[:pidfile])
132
139
  @at_exit.call if @at_exit
133
140
  end
134
141
  end
135
142
 
136
- def run(&block)
137
- @opts[:repeat] = block
138
- end
139
143
  def at_exit(&blk)
140
144
  @at_exit = blk
141
145
  end
146
+ end
147
+
148
+ class Daemonite
149
+ include Daemonism
150
+
151
+ def initialize(opts={},&blk)
152
+ @opts = DAEMONISM_DEFAULT_OPTS.merge(opts)
153
+ daemonism @opts, &blk
154
+ end
155
+
156
+ def run(&block)
157
+ @opts[:repeat] = block
158
+ end
142
159
 
143
160
  def loop!
144
161
  begin
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: daemonite
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Juergen eTM Mangler
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-04-04 00:00:00.000000000 Z
11
+ date: 2018-04-24 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Daemonite - Process.daemon and argparse wrapper for loopies.
14
14
  email: juergen.mangler@gmail.com