|
10 | 10 | import logging |
11 | 11 | from datetime import datetime |
12 | 12 | from datetime import timezone |
| 13 | +from timeit import default_timer as timer |
13 | 14 | from traceback import format_exc as traceback_format_exc |
14 | 15 | from typing import Iterable |
15 | 16 |
|
16 | 17 | from aboutcode.pipeline import BasePipeline |
17 | 18 | from aboutcode.pipeline import LoopProgress |
| 19 | +from aboutcode.pipeline import humanize_time |
18 | 20 |
|
19 | 21 | from vulnerabilities.importer import AdvisoryData |
20 | 22 | from vulnerabilities.improver import MAX_CONFIDENCE |
|
29 | 31 | class VulnerableCodePipeline(BasePipeline): |
30 | 32 | pipeline_id = None # Unique Pipeline ID |
31 | 33 |
|
| 34 | + def on_failure(self): |
| 35 | + """ |
| 36 | + Tasks to run in the event that pipeline execution fails. |
| 37 | +
|
| 38 | + Implement cleanup or other tasks that need to be performed |
| 39 | + on pipeline failure, such as: |
| 40 | + - Removing cloned repositories. |
| 41 | + - Deleting downloaded archives. |
| 42 | + """ |
| 43 | + pass |
| 44 | + |
| 45 | + def execute(self): |
| 46 | + """Execute each steps in the order defined on this pipeline class.""" |
| 47 | + self.log(f"Pipeline [{self.pipeline_name}] starting") |
| 48 | + |
| 49 | + steps = self.pipeline_class.get_steps(groups=self.selected_groups) |
| 50 | + steps_count = len(steps) |
| 51 | + pipeline_start_time = timer() |
| 52 | + |
| 53 | + for current_index, step in enumerate(steps, start=1): |
| 54 | + step_name = step.__name__ |
| 55 | + |
| 56 | + if self.selected_steps and step_name not in self.selected_steps: |
| 57 | + self.log(f"Step [{step_name}] skipped") |
| 58 | + continue |
| 59 | + |
| 60 | + self.set_current_step(f"{current_index}/{steps_count} {step_name}") |
| 61 | + self.log(f"Step [{step_name}] starting") |
| 62 | + step_start_time = timer() |
| 63 | + |
| 64 | + try: |
| 65 | + step(self) |
| 66 | + except Exception as exception: |
| 67 | + self.log("Pipeline failed") |
| 68 | + on_failure_start_time = timer() |
| 69 | + self.log(f"Running [on_failure] tasks") |
| 70 | + self.on_failure() |
| 71 | + on_failure_run_time = timer() - on_failure_start_time |
| 72 | + self.log(f"Completed [on_failure] tasks in {humanize_time(on_failure_run_time)}") |
| 73 | + |
| 74 | + return 1, self.output_from_exception(exception) |
| 75 | + |
| 76 | + step_run_time = timer() - step_start_time |
| 77 | + self.log(f"Step [{step_name}] completed in {humanize_time(step_run_time)}") |
| 78 | + |
| 79 | + self.set_current_step("") # Reset the `current_step` field on completion |
| 80 | + pipeline_run_time = timer() - pipeline_start_time |
| 81 | + self.log(f"Pipeline completed in {humanize_time(pipeline_run_time)}") |
| 82 | + |
| 83 | + return 0, "" |
| 84 | + |
32 | 85 | def log(self, message, level=logging.INFO): |
33 | 86 | """Log the given `message` to the current module logger and execution_log.""" |
34 | 87 | now_local = datetime.now(timezone.utc).astimezone() |
|
0 commit comments