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

Can we leverage go's Context package to cancel jobs with ANTS #483

Closed
joncodo opened this issue Oct 7, 2021 · 3 comments
Closed

Can we leverage go's Context package to cancel jobs with ANTS #483

joncodo opened this issue Oct 7, 2021 · 3 comments
Assignees
Labels
type/feature-request This issue is a proposal for something new

Comments

@joncodo
Copy link
Contributor

joncodo commented Oct 7, 2021

Summary

The main reason to cancel jobs in the first place is so Users can start a task that can take a long time to complete and change their mind and cancel it. EE also needs this I think.

What we are doing now

Right now, we are cancelling jobs that are running ants like this:

(Using Nats)
docker-compose.yml

nats:
    image: 'nats'
    ports:
      - "4222:4222"
    hostname: nats-server

api/task/task.go

func CancelTask(ctx *gin.Context) {
	// pull the task name from the query params
	taskName := ctx.Query("taskName")

	nc, err := nats.Connect(nats.DefaultURL)
	if err != nil {
		logger.Error("err connecting to nats server", err)
	}

	// Simple Publisher
	**err2 := nc.Publish(taskName, []byte("Hello World"))**
	if err2 != nil {
		logger.Error("err2", err2)
	}
}

And then in each plugin, we do something like this:

_, errNc := nc.Subscribe("github", func(m *nats.Msg) {
  fmt.Printf("Received a message: %s\n", string(m.Data))
  scheduler.Release()
  progress <- 1
  logger.Info("You cancelled the collector with nats", false)
  close(progress)
})

This causes us to us run a NATS service at all time and also requires the cancel listener in each plugin.

What we could do in the future

We might be able to use the Context package to cancel tasks. The main benefits of this is that we will no longer need NATS in the docker-compose file and the plugins will not have to implement the cancel method.

Unknowns:

  1. Can you cancel ants with context? I know you can cancel go routines with context but ants seems to not give that option.

Research:

  1. Post on SO for how to cancel processes with Context: https://stackoverflow.com/questions/59607744/how-to-kill-running-goroutines-from-outside/59608070#59608070
  2. ANTS Creator saying to use Context: Cancel all task when has any task return error panjf2000/ants#169
  3. ANTS Creator talking about Context: how to pass context and other args in pool_func? panjf2000/ants#81
@joncodo joncodo added the type/feature-request This issue is a proposal for something new label Oct 7, 2021
@joncodo
Copy link
Contributor Author

joncodo commented Oct 7, 2021

For now, since I was unable to get Context working quickly, I will go with the original solution with NATS until we have time to revisit this or @hezyin sees it as a high priority.

Implementation in : https://github.com/merico-dev/lake/pull/478

@joncodo
Copy link
Contributor Author

joncodo commented Oct 8, 2021

@klesh If this is possible, it would make our code much cleaner but I am not able to get it to cancel ANTS with the context Cancel method. Can you take about an hour to try this out and show me your findings?

@klesh
Copy link
Contributor

klesh commented Oct 15, 2021

closed by #492

@klesh klesh closed this as completed Oct 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/feature-request This issue is a proposal for something new
Projects
None yet
Development

No branches or pull requests

2 participants