Class: RSpecBackgroundProcess::BackgroundProcess

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec-background-process/server.rb,
lib/rspec-background-process/background_process.rb

Direct Known Subclasses

LoadedBackgroundProcess

Defined Under Namespace

Modules: Server Classes: ProcessExitedError, ProcessReadyFailedError, ProcessReadyTimeOutError, ProcessRunAwayError, StateError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, cmd, args = [], working_directory = nil, options = {}) ⇒ BackgroundProcess

Returns a new instance of BackgroundProcess.



41
42
43
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
86
87
88
89
90
91
92
93
94
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
# File 'lib/rspec-background-process/background_process.rb', line 41

def initialize(name, cmd, args = [], working_directory = nil, options = {})
	@name = name

	@exec = (Pathname.new(Dir.pwd) + cmd).cleanpath.to_s
	@args = args.map(&:to_s)

	@pid = nil
	@process = nil

	@state_log = []

	case working_directory
	when Array
		working_directory = Dir.mktmpdir(working_directory)
	when nil
		working_directory = Dir.mktmpdir(name.to_s)
	end

	@working_directory = Pathname.new(working_directory.to_s)
	@working_directory.directory? or @working_directory.mkdir

	@pid_file = @working_directory + "#{@name}.pid"
	@log_file = @working_directory + "out.log"

	@options = options
	reset_options(options)

	@fsm_lock = Mutex.new

	@_fsm = MicroMachine.new(:not_running)

	@state_change_time = Time.now.to_f
	@after_state_change = []

	@_fsm.on(:any) do
		@state_change_time = Time.now.to_f
		puts "process is now #{@_fsm.state}"
		@after_state_change.each{|callback| callback.call(@_fsm.state)}
	end

	@_fsm.when(:starting,
		not_running: :starting
	)

	@_fsm.on(:starting) do
		puts "starting: `#{command}`"
		puts "working directory: #{@working_directory}"
		puts "log file: #{@log_file}"
	end

	@_fsm.when(:started,
		starting: :running
	)
	@_fsm.on(:running) do
		puts "running with pid: #{@pid}"
	end

	@_fsm.when(:stopped,
		running: :not_running,
		ready: :not_running
	)

	@_fsm.when(:died,
		starting: :dead,
		running: :dead,
		ready: :dead
	)

	# it is topped before marked failed
	@_fsm.when(:failed,
		not_running: :failed
	)

	@_fsm.when(:verified,
		running: :ready,
		ready: :ready,
	)
	@_fsm.when(:run_away,
		running: :jammed,
		ready: :jammed
	)

	@template_renderer = options[:template_renderer]

	# make sure we stop on exit
	my_pid = Process.pid
	at_exit do
		stop if Process.pid == my_pid and running? #only run in master process
	end
end

Instance Attribute Details

#kill_timeoutObject (readonly)

Returns the value of attribute kill_timeout.



160
161
162
# File 'lib/rspec-background-process/background_process.rb', line 160

def kill_timeout
  @kill_timeout
end

#log_fileObject (readonly)

Returns the value of attribute log_file.



157
158
159
# File 'lib/rspec-background-process/background_process.rb', line 157

def log_file
  @log_file
end

#nameObject (readonly)

Returns the value of attribute name.



154
155
156
# File 'lib/rspec-background-process/background_process.rb', line 154

def name
  @name
end

#pid_fileObject (readonly)

Returns the value of attribute pid_file.



156
157
158
# File 'lib/rspec-background-process/background_process.rb', line 156

def pid_file
  @pid_file
end

#ready_timeoutObject (readonly)

Returns the value of attribute ready_timeout.



158
159
160
# File 'lib/rspec-background-process/background_process.rb', line 158

def ready_timeout
  @ready_timeout
end

#state_change_timeObject (readonly)

Returns the value of attribute state_change_time.



161
162
163
# File 'lib/rspec-background-process/background_process.rb', line 161

def state_change_time
  @state_change_time
end

#state_logObject (readonly)

Returns the value of attribute state_log.



162
163
164
# File 'lib/rspec-background-process/background_process.rb', line 162

def state_log
  @state_log
end

#term_timeoutObject (readonly)

Returns the value of attribute term_timeout.



159
160
161
# File 'lib/rspec-background-process/background_process.rb', line 159

def term_timeout
  @term_timeout
end

#working_directoryObject (readonly)

Returns the value of attribute working_directory.



155
156
157
# File 'lib/rspec-background-process/background_process.rb', line 155

def working_directory
  @working_directory
end

Instance Method Details

#after_state_change(&callback) ⇒ Object



318
319
320
# File 'lib/rspec-background-process/background_process.rb', line 318

def after_state_change(&callback)
	@after_state_change << callback
end

#commandObject



149
150
151
152
# File 'lib/rspec-background-process/background_process.rb', line 149

def command
	# update arguments with actual port numbers, working directories etc. (see template variables)
	Shellwords.join([@exec, *@args.map{|arg| render(arg)}])
end

#dead?Boolean

Returns:

  • (Boolean)


195
196
197
# File 'lib/rspec-background-process/background_process.rb', line 195

def dead?
	state == :dead
end

#exit_codeObject



179
180
181
# File 'lib/rspec-background-process/background_process.rb', line 179

def exit_code
	@process.value.exitstatus if not running? and @process
end

#failed?Boolean

Returns:

  • (Boolean)


