Custom trigger to run itself?

safi
safi Registered Posts: 3 ✭✭

Hi everyone,

I want to execute scenario again if its fail, so it can try 3 times, sometimes kubernetes fails or, spark fails could be fixed after run again so, i dont miss time range between fail and fix manually.

This is the code created with LLM, it used python for that. I changed project name to variable "project name", normally it written project name already, dont worry.

from dataiku import api_client
import time

def create_retry_trigger(scenario_name, project_key=None, max_retries=3, wait_time=300):
"""
Creates a custom trigger to retry failed scenarios in Dataiku using scenario name (or ID).

Parameters:
scenario_name (str): Scenario ID (or name, if same) to retry
project_key (str): Project key where the scenario is located. If None, uses the current project.
max_retries (int): Maximum number of retry attempts.
wait_time (int): Time to wait between retries in seconds.

Returns:
None
"""
client = api_client()

if project_key is None:
project = client.get_default_project()
else:
project = client.get_project(project_key)

try:
# Directly get scenario by ID (if name matches ID)
scenario = project.get_scenario(scenario_name)

latest_runs = scenario.get_last_runs(1)
if not latest_runs:
print(f"No previous runs found for scenario '{scenario_name}'.")
return

latest_run = latest_runs[0]

if latest_run['result']['outcome'] == 'FAILED':
metadata = scenario.get_custom_metadata()
retry_count = int(metadata.get('retry_count', 0))

if retry_count < max_retries:
scenario.set_custom_metadata({'retry_count': str(retry_count + 1)})

print(f"Retrying scenario '{scenario_name}'. Attempt {retry_count + 1} of {max_retries}")

time.sleep(wait_time)
scenario.run()
else:
print(f"Maximum retry attempts ({max_retries}) reached for scenario '{scenario_name}'")
scenario.set_custom_metadata({'retry_count': "0"})
else:
scenario.set_custom_metadata({'retry_count': "0"})

except Exception as e:
print(f"Error while processing scenario '{scenario_name}': {str(e)}")

# Example usage
create_retry_trigger(
scenario_name="Project_name",
project_key=None, # Use None for the current project or specify a project key
max_retries=3,
wait_time=300 # 5 minutes
)

Operating system used: Windows

Answers

Setup Info
    Tags
      Help me…