| 
									
										
										
										
											2016-03-30 12:57:24 +03:00
										 |  |  | module Devops | 
					
						
							|  |  |  |   module Helpers | 
					
						
							|  |  |  |     class JobWaiter | 
					
						
							|  |  |  |       class TimeoutReached < StandardError; end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       INTERVAL = 5
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-11 15:45:55 +03:00
										 |  |  |       # timeout in seconds | 
					
						
							| 
									
										
										
										
											2016-03-30 12:57:24 +03:00
										 |  |  |       def initialize(job_id, timeout=5000) | 
					
						
							|  |  |  |         @job_id, @timeout = job_id, timeout | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def wait | 
					
						
							|  |  |  |         (@timeout / INTERVAL).times do | 
					
						
							|  |  |  |           sleep(INTERVAL) | 
					
						
							|  |  |  |           report = ::Devops::Db.connector.report(@job_id) | 
					
						
							|  |  |  |           case report.status | 
					
						
							|  |  |  |           when Worker::STATUS::COMPLETED | 
					
						
							|  |  |  |             return 0
 | 
					
						
							|  |  |  |           when Worker::STATUS::FAILED | 
					
						
							|  |  |  |             return report.job_result_code | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |         raise TimeoutReached | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |