[GH-ISSUE #775] [FEATURE REQUEST] Expose method within handler to shutdown server #2406

Open
opened 2026-03-15 20:22:44 +03:00 by kerem · 1 comment
Owner

Originally created by @Xenov-X on GitHub (Nov 1, 2023).
Original GitHub issue: https://github.com/hibiken/asynq/issues/775

Originally assigned to: @hibiken on GitHub.

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.

  • Or someone to tell me I'm approaching this wrong :D
Originally created by @Xenov-X on GitHub (Nov 1, 2023). Original GitHub issue: https://github.com/hibiken/asynq/issues/775 Originally assigned to: @hibiken on GitHub. 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. * Or someone to tell me I'm approaching this wrong :D
Author
Owner

@g41797 commented on GitHub (Dec 30, 2023):

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

<!-- gh-comment-id:1872533282 --> @g41797 commented on GitHub (Dec 30, 2023): shutdown should be supported by asynq itself it's bad idea to kill process if another tasks should be finished of terminated gracefully
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/asynq#2406
No description provided.