Sha256: 0240ca8cad0ddf10c52ab663da2b611482e62a75b3e7535e9a1017fcf83faace

Contents?: true

Size: 847 Bytes

Versions: 2

Compression:

Stored size: 847 Bytes

Contents

# Helper for running threads in the background, with a timeout
# and error logging.
#
# Example:
#
#    WorkerThread.new.start :timeout => 5.minutes do 
#        # long running task here...
#        sleep 10.0
#    end
#
class WorkerThread

	def start(options = nil)
		raise 'background_task needs a block' unless block_given?

		options ||= {}

		worker = Thread.new do
			begin
				yield
			rescue => e
				$stderr.puts "#{Time.now}\t#{e.class.to_s}\t#{e.message}\n"
				raise e
			end
		end

		# if the user set a timeout then we need a thread to monitor
		# the worker to make sure it doesn't run too long
		if !options[:timeout].nil?
			Thread.new do
				sleep options[:timeout].to_f
				
				if worker.status != false
					#$stderr.puts "#{Time.now}\tbackground_task thread timeout\n"
					worker.kill
				end
			end
		end

		worker
	end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
udp_rest-0.9.2 lib/worker_thread.rb
udp_rest-0.9.0 lib/worker_thread.rb