Class: Worker::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/buzzcore/extra/thread_utils.rb

Constant Summary collapse

TempDirectory =
Dir.tmpdir

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.pid_filenameObject



409
410
411
# File 'lib/buzzcore/extra/thread_utils.rb', line 409

def self.pid_filename
	File.join(TempDirectory, "#{name}.pid")
end

Instance Method Details

#loggerObject



413
414
415
416
417
418
419
# File 'lib/buzzcore/extra/thread_utils.rb', line 413

def logger
	if not @logger
		@logger = Logger.new(STDERR)
		@logger.level = Logger::DEBUG
	end
	@logger
end

#main_procObject



421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
# File 'lib/buzzcore/extra/thread_utils.rb', line 421

def main_proc
	begin
		@is_stopped = false
		@is_started = false
		self.starting()
		@is_started = true
		@is_stopping = false
		while !@is_stopping do
			running();
			logger.debug { "ServiceThread running loop: @is_stopping="+@is_stopping.to_s }
		end
	rescue SystemExit => e  # smother and do nothing
	rescue Exception => e
		logger.warn { "Thread #{@name} #{e.inspect} exception in Starting() or Running()" }
		logger.warn { e.backtrace       }
	ensure
		@is_stopping = true
	end
	
	begin
		stopping()
	rescue Exception => e
		logger.warn { "Thread #{@name} #{e.inspect} exception in stopping()" }
		logger.warn { e.backtrace       }
	end
	logger.info { "Thread #{@name} dropped out"  }
	@is_stopped = true
end

#runningObject



473
474
# File 'lib/buzzcore/extra/thread_utils.rb', line 473

def running
end

#startingObject



470
471
# File 'lib/buzzcore/extra/thread_utils.rb', line 470

def starting
end

#stopObject



466
467
468
# File 'lib/buzzcore/extra/thread_utils.rb', line 466

def stop
	@is_stopping = true
end

#stoppingObject



476
477
# File 'lib/buzzcore/extra/thread_utils.rb', line 476

def stopping
end

#wait_for_started(aTimeout) ⇒ Object

Raises:

  • (Timeout::Error)


450
451
452
453
454
455
456
# File 'lib/buzzcore/extra/thread_utils.rb', line 450

def wait_for_started(aTimeout)
	before = Time.now
	while !@is_started and (Time.now-before) < aTimeout
		sleep(aTimeout / 10)
	end
	raise Timeout::Error.new("failed to start within timeout (#{aTimeout.to_s})") if !@is_started
end

#wait_for_stopped(aTimeout) ⇒ Object

Raises:

  • (Timeout::Error)


458
459
460
461
462
463
464
# File 'lib/buzzcore/extra/thread_utils.rb', line 458

def wait_for_stopped(aTimeout)
	before = Time.now
	while !@is_stopped and (Time.now-before) < aTimeout
		sleep(aTimeout / 10)
	end
	raise Timeout::Error.new("failed to stop within timeout (#{aTimeout.to_s})") if !@is_stopped
end