Skip to content

Idea: Add a global defer: attribute to cleanup stuff before exiting task? #2273

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

Open
andreynering opened this issue May 28, 2025 · 2 comments
Labels
area: exec Changes related to the execution of commands.

Comments

@andreynering
Copy link
Member

Keep in mind that the original request asks for running a cleanup before exiting Task, and not after every task ran.

Not sure if defer: if the right keywork for this. cleanup: could be clearer on the intention, but defer: is more consistent to what we already have.


Discussed in #2272

Originally posted by @ssbarnea May 28, 2025

How can I make task run some commands always before exiting regardless which task was called?

I want to add some special cleanup logic at the end of execution of task command but one that would run regardless which task I called.

For maintenance reasons I cannot really add an extra defer task of each task being added, so I need to rely of a feature that would allow me to define this special type of implicit "task", one that would be called only before exiting.

It would be key that this task is not called before task executable exits, as we don't want to be called accidentally in the middle of task executions.

I am aware that that there is a small risk of this being called if someone is calling task recursively using shell commands, but that is a small price to pay (in theory nesting can be detected via env vars).

@andreynering andreynering added the area: exec Changes related to the execution of commands. label May 28, 2025
@trulede
Copy link
Contributor

trulede commented May 28, 2025

Perhaps extend task.run to support a new value exit. And at the same time, why not init ... for similar reasons. Could be very useful.

I sketched out the idea in PR #2277.

@trulede
Copy link
Contributor

trulede commented May 31, 2025

@andreynering would an alternative be to have a "template" task? This is how such a thing looks within the current schema:

version: '3'

tasks:
  default:
    cmds:
      - task: setup
      - defer: { task: cleanup}
      - task: '{{.CLI_ARGS}}'

  setup:
    run: once
    cmds:
      - echo "setup"
  cleanup:
    run: always
    cmds:
      - echo "cleanup"

  my-cmd:
    cmds:
      - echo "my-cmd"
 
Run with: 
$ task -s -- my-cmd
setup
my-cmd
cleanup

however, it could be templated like this; new keywords with and template; and a run=defer (or run=exit or a keyword exit) to postpone the cleanup until task exits:

version: '3'

tasks:
  template-cmd:
    cmds:
      - task: setup
      - defer: cleanup
      - echo "run templated commands"
      - template: {}
      - exit: cleanup # alternative to defer, run task on exit

  my-cmd:
    with: template-cmd:
    cmds:
      - echo "my-cmd"

  setup:
    run: once
    cmds:
      - echo "setup"
  cleanup:
    run: exit # alternative `exit` above might be better
    cmds:
      - echo "cleanup"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: exec Changes related to the execution of commands.
Projects
None yet
Development

No branches or pull requests

2 participants