Auto trigger in Scenario

Options
SMish8134
SMish8134 Registered Posts: 1

For a scenario, I wish to add a auto trigger where I wish to run it when two particular scenarios are run successfully.

I have a scenario C, which I wish to auto trigger after scenario A and B are both run successfully and not just one of them. Can you please provide me the custom trigger python code to write to achieve this. I am confused

Tagged:

Answers

  • Zach
    Zach Dataiker, Dataiku DSS Core Designer, Dataiku DSS Adv Designer, Registered Posts: 153 Dataiker
    edited July 17
    Options

    Hi @SMish8134
    ,

    It's possibly to run a scenario from a step in another scenario, so if possible, I recommend modifying scenario C so that it runs scenario A and scenario B as the first steps. For details, please see this post: Run Scenario after multiple scenarios have completed.

    If the above doesn't work with your use case, then it is possible to create a custom trigger that triggers only when scenario A and scenario B have both completed successfully.

    Here's the code for the trigger. Be sure to replace the scenario IDs at line 24 with your actual scenario IDs.

    import sys
    
    import dataiku
    from dataiku.scenario import Trigger
    
    
    def get_last_run_time(scenario):
        """Get the time that the scenario last completed successfully
        
        Returns `None` if the scenario has never completed successfully
        """
        try:
            last_run = scenario.get_last_successful_run()
        except ValueError:
            return None
            
        return last_run.start_time
    
    
    t = Trigger()
    client = dataiku.api_client()
    project = client.get_default_project()
    
    # This scenario
    scenario = project.get_scenario("SCENARIO_C")
    # Scenarios that you want to watch
    watched1 = project.get_scenario("SCENARIO_A")
    watched2 = project.get_scenario("SCENARIO_B")
    
    this_time = get_last_run_time(scenario)
    watched1_time = get_last_run_time(watched1)
    watched2_time = get_last_run_time(watched2)
    
    if watched1_time is None or watched2_time is None:
        print("Watched scenarios have never run. Aborting")
        sys.exit()
        
    if this_time is None:
        print("This scenario has never run. Triggering")
        t.fire()
        sys.exit()
        
    if watched1_time > this_time and watched2_time > this_time:
        print("Watched scenarios have both run recently. Triggering")
        t.fire()
    else:
        print("This scenario has run recently. Aborting")

    Thanks,

    Zach

Setup Info
    Tags
      Help me…