Breaking a task early
Naomi Sirkin
I've been using {% break %} to end my task runs early (like a sys.exit in Python), but I just realized that it's not written anywhere in the documentation that this is an official way to stop executing task code. It's pretty useful to have this. If this is the intended behavior, could it be added to the documentation? And if not, then my feature request is for a command that halts a task early. Thanks!
Tim Mackey
Interesting, I didn't realize that
{% break %}
works this way! Inside of a for-loop it just skips to the end of the {% for %}
block, but apparently outside of a for-loop it stops execution of the rest of the liquid code. I don't think this is documented anywhere.See this conversation on the Mechanic Slack: https://usemechanic.slack.com/archives/CURE65U2W/p1660199619952199?thread_ts=1659978313.426479&cid=CURE65U2W
“interestingly enough, {% break %} will doing what you’re looking for, as long as it’s outside of a forloop. :slightly_smiling_face: you could do something like this, to get closer to what you’re describing:”
{% assign halt = false %}
{% for n in m %}
{% assign halt = true %}
{% break %}
{% endfor %}
{% if halt %}
{% break %}
{% endif %}
{% log "this line will never be reached" %}