I'm looking to trigger a script on the 4th day bef...
# ask-community
j
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!
b
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.
a
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
j
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.
w
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’
j
@Wilson Bilkovich thanks! I actually wasn’t familiar with cron relative syntax. @Jelle Vegter ^ this is a better approach than my recommendation
w
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
@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!
w
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
k
Looks like we don’t support the L-4 state on the UI. Will try on the API
j
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
k
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
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!
b
ponder for a minute
Is that
* * * * *
?
😁 2
😂 1
w
I wonder if:
Copy code
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
k
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.