daemon-ogre 1.5.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- ZTliYTU5NmU3YjJjYTI0YzAwYzdjODhiYWZhMzdlMzk5ZmQ5ZGFkOQ==
5
- data.tar.gz: !binary |-
6
- MGY0M2QzZWZmNjczMTEwOTE5MmRkOWIzMDRmMzFjYWJjYTIzZThjNg==
2
+ SHA1:
3
+ metadata.gz: b48b7bf70cfcc86e840c955ade329eee5e2c93d9
4
+ data.tar.gz: 390384cf25cd89b59c855d8a6c53a4fa8ac76a5a
7
5
  SHA512:
8
- metadata.gz: !binary |-
9
- MmE5OGZjMGQxOTIwYzA1MDZiYWU0ZjExNzdmYzJlOWE0NTEyMjUzOTAxOTEy
10
- OWMwZGZlYjdkN2NhMTBlMzYxYzhhNTIwYzllZjViMDhjMjgwMGZiMWFjNGJh
11
- Nzg0NmMyMTQ3MzM3NjYwMzk5YzgxOWQ2NTJmNDg4NGIzZGU3OWU=
12
- data.tar.gz: !binary |-
13
- ODVkOTNjOTIxNGMzNjEzYjlmNDhjOGE2ZmUyYzAwODhiZDIwZGE3ZjI1MGE5
14
- MmZmMTVmZGQ0ZTkzNWFmYmIwNjdkYzIxMDE3YzZjNTFmNzRlZTQxNmIyN2U5
15
- OWQ0ZjM4OTM1YmIzZDcxNTZmYTEzOThhN2JlYTgxNGJhYjkyNTg=
6
+ metadata.gz: ee07df0736ac91c0b7adc146b5fe8702996dce03935c52cf6ab94ce01a094be7821d626ba4fadc2750d266dd806835b5bb69e4b89f441e31fde5af68ca42d9a7
7
+ data.tar.gz: 1c4f03bf3b2b86b6c8710f8cf0901b3cf96c4cc6d0825094f91b0807a1370b27451741375876990d2a7f79a5208f392028637e67c4c90eace0c5d08603161983
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg
9
+ rdoc
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp
14
+
15
+ # YARD artifacts
16
+ .yardoc
17
+ _yardoc
18
+ doc/
data/Gemfile.lock CHANGED
@@ -1,26 +1,21 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ daemon-ogre (2.0.0)
5
+ argv (>= 2.0.1)
6
+ tmp (>= 2.0.1)
7
+
1
8
  GEM
2
9
  remote: http://rubygems.org/
3
10
  specs:
4
- columnize (0.3.6)
5
- debugger (1.5.0)
6
- columnize (>= 0.3.1)
7
- debugger-linecache (~> 1.2.0)
8
- debugger-ruby_core_source (~> 1.2.0)
9
- debugger-linecache (1.2.0)
10
- debugger-ruby_core_source (1.2.0)
11
- git (1.2.5)
12
- jeweler (1.8.4)
13
- bundler (~> 1.0)
14
- git (>= 1.2.5)
15
- rake
16
- rdoc
11
+ argv (2.0.1)
17
12
  rake (10.0.4)
18
- rdoc (3.9.5)
13
+ tmp (2.1.0)
19
14
 
20
15
  PLATFORMS
21
16
  ruby
22
17
 
23
18
  DEPENDENCIES
24
- debugger
25
- jeweler (~> 1.8.4)
26
- rdoc
19
+ bundler
20
+ daemon-ogre!
21
+ rake
data/README.md CHANGED
@@ -4,23 +4,8 @@ daemon-ogre
4
4
  Description
5
5
  -----------
6
6
 
7
- WARNING!
8
- The gem will be not updated in the future but redirected to my other project:
9
- Procemon
10
- https://github.com/adamluzsi/procemon
11
- All the functions are implemented and upgraded in all meaning there what you liked in daemon-ogre
12
-
13
-
14
- This gem is made for one purpose.
15
-
16
- Ruby meant to be for easy use, not hardcore coding!
17
- And in terms for this, ogre will aid you in the brute way, so you can relax...
18
-
19
-
20
- so all you need is enjoy your code!
21
- Follow me on Github and send request,
22
- if you have idea what can be usefull in creating your app :)
23
-
7
+ This module made for easy argv based damonize for the app.
8
+ 100% compatible with any framework
24
9
 
25
10
 
26
11
  Installation
