Module: Djinn::Base
- Defined in:
- lib/djinn/base.rb,
lib/djinn/base/dsl.rb,
lib/djinn/base/tonic.rb,
lib/djinn/base/logging.rb,
lib/djinn/base/pid_file.rb
Overview
The base class from which all Djinn spring forth
Defined Under Namespace
Modules: Dsl, Logging, Tonic Classes: PidFile
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
-
#handle_exit ⇒ Object
Override this with useful exit code if you need to, but remember to call super or call exit yourself, or your Djinn will be immortal.
- #initialize ⇒ Object
-
#perform(config = {}) ⇒ Object
Base implementation does nothing worthwhile, you should override this in your own implementation.
-
#restart(config = {}) ⇒ Object
Convenience method, really just calls stop and then start for you :P.
-
#run(config = {}) {|_self| ... } ⇒ Object
Starts the Djinn in the foreground, which is often useful for testing or other noble pursuits.
-
#start(config = {}, &block) ⇒ Object
Starts the Djinn in the background.
-
#stop(config = {}) {|_self| ... } ⇒ Object
Stops the Djinn, unless you change the location of the pid file, in which case its all about you and the kill command.
Methods included from Logging
Methods included from Tonic
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
15 16 17 |
# File 'lib/djinn/base.rb', line 15 def config @config end |
Instance Method Details
#handle_exit ⇒ Object
Override this with useful exit code if you need to, but remember to call super or call exit yourself, or your Djinn will be immortal. This is truly terrible code, and will be removed soon.
33 34 35 36 |
# File 'lib/djinn/base.rb', line 33 def handle_exit __exit! if respond_to?(:__exit!) exit(0) end |
#initialize ⇒ Object
17 18 19 |
# File 'lib/djinn/base.rb', line 17 def initialize @config = { :__daemonize => true } end |
#perform(config = {}) ⇒ Object
Base implementation does nothing worthwhile, you should override this in your own implementation.
23 24 25 26 27 28 |
# File 'lib/djinn/base.rb', line 23 def perform config={} while log("[#{name}] Djinn is running.. and doing nothing worthwhile.") sleep(5) end end |
#restart(config = {}) ⇒ Object
Convenience method, really just calls stop and then start for you :P
71 72 73 74 |
# File 'lib/djinn/base.rb', line 71 def restart config={} stop start(config) end |
#run(config = {}) {|_self| ... } ⇒ Object
Starts the Djinn in the foreground, which is often useful for testing or other noble pursuits.
57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/djinn/base.rb', line 57 def run config={}, &block @config.update(config).update(load_config) # @config = (config.empty?) ? load_config : config log "Starting #{name} in the foreground.." trap('TERM') { handle_exit } trap('INT') { handle_exit } yield(self) if block_given? (respond_to?(:__start!)) ? __start! : perform(@config) # If this process doesn't loop or otherwise breaks out of # the loop we still want to clean up after ourselves handle_exit end |
#start(config = {}, &block) ⇒ Object
Starts the Djinn in the background.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/djinn/base.rb', line 39 def start config={}, &block @config.update(config).update(load_config) #@config = (config.empty?) ? load_config : config log "Starting #{name} in the background.." logfile = get_logfile(config) daemonize(logfile, get_pidfile(config)) do yield(self) if block_given? trap('TERM') { handle_exit } trap('INT') { handle_exit } (respond_to?(:__start!)) ? __start! : perform(@config) # If this process doesn't loop or otherwise breaks out of # the loop we still want to clean up after ourselves handle_exit end end |
#stop(config = {}) {|_self| ... } ⇒ Object
Stops the Djinn, unless you change the location of the pid file, in which case its all about you and the kill command
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/djinn/base.rb', line 78 def stop config={} @config.update(config).update(load_config) # @config = (config.empty?) ? load_config : config yield(self) if block_given? __stop! if respond_to?(:__stop!) pidfile = get_pidfile(@config) log 'No such process' and exit unless pidfile.pid begin log "Sending TERM signal to process #{pidfile.pid}" Process.kill("TERM", pidfile.pid) rescue log 'Could not find process' ensure pidfile.remove end end |