|
1 | 1 | from datetime import datetime |
| 2 | +import time |
| 3 | +from dataikuapi.utils import DataikuException |
| 4 | + |
2 | 5 |
|
3 | 6 | class DSSScenario(object): |
4 | 7 | """ |
@@ -26,6 +29,50 @@ def run(self, params={}): |
26 | 29 | "POST", "/projects/%s/scenarios/%s/run" % (self.project_key, self.id), body=params) |
27 | 30 | return DSSTriggerFire(self, trigger_fire) |
28 | 31 |
|
| 32 | + def get_trigger(self, trigger_id, run_id): |
| 33 | + """ |
| 34 | + Requests a trigger of the run of a scenario |
| 35 | +
|
| 36 | + Args: |
| 37 | + trigger_id: Id of trigger |
| 38 | + run_id: Id of associated run |
| 39 | +
|
| 40 | + Returns: |
| 41 | + A :class:`dataikuapi.dss.admin.DSSTriggerFire` trigger handle |
| 42 | + """ |
| 43 | + trigger_fire = self.client._perform_json( |
| 44 | + "GET", "/projects/%s/scenarios/%s/get-trigger/%s" % (self.project_key, self.id, trigger_id), params={ |
| 45 | + 'runId' : run_id |
| 46 | + }) |
| 47 | + return DSSTriggerFire(self, trigger_fire) |
| 48 | + |
| 49 | + def run_and_wait(self, params={}): |
| 50 | + """ |
| 51 | + Requests a run of the scenario, which will start after a few seconds. Wait the end of the run to complete. |
| 52 | +
|
| 53 | + Args: |
| 54 | + params: additional parameters that will be passed to the scenario through trigger params |
| 55 | +
|
| 56 | + Returns: |
| 57 | + A :class:`dataikuapi.dss.admin.DSSScenarioRun` run handle |
| 58 | + """ |
| 59 | + trigger_fire = self.run(params) |
| 60 | + scenario_run = None |
| 61 | + refresh_trigger_counter = 0 |
| 62 | + while scenario_run is None: |
| 63 | + refresh_trigger_counter += 1 |
| 64 | + if refresh_trigger_counter == 10: |
| 65 | + refresh_trigger_counter = 0 |
| 66 | + trigger_fire = self.get_trigger(trigger_fire.trigger_id, trigger_fire.run_id) |
| 67 | + if trigger_fire.is_cancelled(): |
| 68 | + raise DataikuException("Scenario run has been cancelled") |
| 69 | + scenario_run = trigger_fire.get_scenario_run() |
| 70 | + time.sleep(5) |
| 71 | + while not scenario_run.run.get('result', False): |
| 72 | + scenario_run = trigger_fire.get_scenario_run() |
| 73 | + time.sleep(60) |
| 74 | + return scenario_run |
| 75 | + |
29 | 76 | def get_last_runs(self, limit=10, only_finished_runs=False): |
30 | 77 | """ |
31 | 78 | Get the list of the last runs of the scenario. |
@@ -191,3 +238,6 @@ def get_scenario_run(self): |
191 | 238 | return None |
192 | 239 | else: |
193 | 240 | return DSSScenarioRun(self.client, run['scenarioRun']) |
| 241 | + |
| 242 | + def is_cancelled(self): |
| 243 | + return self.trigger_fire["cancelled"] |
0 commit comments