Class: ProcessWanker::ServiceMgr

Inherits:
Object
  • Object
show all
Includes:
Log
Defined in:
lib/service_mgr.rb

Constant Summary collapse

CLASS_SUBDIR =
"service_classes"
CLASS_DIR =
File.join(File.dirname(File.expand_path(__FILE__)),CLASS_SUBDIR)

Constants included from Log

Log::DEBUG, Log::ERROR, Log::INFO, Log::WARN

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Log

debug, error, info, log, set_level, warn

Constructor Details

#initializeServiceMgr

initialize



63
64
65
66
67
68
69
70
71
# File 'lib/service_mgr.rb', line 63

def initialize()
  @@instance=self
  @services_by_name={}
@service_classes={}
  @tick_count=0
	
  ProcessUtil::scan_processes()
load_classes()
end

Instance Attribute Details

#reload_configObject

Returns the value of attribute reload_config.



36
37
38
# File 'lib/service_mgr.rb', line 36

def reload_config
  @reload_config
end

#service_classesObject

Returns the value of attribute service_classes.



35
36
37
# File 'lib/service_mgr.rb', line 35

def service_classes
  @service_classes
end

#services_by_nameObject

Returns the value of attribute services_by_name.



33
34
35
# File 'lib/service_mgr.rb', line 33

def services_by_name
  @services_by_name
end

#terminateObject

Returns the value of attribute terminate.



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

def terminate
  @terminate
end

#tick_countObject

Returns the value of attribute tick_count.



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

def tick_count
  @tick_count
end

Class Method Details

.instanceObject



46
47
48
# File 'lib/service_mgr.rb', line 46

def instance
  @@instance
end

.register_service(service) ⇒ Object



49
50
51
# File 'lib/service_mgr.rb', line 49

def register_service(service)
  @@instance.register_service(service)
end

.register_service_class(service_class) ⇒ Object



52
53
54
# File 'lib/service_mgr.rb', line 52

def register_service_class(service_class)
  @@instance.register_service_class(service_class)
end

Instance Method Details

#apply_config(cfg, notboot = true) ⇒ Object



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

def apply_config(cfg,notboot=true)

   @services_by_name={}

	cfg.daemon.services.each do |group_name,services|
		services.services.each do |name,service|
			params=service.params.merge({ :name => service.name, :group_name => group_name })
			if(!notboot)
				params[:initial_state]=:up
			end
			svc=service.klass.new(params)
			
			# the service will register itself
			# we must store its config block
			svc.config_node=service
			
		end
	end
	
	@services_by_name.each do |n,v|
		v.resolve_dependencies()
	end
	
	ProcessUtil::build_known_hashes()

end

#load_classesObject



79
80
81
82
83
84
# File 'lib/service_mgr.rb', line 79

def load_classes()
	$LOAD_PATH << CLASS_DIR unless($LOAD_PATH.include?(CLASS_DIR))
	Dir.glob( File.join(CLASS_DIR,"*.rb") ).each do |fn|
		require( File.basename(fn,".rb") )
	end
end

#match_services(spec) ⇒ Object



136
137
138
139
140
141
142
# File 'lib/service_mgr.rb', line 136

def match_services(spec)
	matched={}
	@services_by_name.each do |sn,s|
		matched[sn]=s if(s.matches_spec(spec))
	end
	matched
end

#register_service(service) ⇒ Object



92
93
94
95
96
# File 'lib/service_mgr.rb', line 92

def register_service(service)
	raise "service is missing name" if(!service.name)
raise "service #{service.name} is multiply defined" if(@services_by_name[service.name])  
  @services_by_name[service.name]=service
end

#register_service_class(service_class) ⇒ Object



104
105
106
# File 'lib/service_mgr.rb', line 104

def register_service_class(service_class)
	@service_classes[ service_class.nice_name ]=service_class
end

#runObject



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/service_mgr.rb', line 183

def run()
 	
 	while(true)
 		
		tick()
		sleep(1)

		#
		# check for requests
		#
		
		if(@reload_config)
			info("reloading configuration")
			ProcessWanker::with_logged_rescue("ServiceMgr - reloading configuration") do
				c=Config::load_config(Config::get_config_path)
				apply_config(c)
			end
			@reload_config=false
		end
		
		if(@terminate)
			info("terminating")
			break
		end

 	end
end

#tickObject

main tick function



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/service_mgr.rb', line 114

def tick()
  
  # scan processes... and kill anything we don't recognize
  ProcessUtil::scan_processes()
  ProcessUtil::kill_unknown()
  ProcessUtil::reap()
  
  # tick each service in turn
  @services_by_name.values.each do |service|
    service.tick()      
  end
  
  @tick_count += 1
  
end