Hey all, Can I get information on the try number w...
# ask-metaflow
m
Hey all, Can I get information on the try number when using the
retry
decorator? I want to run some code, allow it to retry and only if it fails on all retries I want to send a wehook to github. I know that I can use
catch
and check in the next step if an exception was raised, but that makes the process more complected + the failed step will be the second one instead of the first one. Thanks
c
Hi! Here is the retry decorator. You could use it normally and make your own StepDecorator plugin that sends the webhook in Metaflow's
task_finished
lifecycle hook.
d
Another (possibly simpler way) may be to also use this: https://docs.metaflow.org/api/current#current.retry_count and just wrap your function in a regular python decorator (with a try catch and in the catch send the webhook). Note it may not be as robust and won’t work if your machine dies (as opposed to an error in user code) but depending on your needs, that may be something worth considering.
m
@crooked-jordan-29960 do you mean to extend
RetryDecorator
with my own code and there add my implementation of
task_finished
that will send the Github hook? @dry-beach-38304 If I mix
retry
and
catch
decorators I will get an "extra try" from
catch
and their I can check the counter and send message. something like:
Copy code
@retry(times=MAX_RETRIES, minutes_between_retries=0)
    @catch(var="demo_ex")
    @step
    def a(self):
        print('the data artifact is: %s' % self.my_var)
        if current.retry_count == MAX_RETRIES:
            print(f'Failed all tries and will update github action')
        raise Exception()
        self.next(self.end)
on the other end, it will mark
a
task with green making it hard to know from where the fail came on large flows.
c
You could inherit the retry deco yourself, but I'd suggest making an independent StepDecorator subclass if you go that route. If you aren't sure yet, I agree with Romain that it is easier not to mess with a StepDecorator in the beginning; a regular Python decorator could be sufficient.
👍 1
d
(sorry had started typing this and forgot to send) the catch decorator does not allow you to run code in the “catch” side of things but yes, it does run an additional time (ie: it will run one more code). You can see this here: https://github.com/Netflix/metaflow/blob/master/metaflow/plugins/catch_decorator.py#L109
I was thinking of something a bit simple as so:
Copy code
def send_on_last_error(f):
  @wraps(f)
  def wrapper(*args, **kwargs):
    try:
       f(*args, **kwargs)
    except:
        if current.retry_count == MAX_RETRIES:
            print("Do whatever in github")
but it will probably not work just as is (some internal issues with identifying a step). Let me try something out a little later if I have some time.
👍 1