Class: ProcessWanker::ProcessService

Inherits:
Service
  • Object
show all
Includes:
Log
Defined in:
lib/service_classes/process_service.rb

Direct Known Subclasses

PIDService

Constant Summary

Constants included from Log

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

Instance Attribute Summary

Attributes inherited from Service

#attempt_count, #config_node, #current_state, #dependencies, #ignore, #last_action_time, #last_fail_time, #last_transition_time, #params, #prev_state, #show_state, #stable, #suppress, #want_state, #want_state_mode

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Log

debug, error, info, log, set_level, warn

Methods inherited from Service

#check_action_delay, #extract_params, #group_name, #matches_single, #matches_spec, #name, #resolve_dependencies, #safe_do, #safe_do_ping, #safe_do_start, #safe_do_stop, #set_want_state, #tick, #validate_params

Constructor Details

#initialize(iparams) ⇒ ProcessService

iparams is a hash containing:

:start_cmd
:start_dir (optional)
:stop_cmd (optional)
:run_user (optional)
:soft_kill_limit (optional)

plus anything to be passed to Service



44
45
46
47
48
49
50
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
# File 'lib/service_classes/process_service.rb', line 44

def initialize(iparams)

# extract parameters
extract_params(
	iparams,
	[
		:start_cmd,
		:start_dir,
		:stop_cmd,
		:run_user,
		:soft_kill_limit,
	])
	
# set defaults
	@params=
	{
		:soft_kill_limit		=>		3,
		:start_dir 					=> 		"/"
	}.merge(@params)

raise "service has no start_cmd" if(!@params[:start_cmd])

# determine run_user properties
if(@params[:run_user])
	if(@params[:run_user].class == String)
		@params[:run_user]=Etc.getpwnam(@params[:run_user])
	elsif(@params[:run_user].class == Fixnum)
		@params[:run_user]=Etc.getpwuid(@params[:run_user].to_i)
	else
		raise "bad run_user parameter for process_service"
	end

	# verify we can switch to this uid if necessary
	current_uid=Process.euid()
	if(current_uid != 0 && current_uid != @params[:run_user].uid)
		raise "can't have a :run_user parameter unless we are running as root, or the uids match"
	end
	
end

  super(iparams)    
end

Class Method Details

.nice_nameObject



26
27
28
# File 'lib/service_classes/process_service.rb', line 26

def self.nice_name
	"process_service"
end

Instance Method Details

#do_pingObject

ping

return run state of process



182
183
184
# File 'lib/service_classes/process_service.rb', line 182

def do_ping
ProcessUtil::processes[env_hash] ? :up : :down	  
end

#do_start(attempt_count) ⇒ Object

start

fork() and exec() the start_cmd



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/service_classes/process_service.rb', line 95

def do_start(attempt_count)
  info("do_start[#{attempt_count}] for #{self.name}")
  
  Process.fork do

	# close network descriptors
	NetUtil::post_fork()

	# start new session
	Process.setsid()

	# set environment cookie so we can be identified		
	hash=env_hash()
	ENV[ ProcessUtil::ENVIRONMENT_KEY ] = hash if(hash)
	
	# change user/group?
	if(@params[:run_user])

		current_uid=Process.euid()
		current_user=Etc.getpwuid(current_uid)
		
		if(current_uid != @params[:run_user].uid)
			Process.uid=@params[:run_user].uid
			Process.gid=@params[:run_user].gid
			Process.euid=@params[:run_user].uid
			Process.egid=@params[:run_user].gid
			ENV["HOME"]=@params[:run_user].dir
		end					
		
	end
	
	# change directory?
	Dir.chdir(@params[:start_dir])
  
	# redirect inputs/outputs
	STDIN.reopen("/dev/null")		# - closing STDIN causes problems with apache
	file=@params[:log_file] ? @params[:log_file] : "/dev/null"
	STDOUT.reopen(file,"a")
	STDERR.reopen(file,"a")
  
  	# run!
  	Process.exec(@params[:start_cmd])
   
  end
  
end

#do_stop(attempt_count) ⇒ Object

stop

stop the process (either with -TERM or -KILL)



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

def do_stop(attempt_count)

	kl=@params[:soft_kill_limit]
	mode = (kl && attempt_count >= kl) ? :hard : :soft

  info("do_stop[#{attempt_count}]->#{mode} for #{self.name}")
  
if(mode == :soft)
	if(params[:stop_cmd])
		system(params[:stop_cmd])
		return
	end
end	    
  
  # find all processes with matching hash
  procs=ProcessUtil::processes[ env_hash ]
  if(procs)
	procs.each do |pid|
		Process.kill({ :hard => "KILL", :soft => "TERM" }[mode],pid)
	end
  end
	   
end

#env_hashObject

env_hash

returns magic environment cookie that identifies processes belonging to this service.



195
196
197
# File 'lib/service_classes/process_service.rb', line 195

def env_hash()
	Digest::MD5.hexdigest( name )
end