Skip to content
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

Feature Request: Dependency Cleanup #1082

Open
paulo-raca opened this issue Mar 23, 2023 · 1 comment
Open

Feature Request: Dependency Cleanup #1082

paulo-raca opened this issue Mar 23, 2023 · 1 comment
Labels
state: needs triage Waiting to be triaged by a maintainer.

Comments

@paulo-raca
Copy link

Hello, and thanks for this great tool!

I was writing a task today and wished for a better way to express it:

  • I want to execute a command to generate documentation
  • This commands depends on a background service to be running. (e.g., docker-compose up ...)
  • I want the service to stop once it is no longer necessary (e.g., docker-compose down ...)

(For context, the service in question takes a lot of RAM, so I do want to stop it when done)

What I have right now is something like this:

version: '3'

tasks:

  start-docker-service:
    cmds:
      - docker compose up

  stop-docker-service:
    cmds:
      - docker compose down

  default:
    cmds:
      - task: start-docker-service
      - defer: {task: stop-docker-service}
      - echo "Doing stuff"

This works well enough, but the "docker-service" would be better expressed as a dependency. It should also be possible to have multiple concurrent tasks that depend on this.

I think it would be great if we could do something like this:

version: '3'

tasks:

  with-docker-service:
    cmds:
      - docker compose up ...
      - defer-dep: docker compose down ...  # similar to `defer`, but only executes after all tasks that depend on it are complete

  default:
    deps:
      - with-docker-service
    cmds:
      - echo "Doing stuff"

defer is pretty close to what I want, but it only waits for the next commands, not the dependents tasks. I guess we need a new keyword (Or modifying the semantics of defer, which sounds more problematic)

@task-bot task-bot added the state: needs triage Waiting to be triaged by a maintainer. label Mar 23, 2023
@trulede
Copy link

trulede commented Mar 31, 2024

Currently defer is using the go defer keyword and as such is limited to the executing scope of a/the task. I did an experiment where I added "defer functions" to the Run context, and then called those "defer functions" after the task had completed. It worked ... not sure how flaky it as a mechanism though.

version: '3'
tasks:
    docker:
    cmds:
      - echo "START"
      - defer: echo "STOP"  # "final" might be a better keyword choice
  bar:
    deps:
      - docker
    cmds:
      - echo "bar"

and the console:

$ ~/go/bin/task bar -v
task: "bar" started
task: "docker" started
task: [docker] echo "START"
START
task: "docker" finished
task: [bar] echo "bar"
bar
task: "bar" finished
task: [docker] echo "STOP"
STOP

The code is like this, in case any one wants to take the idea further.

func (e *Executor) Run(ctx context.Context, calls ...*ast.Call) error {
        ...
	var deferedTasks []func()
	ctx = context.WithValue(ctx, "deferedTasks", &deferedTasks)
       ...
	for _, f := range deferedTasks {
		f()
	}
        ...
}

...

                   for i := range t.Cmds {
			if t.Cmds[i].Defer {
				if x, ok := ctx.Value("deferedTasks").(*[]func()); ok {
					deferIdx := i // force immediate evaluation of parameter "i"
					*x = append(*x, func() {
						e.runDeferred(t, call, deferIdx)
					})
				} else {
					e.Logger.VerboseErrf(logger.Yellow, "task:   defer not possible: %d\n", i)
				}

				continue
			}

I think it might be better to put the defer/final functions on the executor struct ... still, you get the idea.

@trulede trulede mentioned this issue Apr 1, 2024
8 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
state: needs triage Waiting to be triaged by a maintainer.
Projects
None yet
Development

No branches or pull requests

3 participants