Class: Sklaventreiber::Worker

Inherits:
Object
  • Object
show all
Defined in:
lib/sklaventreiber/worker.rb

Class Method Summary collapse

Class Method Details

.check_and_hireObject

Check if there are jobs and act accordingly



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
# File 'lib/sklaventreiber/worker.rb', line 52

def self.check_and_hire
	if Sklaventreiber::Config::SKLAVENTREIBER_LOG
 			puts "Sklaventreiber checks the situation."
 		end
	# if there are uncompleted jobs
	if ( Navvy::Job.filter( :failed_at => nil, :completed_at => nil ).count > 0 )
		# then hire worker if necessary
		if ( Sklaventreiber::HerokuConnection.is_fired?() )
			Sklaventreiber::HerokuConnection.hire_worker()
		else
			if Sklaventreiber::Config::SKLAVENTREIBER_LOG
	  			puts "Sklaventreiber is happy."
	  		end
		end
	else
		# else fire worker if necessary
		if ( Sklaventreiber::HerokuConnection.is_hired?() )
			Sklaventreiber::HerokuConnection.fire_worker()	
		else
			if Sklaventreiber::Config::SKLAVENTREIBER_LOG
	  			puts "Sklaventreiber is happy."
	  		end
		end
	end
end

.startObject

Start Sklaventreiber



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/sklaventreiber/worker.rb', line 12

def self.start

	if Sklaventreiber::Config::SKLAVENTREIBER_LOG
 			puts "Sklaventreiber goes to work."
 		end

	trap('TERM') { 
		if Sklaventreiber::Config::SKLAVENTREIBER_LOG
  			puts "Sklaventreiber was interrupted."
  		end 
  		$exit = true;
  	}
	trap( 'INT') {
		if Sklaventreiber::Config::SKLAVENTREIBER_LOG
  			puts "Sklaventreiber was interrupted."
  		end
		$exit = true; 
	}

	Sklaventreiber::HerokuConnection.init()

	loop do
		check_and_hire()

		if ( $exit )
			if Sklaventreiber::Config::SKLAVENTREIBER_LOG
	  			puts "Sklaventreiber leaves work."
	  		end
			break;
		end

		if Sklaventreiber::Config::SKLAVENTREIBER_LOG
  			puts "Sklaventreiber takes a nap."
  		end
		sleep( Sklaventreiber::Config::SKLAVENTREIBER_SLEEPTIME )
	end
end