Skip to content

Commit 43b73d6

Browse files
committed
Added run and wait method for scenarios
1 parent 57d3916 commit 43b73d6

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

dataikuapi/dss/scenario.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
from datetime import datetime
2+
import time
3+
from dataikuapi.utils import DataikuException
4+
25

36
class DSSScenario(object):
47
"""
@@ -26,6 +29,50 @@ def run(self, params={}):
2629
"POST", "/projects/%s/scenarios/%s/run" % (self.project_key, self.id), body=params)
2730
return DSSTriggerFire(self, trigger_fire)
2831

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+
2976
def get_last_runs(self, limit=10, only_finished_runs=False):
3077
"""
3178
Get the list of the last runs of the scenario.
@@ -191,3 +238,6 @@ def get_scenario_run(self):
191238
return None
192239
else:
193240
return DSSScenarioRun(self.client, run['scenarioRun'])
241+
242+
def is_cancelled(self):
243+
return self.trigger_fire["cancelled"]

0 commit comments

Comments
 (0)