Class: N::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/n/application.rb

Overview

Application

the default Application object of the Framework.

Direct Known Subclasses

Cluster, Server, Sync::Server

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = "Unknown") ⇒ Application

Returns a new instance of Application.



41
42
43
44
45
46
# File 'lib/n/application.rb', line 41

def initialize(name = "Unknown")
	@name = name
	@pidfile = "/tmp/#{@name}.pid"
	@create_time = Time.now()
	@status = :stop
end

Instance Attribute Details

#create_timeObject

startup time of the application



32
33
34
# File 'lib/n/application.rb', line 32

def create_time
  @create_time
end

#daemonizedObject (readonly)

Returns the value of attribute daemonized.



34
35
36
# File 'lib/n/application.rb', line 34

def daemonized
  @daemonized
end

#descriptionObject

credentials of the application



22
23
24
# File 'lib/n/application.rb', line 22

def description
  @description
end

#nameObject

credentials of the application



22
23
24
# File 'lib/n/application.rb', line 22

def name
  @name
end

#pidObject (readonly)

the pid of the application



37
38
39
# File 'lib/n/application.rb', line 37

def pid
  @pid
end

#pidfileObject (readonly)

the pid of the application



37
38
39
# File 'lib/n/application.rb', line 37

def pidfile
  @pidfile
end

#statusObject

the status of the application use symbols to denote status. valid status symbols:

  • :run = The server is running

  • :stop = The server is stopped



29
30
31
# File 'lib/n/application.rb', line 29

def status
  @status
end

#titleObject

credentials of the application



22
23
24
# File 'lib/n/application.rb', line 22

def title
  @title
end

#versionObject

credentials of the application



22
23
24
# File 'lib/n/application.rb', line 22

def version
  @version
end

Instance Method Details

#daemon_pidObject

Returns the pid of the running daemon



176
177
178
179
180
181
182
183
# File 'lib/n/application.rb', line 176

def daemon_pid()
	begin
		return File.open(@pidfile).read.chomp.to_i
	rescue
		$log.info "Could not read pid from #{@pidfile}"
		return nil
	end
end

#daemonizeObject

Run the application as a daemon FIXME: handle logging for daemons. ???: How does this work !?!?



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/n/application.rb', line 151

def daemonize
	unless @daemonized
		trap "SIGCLD", "IGNORE"

		pid = fork()
		
		if pid # parent process
			@pid = pid
			File.open(@pidfile, 'w') {|f| f << "#{pid}\n" }
			exit!
		else # child
			$log.info "Daemon for '#{@name}', process id: #{Process.pid}"
		end

		# change process group and lose control tty
		Process.setpgrp()
	end

	@daemonized = true

	return self
end

#parse_arguments(args = ARGV) ⇒ Object Also known as: exec

FIXME: deleting of arguments is wrong, recode this using GETOPT.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/n/application.rb', line 51

def parse_arguments(args = ARGV)
     
	opts = OptionParser.new { |opts|
		opts.banner = "Usage: application.rb [action]"

		opts.separator ""
		opts.separator "Specific options:"

  		opts.on("-s", "--start",
  				"Start Navel's Application Server.") { 
			start()
  		}

		opts.on("-S", "--stop",
  				"Stop Navel's Application Server.") { 
			stop()
  		}

		opts.on("-r", "--restart",
  				"Restart Navel's Application Server.") { 
			restart()
  		}

		opts.on("-d", "--daemon",
  				"Run Navel's Application Server as a daemon.") { 
			daemonize()
			start()
  		}

		opts.on_tail("-?", "--help", "Show this message.") {
  			puts opts
    		exit
		}
   }   
	begin
		opts.parse!(args)
	rescue => ex
		puts ex
		puts "Please, either specify a valid option or invoke the usage using -? or --help."
	end
end

#restartObject

Override to properly restart the application



111
112
113
114
115
116
117
# File 'lib/n/application.rb', line 111

def restart()
	stop()
	start()

	# to allow for chaining
	return self
end

#runObject

Begin the application loop. Override this method in your application.



142
143
144
145
# File 'lib/n/application.rb', line 142

def run
	# to allow for chaining
	return self
end

#startObject



96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/n/application.rb', line 96

def start()
	raise "Application allready started!" unless :stop == @status

	begin		
		run()
	rescue => ex
		puts ex
	end
	
	# to allow for chaining
	return self
end

#stopObject

Stop the application. Typically kills ths application daemon.



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/n/application.rb', line 121

def stop
	if dpid = daemon_pid()
		begin
			File.delete(@pidfile)
			Process.kill("SIGKILL", dpid)
			$log.info "Successfully killed #{@name} instance, pid=#{dpid}"
		rescue => ex
			$log.error "Cannot kill #{@name} instance, pid=#{dpid}"
			$log.error ex
		end
	else
		$log.error "Cannot locate pid of running instance!"
	end

	# to allow for chaining
	return self
end