Skip to content

Defer tasks until a later named point. #2346

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
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

trulede
Copy link
Contributor

@trulede trulede commented Aug 1, 2025

This PR makes it possible to defer a command to a "named point" such as exit (i.e. when task itself is exiting) using a new when keywork. For example:

version: '3'
vars:
  GREETING: Hello, World!
tasks:
  default:
    cmds:
      - defer:
          task: when-message
          when: exit
      - defer:
          task: defer-message
      - task: message
  message:
    cmds:
      - echo "MESSAGE - {{.GREETING}}"
  defer-message:
    cmds:
      - echo "DEFER - {{.GREETING}}"
  when-message:
    run: once
    cmds:
      - echo "WHEN - {{.GREETING}}"

which produces this output (note the order of the messages):

$ go run ../cmd/task/task.go -s
MESSAGE - Hello, World!
DEFER - Hello, World!
WHEN - Hello, World!

The "when" mechanism operates in a method similar to a "pub/sub". Defered tasks a pushed to a named when list, and then pulled at some later point. Only the exit tasks are pulled and executed at the moment, however it would be possible to trigger the execution of tasks contained in a named list - such a mechanism only needs to be defined (e.g. a tempate function).

#2273
#2040
#1082
#141 - partial, would need addition, for instance defer/if (see #2345 (comment))

@trulede
Copy link
Contributor Author

trulede commented Aug 1, 2025

If this PR is interesting, I can develop further (tests, doc ... extensions for #141 ... etc).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant