j

    Jelle Vegter

    1 year ago
    I'm looking to trigger a script on the 4th day before the end of the month. Can a setup like this work where I check if the result of a task is True to run the rest or do I need a different approach? Thanks!
    Bouke Krom

    Bouke Krom

    1 year ago
    I don't think that will work since the return type of
    execution_day
    is a
    Task
    in the
    Flow
    context. The blunt way would be to just fail the task (raise an exception or something) if the day is not right.
    Amanda Wee

    Amanda Wee

    1 year ago
    That won't work since you're trying to evaluate the task when the flow is created, rather than when it is run. Take a look at say, using case:https://docs.prefect.io/core/idioms/conditional.html
    Jeremiah

    Jeremiah

    1 year ago
    Hi @Jelle Vegter! Have you looked at adding “filters” and “adjustments” to your schedule? This is a bit advanced, and you can find the docs here: https://docs.prefect.io/core/concepts/schedules.html. What I have in mind is that you would make a daily
    Clock
    (so a candidate event is created for every day), then use
    filters.is_month_end
    to only keep the candidate events corresponding to the end of the month, then use
    adjustments.add
    to add negative four days to the candidate event. The final event would be 4 days prior to a month end. Recognizing that this is a little convoluted, I do have good news: we are actively working on schedule enhancements that anticipate your use case in a more first class way.
    Wilson Bilkovich

    Wilson Bilkovich

    1 year ago
    Can you simply express the “4th-from-month-end” in the
    cron
    syntax?
    0 8 L-4 * *
    should be ‘8th hour of every 4th-from-month-end’
    Jeremiah

    Jeremiah

    1 year ago
    @Wilson Bilkovich thanks! I actually wasn’t familiar with cron relative syntax. @Jelle Vegter ^ this is a better approach than my recommendation
    Wilson Bilkovich

    Wilson Bilkovich

    1 year ago
    I love cron, but I still have to ponder for a minute to remember what the positions do.. I should use it more often until that goes away.
    j

    Jelle Vegter

    1 year ago
    @Wilson Bilkovich are you sure this works? I remember trying the L-4 statement without it working. Could be that I made a mistake trying it out though!
    Wilson Bilkovich

    Wilson Bilkovich

    1 year ago
    It may not work with the CronClock library; should work in regular cron implementations
    Hopefully there will be an error if it doesn’t support the L syntax
    Kevin Kho

    Kevin Kho

    1 year ago
    Looks like we don’t support the L-4 state on the UI. Will try on the API
    Jeremiah

    Jeremiah

    1 year ago
    CronClock
    uses
    croniter
    under the hood and it looks like the syntax is either
    #<number>
    or
    L<number>
    according to its docs https://github.com/kiorky/croniter#usage
    Kevin Kho

    Kevin Kho

    1 year ago
    Seems both #4 and L4 are not supported in the UI and API. It would make this significantly easier. Jeremiah’s approach with adjustments might be the best for now. I’ll open a ticket for this Cron syntax.
    j

    Jelle Vegter

    1 year ago
    Awesome, thanks. for now I just added a case statement and a task to check if the date is 4 before before the end. Thanks all!
    Bouke Krom

    Bouke Krom

    1 year ago
    ponder for a minute
    Is that
    * * * * *
    ?
    Wilson Bilkovich

    Wilson Bilkovich

    1 year ago
    I wonder if:
    0 8 24-27 * * [ `/bin/date -d +4day +\%d` -eq 1 ]
    would work? That’s another slightly-more-standard-ish way to express it
    I’ll bet the python lib that parses cron doesn’t support shelling out like that though
    Actually that’s not a very portable
    date
    expression, we could probably do better. For example on a BSD
    date
    it would need to be something like
    /bin/date -v +4d +%d
    Kevin Kho

    Kevin Kho

    1 year ago
    I looked into this and we just pass the Cron expression to
    croniter
    .
    croniter
    has an example
    iter = croniter('0 0 * * 5#3,L5', base)
    like what Jeremiah suggested, but I tried this out using
    print(croniter.is_valid("0 0 * * 5#3,L5"))
    and it printed False…. So I think this might be more on an issue with
    croniter
    . I’ll open an issue on their repo.