| OLD | NEW |
| 1 # Copyright 2015 The Chromium Authors. All rights reserved. | 1 # Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 import time | 5 import time |
| 6 | 6 |
| 7 from common import buildbucket_client | 7 from common import buildbucket_client |
| 8 from model import wf_analysis_status | 8 from model import wf_analysis_status |
| 9 from model.wf_try_job import WfTryJob | 9 from model.wf_try_job import WfTryJob |
| 10 from pipeline_wrapper import BasePipeline | 10 from pipeline_wrapper import BasePipeline |
| 11 from pipeline_wrapper import pipeline | 11 from pipeline_wrapper import pipeline |
| 12 | 12 |
| 13 | 13 |
| 14 class MonitorTryJobPipeline(BasePipeline): | 14 class MonitorTryJobPipeline(BasePipeline): |
| 15 """A pipeline for monitoring a tryjob and recording results when it's done. | 15 """A pipeline for monitoring a tryjob and recording results when it's done. |
| 16 | 16 |
| 17 The result will be stored to compile_results or test_results according to | 17 The result will be stored to compile_results or test_results according to |
| 18 which type of build failure we are running try job for. | 18 which type of build failure we are running try job for. |
| 19 """ | 19 """ |
| 20 | 20 |
| 21 # Arguments number differs from overridden method - pylint: disable=W0221 | 21 # Arguments number differs from overridden method - pylint: disable=W0221 |
| 22 # TODO(chanli): Handle try job for test failures later. | 22 # TODO(chanli): Handle try job for test failures later. |
| 23 def run(self, master_name, builder_name, build_number, try_job_id): | 23 def run( |
| 24 self, master_name, builder_name, build_number, try_job_type, try_job_id): |
| 24 assert try_job_id | 25 assert try_job_id |
| 25 | 26 |
| 26 timeout_hours = 5 # Timeout after 5 hours. | 27 timeout_hours = 5 # Timeout after 5 hours. |
| 27 deadline = time.time() + timeout_hours * 60 * 60 | 28 deadline = time.time() + timeout_hours * 60 * 60 |
| 28 | 29 |
| 29 already_set_started = False | 30 already_set_started = False |
| 30 while True: | 31 while True: |
| 31 error, build = buildbucket_client.GetTryJobs([try_job_id])[0] | 32 error, build = buildbucket_client.GetTryJobs([try_job_id])[0] |
| 32 if error: # pragma: no cover | 33 if error: # pragma: no cover |
| 33 raise pipeline.Retry( | 34 raise pipeline.Retry( |
| 34 'Error "%s" occurred. Reason: "%s"' % (error.message, error.reason)) | 35 'Error "%s" occurred. Reason: "%s"' % (error.message, error.reason)) |
| 35 elif build.status == 'COMPLETED': | 36 elif build.status == 'COMPLETED': |
| 36 result = { | 37 result = { |
| 37 'result': build.result, | 38 'result': build.result, |
| 38 'url': build.url, | 39 'url': build.url, |
| 39 'try_job_id': try_job_id, | 40 'try_job_id': try_job_id, |
| 40 } | 41 } |
| 41 | 42 |
| 42 try_job_result = WfTryJob.Get(master_name, builder_name, build_number) | 43 try_job_result = WfTryJob.Get(master_name, builder_name, build_number) |
| 43 if (try_job_result.compile_results and | 44 if try_job_type == 'compile': |
| 44 try_job_result.compile_results[-1]['try_job_id'] == try_job_id): | 45 result_needs_update = try_job_result.compile_results |
| 45 try_job_result.compile_results[-1].update(result) | 46 else: |
| 47 result_needs_update = try_job_result.test_results |
| 48 if (result_needs_update and |
| 49 result_needs_update[-1]['try_job_id'] == try_job_id): |
| 50 result_needs_update[-1].update(result) |
| 46 else: # pragma: no cover | 51 else: # pragma: no cover |
| 47 try_job_result.compile_results.append(result) | 52 result_needs_update.append(result) |
| 53 try_job_result.put() |
| 54 return result_needs_update[-1] |
| 48 | 55 |
| 49 try_job_result.put() | |
| 50 return try_job_result.compile_results[-1] | |
| 51 else: # pragma: no cover | 56 else: # pragma: no cover |
| 52 if build.status == 'STARTED' and not already_set_started: | 57 if build.status == 'STARTED' and not already_set_started: |
| 53 result = { | 58 result = { |
| 54 'result': None, | 59 'result': None, |
| 55 'url': build.url, | 60 'url': build.url, |
| 56 'try_job_id': try_job_id, | 61 'try_job_id': try_job_id, |
| 57 } | 62 } |
| 58 | 63 |
| 59 try_job_result = WfTryJob.Get(master_name, builder_name, build_number) | 64 try_job_result = WfTryJob.Get(master_name, builder_name, build_number) |
| 60 if (try_job_result.compile_results and | 65 if try_job_type == 'compile': |
| 61 try_job_result.compile_results[-1]['try_job_id'] == try_job_id): | 66 result_needs_update = try_job_result.compile_results |
| 62 try_job_result.compile_results[-1].update(result) | 67 else: |
| 68 result_needs_update = try_job_result.test_results |
| 69 if (result_needs_update and |
| 70 result_needs_update[-1]['try_job_id'] == try_job_id): |
| 71 result_needs_update[-1].update(result) |
| 63 else: # pragma: no cover | 72 else: # pragma: no cover |
| 64 # Normally result for current try job should've been saved in | 73 # Normally result for current try job should've been saved in |
| 65 # schedule_try_job_pipeline, so this branch shouldn't be reached. | 74 # schedule_try_job_pipeline, so this branch shouldn't be reached. |
| 66 try_job_result.compile_results.append(result) | 75 result_needs_update.append(result) |
| 67 | 76 |
| 68 try_job_result.status = wf_analysis_status.ANALYZING | 77 try_job_result.status = wf_analysis_status.ANALYZING |
| 69 try_job_result.put() | 78 try_job_result.put() |
| 70 already_set_started = True | 79 already_set_started = True |
| 71 | 80 |
| 72 time.sleep(60) | 81 time.sleep(60) |
| 73 | 82 |
| 74 if time.time() > deadline: # pragma: no cover | 83 if time.time() > deadline: # pragma: no cover |
| 75 try_job_result.status = wf_analysis_status.ERROR | 84 try_job_result.status = wf_analysis_status.ERROR |
| 76 try_job_result.put() | 85 try_job_result.put() |
| 77 # Explicitly abort the whole pipeline. | 86 # Explicitly abort the whole pipeline. |
| 78 raise pipeline.Abort( | 87 raise pipeline.Abort( |
| 79 'Try job %s timed out after %d hours.' % ( | 88 'Try job %s timed out after %d hours.' % ( |
| 80 try_job_id, timeout_hours)) | 89 try_job_id, timeout_hours)) |
| OLD | NEW |