@@ -31,209 +16,26 @@ Installation
31
16
  $ gem install 'daemon-ogre'
32
17
 
33
18
 
34
- require 'daemon-ogre'
35
- DaemonOgre.start
36
-
37
-
38
- but we love config our beloved App!
39
- -----------------------------------
40
-
41
-
42
- DaemonOgre.start :name => "MySuperAppName!", #this will be the name of the application
43
- :log_path => "./var/log/log_file_name", #this will be the logfile place and name
44
- :pid_path => "./var/pid/pid_file_name", #this will be the pidfile place and name
45
- :terminate => true #this command not let start your code if it's not started
46
- # with "start" arguments like :
47
- # ruby my_awsome_app.rb start
48
-
49
-
50
- In use?
51
- -------
52
-
53
-
54
- you can simply use your terminal with arguments like
55
- * start - for start the process if you choosed :terminate => true in code like above
56
- start command will check does the application already running or not, so you dont have to worry about
57
-
58
- * stop - will terminate the running application
59
- * restart - will stop the process if that is already running than start again
60
- * debug or -d - this will show the fancy loadings if you want look busy by coding big stuffs :) (or debug your code)
61
- * debugger - this will call the ruby debugger gem to aid you in trouble
62
- you should use these commands:
63
-
64
-
65
- set autolist on #show the code, and the position like and editor, in every step.
66
- s #step in any kind of method, beware to use carefully because you end up in really deep somewhere in Alice world
67
- n #step to next code/codeblock etc, without enter it (usefull when you find the right place)
68
- l #list your current position
69
- #simply hit enter to repeate the last one
70
-
71
-
72
- * daemon -this will fork a child process by the given parameters
73
- * log or -l -you can set the log file position n by terminal for one time run
74
- * pid or -p -you can set the pid file position n name by terminal for one time run
75
- * status or -s -this will tell you ,does your process is running not
76
- * help -this will invoke some help to the terminal if you out of luck and forget everything from this page
77
- * clear -this will remove every log by the prev given paths, usefull in debugging
78
-
79
- example:
80
-
81
- ruby my_awsome_app.rb start debugger
82
- ruby my_awsome_app.rb start clear
83
- ruby my_awsome_app.rb status
84
- ruby my_awsome_app.rb stop
85
- ruby my_awsome_app.rb debug
86
-
87
-
88
- or if you dont use terminate command , than you can start simply as how you do normaly
89
-
90
-
91
-
92
- othere stuffs to use:
93
- ---------------------
94
- everybody love: require_relative...
95
- so why should not try require_directory instead all the fuss
96
- you can tell to it if you want some file to be excluded or just delayed loaded,
97
- because you want them loaded in the last place
98
- Example:
99
-
100
-
101
- require_directory "some_dir_name_from_here_where_are_multi_dir_levels"
102
-
103
- or
104
-
105
- require_directory "some_dir_name_from_here_where_are_multi_dir_levels",
106
- :delayed => ["files","to","be","delayed","in","load"],
107
- :excluded => ["files","to","be","excluded","in","load"],
108
- :type => "rb"
109
-
110
- remember, you can use any of those modifier hash-tag by it self only! :)
111
- by default the type will be .rb files
112
-
113
-
114
- Helpers
115
- -------
116
-
117
- and ofc what else what we love if not our beloved yml-s
118
- we should use a nice Config constant for this(or at least i love to do)
119
-
120
- CONFIG = require_ymls "some_dir_name_from_here_where_are_the_yml_files_in_multi_dir_level"
121
-
122
- the file names will be the hash-key and under that , there will be the yml file datas loaded in
123
-
124
- And if we are at hashs, than when you need remove an element perm, or not perm you can use
125
- * remove will return the hash w/o the the key
126
- * remove! will delete the key, than return the hash
127
- example:
128
-
129
- CONFIG.remove key
130
- CONFIG.remove! key
131
-
132
-
133
-
134
-
135
-
136
- if you need get a free port in a range or from, you can use this:
137
-
138
- get_port(from_nmb,to_nmb,host)
139
-
140
- or by simply
141
-
142
- get_port(number)
143
-
144
-
145
- and if you hate find all the bugs... you can use error_logger at your command like this if you running your process on
146
- background and cannot puts on the terminal:
147
-
148
-
149
- exlogger msg, #anything after this is optionable
150
- :path => "./per_relative-absoluth_path",
151
- :prefix => "it's something..."
152
-
153
-
154
- or with less fuss at the exception handle:
155
-
156
-
157
- begin
158
-
159
- rescue Exception => ex
160
- ex.logger
161
- end
162
-
163
-
164
- or the better way to do Exception handle is to rescue only those exceptions what we are know will be raised! so the app broke if something unexpected stupp come up! (development:)
165
-
166
-
167
- begin
168
- raise ArgumentError, "failed"
169
- rescue ArgumentError => ex
170
- ex.logger
171
- end
172
-
173
-
174
-
175
-
176
- if you need methods from any kind of class without the object methods, you should try this!
177
-
178
-
179
- Xyclassname.class_methods
180
-
181
-
182
- you want make some test script with rnd numbers, strings, dates, bools etc? use the Rnd class at your command
183
-
184
-
185
- Rnd
186
- -string
187
- -integer
188
- -boolean
189
- -date
190
-
191
-
192
- examples:
193
-
194
-
195
- Rnd.number(100)
196
- Rnd.string(15,2) #for rnd bla bla names
197
-
198
-
199
-
200
- You need get the index of an Array element? you can use:
201
-
202
-
203
-
204
- array_variable.index_of("something)
205
-
206
-
207
- Do you wanted to know , does that process running on that pid or not?
208
-
209
-
210
-
211
- process_running?(nmbr)
212
-
213
-
214
-
215
-
216
- in Short:
217
- ---------
19
+ ```ruby
218
20
 
219
21
  require 'daemon-ogre'
220
- DaemonOgre.start
221
- your_Super_app_code!
22
+ DaemonOgre.init
222
23
 
24
+ ```
223
25
 
26
+ after this, the app will check for the following tags in the input:
27
+ :d, :daemon, :daemonize for daemonize the app
28
+ :k, :kill, :terminate for terminate already initialized app instance
224
29
 
30
+ you can set the output folder if you dont like the default in the system tmp / "#{your_app_folder_name}_daemon"
225
31
 
32
+ ```ruby
226
33
 
227
- Contributing to daemon-ogre
228
- ---------------------------
34
+ #> for example
35
+ DaemonOgre::OPTS.tmp_folder_path File.join(File.dirname(__FILE__),"tmp")
36
+
37
+ ```
229
38
 
230
- * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
231
- * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
232
- * Fork the project.
233
- * Start a feature/bugfix branch.
234
- * Commit and push until you are happy with your contribution.
235
- * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
236
- * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
237
39
 
238
40
  Copyright
239
41
  ---------
@@ -242,5 +44,4 @@ Right to copy and use at your service fellow coder
242
44
  Copyright (c) 2013 adam.luzsi. See LICENSE.txt for
243
45
  further details.
244
46
 
245
-
246
47
  daemon-ogre
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.5.0
1
+ 2.0.0
data/daemon-ogre.gemspec CHANGED
@@ -1,32 +1,26 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
 
3
- require File.expand_path(File.join(File.dirname(__FILE__),"files.rb"))
4
-
5
3
  Gem::Specification.new do |spec|
6
4
 
7
5
  spec.name = "daemon-ogre"
8
-
9
6
  spec.version = File.open(File.join(File.dirname(__FILE__),"VERSION")).read.split("\n")[0].chomp.gsub(' ','')
10
-
11
- spec.authors = ["Adam.Luzsi"]
12
- spec.email = "adamluzsi@gmail.com"
7
+ spec.authors = ["Adam Luzsi"]
8
+ spec.email = ["adamluzsi@gmail.com"]
9
+ spec.homepage = "http://github.com/adamluzsi/daemon-ogre"
13
10
 
14
11
  spec.description = "Simple to use app ARGV based daemonizer"
15
- spec.summary = %q{DSL for helping make file loads and configuration objects }
12
+ spec.summary = "Simple to use app ARGV based daemonizer"
16
13
 
17
- spec.extra_rdoc_files = [
18
- "LICENSE.txt",
19
- "README.md",
20
- "README.rdoc"
21
- ]
22
-
23
- spec.files = SpecFiles
14
+ spec.files = `git ls-files`.split($/)
24
15
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
25
16
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
26
17
  spec.require_paths = ["lib"]
27
18
 
28
- spec.homepage = "http://github.com/adamluzsi/daemon-ogre"
29
- spec.licenses = ["MIT"]
19
+ spec.add_dependency "argv",">= 2.0.1"
20
+ spec.add_dependency "tmp",">= 2.0.1"
21
+
22
+ spec.add_development_dependency "bundler"
23
+ spec.add_development_dependency "rake"
30
24
 
31
25
  end
32
26
 
@@ -0,0 +1,16 @@
1
+ require 'daemon-ogre.rb'
2
+
3
+ # start as
4
+ # ruby sample_daemon_app.rb --daemon
5
+ #
6
+ # to check it's running
7
+ # `ps aux | grep $0`
8
+
9
+ DaemonOgre.init
10
+
11
+ 60.times do
12
+
13
+ puts Time.now
14
+ sleep 1
15
+
16
+ end
@@ -0,0 +1,29 @@
1
+ module DaemonOgre
2
+
3
+ module DARGV
4
+ class << self
5
+
6
+ def check_args_for *args
7
+
8
+ ::ARGV.flag_syms.each do |element|
9
+ if args.include?(element)
10
+ return true
11
+ end
12
+ end
13
+ return false
14
+
15
+ end
16
+
17
+ def daemonize?
18
+ check_args_for( *[:d,:daemon,:daemonize])
19
+ end
20
+ alias :daemon? :daemonize?
21
+
22
+ def terminate?
23
+ check_args_for( *[:k,:kill,:terminate])
24
+ end
25
+
26
+ end
27
+ end
28
+
29
+ end
@@ -1,65 +1,173 @@
1
- #DaemonOgreBody
2
- begin
3
- module DaemonOgre
4
-
5
- #DaemonEngine
6
- begin
7
- class Daemon
8
- # Checks to see if the current process is the child process and if not
9
- # will update the pid file with the child pid.
10
- def self.start pid, pidfile, outfile, errfile
11
- unless pid.nil?
12
- raise "Fork failed" if pid == -1
13
- write pid, pidfile #for kill we need a pidfile
14
- exit
15
- else
16
- redirect outfile, errfile
1
+ # use Daemon.daemonize or Process.daemonize
2
+ module DaemonOgre
3
+
4
+ module Daemon
5
+
6
+ class << self
7
+
8
+
9
+ # Checks to see if the current process is the child process and if not
10
+ # will update the pid file with the child pid.
11
+ def start pid
12
+ unless pid.nil?
13
+ raise "Fork failed" if pid == -1
14
+ OPTS.pidfile= pid
15
+ ::Kernel.exit
16
+ else
17
+ redirect
18
+ end
19
+ end
20
+
21
+ # Attempts to write the pid of the forked process to the pid file.
22
+ def write pid
23
+ OPTS.pid= pid
24
+
25
+ File.open pidfile, "a+" do |new_line|
26
+ new_line.write "#{pid}\n"
27
+ end
28
+
29
+ rescue ::Exception => e
30
+ $stderr.puts "While writing the PID to file, unexpected #{e.class}: #{e}"
31
+ Process.kill "HUP", pid
32
+ end
33
+
34
+ # Try and read the existing pid from the pid file and signal the
35
+ # process. Returns true for a non blocking status.
36
+ def kill
37
+
38
+ opid = OPTS.pidfile.to_i
39
+ Process.kill 'HUP', opid.to_i
40
+ true
41
+ rescue Errno::ENOENT
42
+ $stdout.puts "#{pidfile} did not exist: Errno::ENOENT" if $DEBUG
43
+ true
44
+ rescue Errno::ESRCH
45
+ $stdout.puts "The process #{opid} did not exist: Errno::ESRCH" if $DEBUG
46
+ true
47
+ rescue Errno::EPERM
48
+ $stderr.puts "Lack of privileges to manage the process #{opid}: Errno::EPERM" if $DEBUG
49
+ false
50
+ rescue ::Exception => e
51
+ $stderr.puts "While signaling the PID, unexpected #{e.class}: #{e}" if $DEBUG
52
+ false
53
+
54
+ end
55
+
56
+ # Send stdout and stderr to log files for the child process
57
+ def redirect
58
+
59
+ $stdin.reopen File.join('','dev','null')
60
+
61
+ out = File.new File.join(OPTS.tmp_folder_path, "out"), "a"
62
+ err = File.new File.join(OPTS.tmp_folder_path, "err"), "a"
63
+
64
+ $stdout.reopen out
65
+ $stderr.reopen err
66
+ $stdout.sync = $stderr.sync = true
67
+
68
+ end
69
+
70
+ def term_kill
71
+ unless OPTS.pidfile.nil?
72
+
73
+ begin
74
+
75
+ STDOUT.puts("PidFile found, processing...") if $DEBUG
76
+ Process.kill('TERM', OPTS.pidfile.to_i)
77
+ return true
78
+
79
+ rescue Exception => ex
80
+
81
+ STDOUT.puts "Exception happened on terminating process: #{ex.class}, #{ex}" if $DEBUG
82
+ return false
83
+
17
84
  end
85
+
86
+ end
87
+ end
88
+
89
+ def terminate
90
+
91
+ # kill methods
92
+ app_killed= false
93
+ [:kill,:term_kill,:kill_by_name].each do |method_name|
94
+ app_killed= self.__send__ method_name
95
+ break if app_killed
18
96
  end
97
+ OPTS.pidfile= nil
98
+
99
+ return app_killed
100
+
101
+ end
102
+
103
+ def kill_by_name
104
+
105
+ # name switch
106
+ target_name ||= $0
107
+ $0 = "ruby_tmp_process"
108
+
19
109
 
20
- # Attempts to write the pid of the forked process to the pid file.
21
- def self.write pid, pidfile
22
- DaemonOgre.create_on_filesystem pidfile
23
- File.open pidfile, "a+" do |new_line|
24
- new_line.write "#{pid}\n"
110
+ app_state= false
111
+
112
+ start_time= Time.now
113
+ while `ps aux | grep #{target_name}`.split(' ')[1] != "" || (Time.now - start_time) < 6
114
+
115
+ begin
116
+
117
+ Process.kill "TERM",`ps aux | grep #{target_name}`.split(' ')[1].to_i
118
+
119
+ app_state= true
120
+ rescue Errno::ESRCH
121
+ $stdout.puts "The process #{target_name} did not exist: Errno::ESRCH" if $DEBUG
122
+ break
123
+ rescue Errno::EPERM
124
+ $stderr.puts "Lack of privileges to manage the process #{target_name}: Errno::EPERM" if $DEBUG
125
+ break
126
+ rescue ::Exception => e
127
+ $stderr.puts "While signaling the PID, unexpected #{e.class}: #{e}" if $DEBUG
128
+ break
25
129
  end
26
- rescue ::Exception => e
27
- $stderr.puts "While writing the PID to file, unexpected #{e.class}: #{e}"
28
- Process.kill "HUP", pid
130
+
131
+ end
132
+
133
+
134
+ # name switch back
135
+ begin
136
+ $0 = target_name
29
137
  end
30
138
 
31
- # Try and read the existing pid from the pid file and signal the
32
- # process. Returns true for a non blocking status.
33
- def self.kill(pidfile)
34
- opid = open("./#{pidfile}").read.strip.to_i
35
- Process.kill 'HUP', opid.to_i
36
- true
37
- rescue Errno::ENOENT
38
- $stdout.puts "#{pidfile} did not exist: Errno::ENOENT" if $DEBUG
39
- true
40
- rescue Errno::ESRCH
41
- $stdout.puts "The process #{opid} did not exist: Errno::ESRCH" if $DEBUG
42
- true
43
- rescue Errno::EPERM
44
- $stderr.puts "Lack of privileges to manage the process #{opid}: Errno::EPERM" if $DEBUG
45
- false
46
- rescue ::Exception => e
47
- $stderr.puts "While signaling the PID, unexpected #{e.class}: #{e}" if $DEBUG
48
- false
139
+ return app_state
140
+ end
141
+
142
+ def stop
143
+
144
+ self.terminate
145
+ ::Process.exit!
146
+
147
+ end
148
+
149
+ def init
150
+
151
+ if DARGV.terminate?
152
+ stop
49
153
  end
50
154
 
51
- # Send stdout and stderr to log files for the child process
52
- def self.redirect outfile, errfile
53
- $stdin.reopen '/dev/null'
54
- out = File.new outfile, "a"
55
- err = File.new errfile, "a"
56
- $stdout.reopen out
57
- $stderr.reopen err
58
- $stdout.sync = $stderr.sync = true
155
+ if DARGV.daemonize?
156
+ start( ::Kernel.fork )
59
157
  end
158
+
60
159
  end
61
- end
62
160
 
161
+ alias :new :init
63
162
 
163
+ end
64
164
  end
165
+
166
+ class << self
167
+ def init
168
+ Daemon.init
169
+ end
170
+ # alias :new :init
171
+ end
172
+
65
173
  end