New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
azure - schema - period modes cron regex #4695
azure - schema - period modes cron regex #4695
Conversation
schedule={ | ||
'type': 'string', | ||
# Pattern based on apscheduler's CronTrigger https://github.com/agronholm/apscheduler/blob/master/apscheduler/triggers/cron/expressions.py | ||
'pattern': '(\*|[0-9]|\,|\/|\-)+ (\*|[0-9]|\,|\/|\-)+ (\*|[0-9]|[1-2][0-9]|3[0-1]|\,|\*\/|\-)+ (jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec|\,|\*\/|[1-9]|1[0-2]|\*)+ (mon|tue|wed|thu|fri|sat|sun|[0-6]|\,|\*|\-)+' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
100 char line max, will fail lint.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes that is what I am working on addressing now. One of the unit tests for function periodic was misformatted and was failing as well
Changes are coming
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@stefangordon This should be fixed now
You might look at some of the validate tests we have for most resources, they are very simple and just verify schema. e.g.
Not a bad idea to toss some of those in to make sure the regex seems to be working right. You can also test the negative by loading the policy inside a
|
tools/c7n_azure/c7n_azure/policy.py
Outdated
schedule_regex += r'(\*|[1-9]|[1-2][0-9]|3[0-1]|\,|\*\/|\-)+ ' | ||
schedule_regex += r'([Jj](an|anuary)|[Ff](eb|ebruary)|[Mm](ar|arch)|[Aa](pr|pril)|[Mm]ay|' | ||
schedule_regex += r'[Jj](un|une)|[Jj](ul|uly)|[Aa](ug|ugust)|[Ss](ep|ept|eptember)|[Oo]' | ||
schedule_regex += r'(ct|ctober)|[Nn](ov|ovember)|[Dd](ec|ecember)|\,|\*\/|[1-9]|1[0-2]|\*)+ ' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can simplify it by using regex for months, days of the week from container.
Yes, functions allow you to use whatever, but we can keep some consistency here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: You don't have to do the +=, they will be auto-concatenated.
https://docs.python.org/3/reference/lexical_analysis.html#string-literal-concatenation
regex = 'line1'
'line2'
'line3'
is valid
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@stefangordon that method of string concatenation was not working. It does not throw a compile error, but the resulting regex matched with all of the invalid cases. To replicate the style I put everything in a list and then join the list when I need the regex. This passes all of the unit tests for it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can simplify it by using regex for months, days of the week from container.
Yes, functions allow you to use whatever, but we can keep some consistency here.
@logachev The current version is working for the functionality of the function timer cron expression. Are you thinking that we should simplify it for maintainability?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes.. c7n is not primary functions provisioning tool, so there is no need to support everything
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While I agree with the sentiment, I think in this case we are already committed to exposing the azure trigger functionality here, I think we even link to it in the docs somewhere. It would be an unnecessary breaking change to block certain syntax.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We do link to the Azure Functions cron expression documentation in the Function Host Options doc. If we change the implementation here, then we need to change the documentation as well
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good point, lets keep it as-is in that case
@@ -49,8 +49,15 @@ def provision(self): | |||
@execution.register(CONTAINER_TIME_TRIGGER_MODE) | |||
class AzureContainerPeriodicMode(AzureContainerHostMode, PullMode): | |||
"""A policy that runs at specified time intervals.""" | |||
# Pattern based on apscheduler's CronTrigger: https://github.com/agronholm/apscheduler |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a better link might be to apscheduler trigger parser code
tools/c7n_azure/c7n_azure/policy.py
Outdated
r'[Jj](un|une)|[Jj](ul|uly)|[Aa](ug|ugust)|[Ss](ep|eptember)|[Oo](ct', | ||
r'|ctober)|[Nn](ov|ovember)|[Dd](ec|ecember)|\,|\*\/|[1-9]|(1[0-2])|\*)+ ', | ||
r'([Mm](on|onday)|[Tt](u|ue|ues|uesday)|[Ww](ed|ednesday)|[Tt](hu|hursday)|', | ||
r'[Ff](ri|riday)|[Ss](at|aturday)|[Ss](un|unday)|[0-6]|\,|\*|\-)+\s?$'] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
impressive regex, reminds me of the jwz quote https://web.archive.org/web/20140424160443/http://regex.info:80/blog/2006-09-15/247
Azure triggers don't allow * * * * * * ? |
I don't see anywhere where they specifically say it is not allowed, but none of their examples have * in the first slot (seconds). I figured blocking a function (that takes 2-3 seconds to run) from running every second seemed ok. Also if you want the every second functionality "0-59 * * * * *" does the same thing |
Agree it would almost certainly crash Functions so fine with blocking it. |
I think this is great! My only thought is that perhaps if we actually do the check in the validator method instead of the schema we might be able to give the user a better error message. If it didn't fit in the schema I can say personally with 100% confidence I would not be able to tell from that regex that I had an extra * or something haha. |
Yeah I had the same thought this error is going to be nasty, but I guess its 10000x better than getting no validation error and having functions just broken. I was actually seeing there might be mechanisms in jsonschema to customize the validation error now, so this might be the best of both worlds. (might require adding code to the validation error handler to print out the custom message) Lets not block the PR on it as its a net-improvement, but we can research that. |
Adds regex expressions to the schemas for azure-periodic and container-periodic that check the validity of the cron expression. Note: The two regexs are different because the interpreter used by the two modes for the cron expressions are different. Included comment linking to the interpreters
Closes #4552