hibiken / asynq

Simple, reliable, and efficient distributed task queue in Go
MIT License
9.87k stars 709 forks source link

[FEATURE REQUEST] Expose method within handler to shutdown server #775

Open Xenov-X opened 1 year ago

Xenov-X commented 1 year ago

I have a task which I'd like to be the last task performed by the Server, with the server closing following completion. I've tried implementing this sending TSTP/TERM to the current PID running the handler, but the server still processes other tasks.

By way of a simple example, HandleShutdownTask should run, followed by shutdown of the Async server / worker (and then the host shortly after)

func HandleShutdownTask(ctx context.Context, t *asynq.Task) error {
    log.Info("[shutdown] Shutdown Queued")

    err = exec.Command("shutdown").Run()
    if err != nil {
        log.Error("Failed to run shutdown command")
        os.Exit(1)
    }
    syscall.Kill(syscall.Getpid(), syscall.SIGTSTP)
    syscall.Kill(syscall.Getpid(), syscall.SIGTERM)
    return nil

However, the syscall signal implementation above doesn't work, with other tasks starting to be processed following completion of the above.

Describe the solution you'd like A means to instruct the Async Server to terminate from within a handler, following completion of the current task.

g41797 commented 10 months ago

shutdown should be supported by asynq itself it's bad idea to kill process if another tasks should be finished of terminated gracefully