199
200
201
# File 'lib/rspec-background-process/background_process.rb', line 199

def failed?
	state == :failed
end

#jammed?Boolean

Returns:

  • (Boolean)


203
204
205
# File 'lib/rspec-background-process/background_process.rb', line 203

def jammed?
	state == :jammed
end

#pidObject



175
176
177
# File 'lib/rspec-background-process/background_process.rb', line 175

def pid
	@pid if starting? or running?
end

#puts(message) ⇒ Object



322
323
324
325
326
# File 'lib/rspec-background-process/background_process.rb', line 322

def puts(message)
	message = "#{name}: #{message}"
	@state_log << message
	super message if @logging
end

#ready?Boolean

Returns:

  • (Boolean)


191
192
193
# File 'lib/rspec-background-process/background_process.rb', line 191

def ready?
	state == :ready
end

#refreshObject



211
212
213
214
215
216
217
218
219
220
221
# File 'lib/rspec-background-process/background_process.rb', line 211

def refresh
	puts 'refreshing'
	cwd = Dir.pwd
	begin
		Dir.chdir(@working_directory.to_s)
		@refresh_action.call(self)
	ensure
		Dir.chdir(cwd)
	end
	self
end

#render(str) ⇒ Object



132
133
134
135
136
137
138
# File 'lib/rspec-background-process/background_process.rb', line 132

def render(str)
	if @template_renderer
		@template_renderer.call(template_variables, str)
	else
		str
	end
end

#reset_options(opts) ⇒ Object



164
165
166
167
168
169
170
171
172
173
# File 'lib/rspec-background-process/background_process.rb', line 164

def reset_options(opts)
	@logging = opts[:logging]

	@ready_timeout = opts[:ready_timeout] || 10
	@term_timeout = opts[:term_timeout] || 10
	@kill_timeout = opts[:kill_timeout] || 10

	@ready_test = opts[:ready_test] || ->(_){true}
	@refresh_action = opts[:refresh_action] || ->(_){restart}
end

#restartObject



223
224
225
226
227
# File 'lib/rspec-background-process/background_process.rb', line 223

def restart
	puts 'restarting'
	stop
	start
end

#running?Boolean

Returns:

  • (Boolean)


183
184
185
# File 'lib/rspec-background-process/background_process.rb', line 183

def running?
	trigger? :stopped # if it can be stopped it must be running :D
end

#startObject



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/rspec-background-process/background_process.rb', line 229

def start
	return self if trigger? :stopped
	trigger? :starting or raise StateError.new(self, 'start', state)

	trigger :starting
	@pid, @process = spawn

	fail "expected 2 values from #spawn, got: #{@pid}, #{@process}" unless @pid and @process

	@process_watcher = Thread.new do
		@process.join
		trigger :died
	end

	trigger :started
	self
end

#starting?Boolean

Returns:

  • (Boolean)


187
188
189
# File 'lib/rspec-background-process/background_process.rb', line 187

def starting?
	state == :starting
end

#stateObject



207
208
209
# File 'lib/rspec-background-process/background_process.rb', line 207

def state
	lock_fsm{|fsm| fsm.state }
end

#stopObject



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/rspec-background-process/background_process.rb', line 247

def stop
	return if trigger? :started
	trigger? :stopped or raise StateError.new(self, 'stop', state)

	# get rid of the watcher thread
	@process_watcher and @process_watcher.kill and @process_watcher.join

	catch :done do
		begin
			if @term_timeout > 0
				puts "terminating process: #{@pid}"
				Process.kill("TERM", @pid)
				@process.join(@term_timeout) and throw :done
				puts "process #{@pid} did not terminate in time"
			end

			if @kill_timeout > 0
				puts "killing process: #{@pid}"
				Process.kill("KILL", @pid)
				@process.join(@kill_timeout) and throw :done
				puts "process #{@pid} could not be killed!!!"
			end
		rescue Errno::ESRCH
			throw :done
		end

		trigger :run_away
		raise ProcessRunAwayError.new(self, @pid)
	end

	trigger :stopped
	self
end

#template_variablesObject



140
141
142
143
144
145
146
147
# File 'lib/rspec-background-process/background_process.rb', line 140

def template_variables
	{
		/working directory/ => -> { working_directory },
		/pid file/ => -> { pid_file },
		/log file/ => -> { log_file },
		/name/ => -> { name },
	}
end

#to_sObject



328
329
330
# File 'lib/rspec-background-process/background_process.rb', line 328

def to_s
	"#{name}[#{@exec}](#{state})"
end

#wait_readyObject



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/rspec-background-process/background_process.rb', line 281

def wait_ready
	trigger? :verified or raise StateError.new(self, 'wait ready', state)

	puts 'waiting ready'

	status = while_running do
		begin
			Timeout.timeout(@ready_timeout) do
				@ready_test.call(self) ? :ready : :failed
			end
		rescue Timeout::Error
			:ready_timeout
		end
	end

	case status
	when :failed
		puts "process failed to pass it's readiness test"
		stop
		trigger :failed
		raise ProcessReadyFailedError.new(self)
	when :ready_timeout
		puts "process not ready in time; see #{log_file} for detail"
		stop
		trigger :failed
		raise ProcessReadyTimeOutError.new(self)
	when Exception
		puts "process readiness check raised error: #{status}; see #{log_file} for detail"
		stop
		trigger :failed
		raise status
	else
		trigger :verified
		self
	end
end