[PR #491] [MERGED] feat(*): Support for handling panic error #761

Closed
opened 2026-03-02 06:05:30 +03:00 by kerem · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/hibiken/asynq/pull/491
Author: @linhbkhn95
Created: 6/15/2022
Status: Merged
Merged: 7/20/2023
Merged by: @kamikazechaser

Base: masterHead: master


📝 Commits (2)

  • 8215412 error panic handling
  • f915368 Merge branch 'master' of github.com:linhbkhn95/asynq

📊 Changes

5 files changed (+85 additions, -3 deletions)

View changed files

📝 internal/errors/errors.go (+15 -0)
📝 internal/errors/errors_test.go (+6 -0)
📝 processor.go (+11 -3)
📝 processor_test.go (+43 -0)
📝 server.go (+10 -0)

📄 Description

Why did we need it?

  • Allow users can handle the panic error if they want
    Example:
func main() {

	srv := asynq.NewServer(
		asynq.RedisClientOpt{
			Addr: redisAddr,
		},
		asynq.Config{
			// Specify how many concurrent workers to use
			Concurrency: 10,
			// Optionally specify multiple queues with different priority.
			Queues: map[string]int{
				"critical": 6,
				"default":  3,
				"low":      1,
			},
			ErrorHandler: asynq.ErrorHandlerFunc(HandleErrorFunc),
		},
	)

	// mux maps a type to a handler
	mux := asynq.NewServeMux()
	mux.HandleFunc(tasks.TypeEmailDelivery, tasks.HandleEmailDeliveryTask)
	mux.Handle(tasks.TypeImageResize, tasks.NewImageProcessor())
	// ...register other handlers...

	if err := srv.Run(mux); err != nil {
		log.Fatalf("could not run server: %v", err)
	}
}

// The ErrorHandlerFunc type is an adapter to allow the use of  ordinary functions as a ErrorHandler.
// If f is a function with the appropriate signature, ErrorHandlerFunc(f) is a ErrorHandler that calls f.
// HandleError calls fn(ctx, task, err)
func HandleErrorFunc(ctx context.Context, task *asynq.Task, err error) {
	if asynq.IsPanicError(err) {
		// Handle panic error here.
	}
}

fixed https://github.com/hibiken/asynq/issues/487


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/hibiken/asynq/pull/491 **Author:** [@linhbkhn95](https://github.com/linhbkhn95) **Created:** 6/15/2022 **Status:** ✅ Merged **Merged:** 7/20/2023 **Merged by:** [@kamikazechaser](https://github.com/kamikazechaser) **Base:** `master` ← **Head:** `master` --- ### 📝 Commits (2) - [`8215412`](https://github.com/hibiken/asynq/commit/821541215c8ad7cc7db109e6de8af4eff9d7a396) error panic handling - [`f915368`](https://github.com/hibiken/asynq/commit/f915368ea5381dd45d7c9220318eda8f11863af7) Merge branch 'master' of github.com:linhbkhn95/asynq ### 📊 Changes **5 files changed** (+85 additions, -3 deletions) <details> <summary>View changed files</summary> 📝 `internal/errors/errors.go` (+15 -0) 📝 `internal/errors/errors_test.go` (+6 -0) 📝 `processor.go` (+11 -3) 📝 `processor_test.go` (+43 -0) 📝 `server.go` (+10 -0) </details> ### 📄 Description ### Why did we need it? - Allow users can handle the panic error if they want Example: ``` func main() { srv := asynq.NewServer( asynq.RedisClientOpt{ Addr: redisAddr, }, asynq.Config{ // Specify how many concurrent workers to use Concurrency: 10, // Optionally specify multiple queues with different priority. Queues: map[string]int{ "critical": 6, "default": 3, "low": 1, }, ErrorHandler: asynq.ErrorHandlerFunc(HandleErrorFunc), }, ) // mux maps a type to a handler mux := asynq.NewServeMux() mux.HandleFunc(tasks.TypeEmailDelivery, tasks.HandleEmailDeliveryTask) mux.Handle(tasks.TypeImageResize, tasks.NewImageProcessor()) // ...register other handlers... if err := srv.Run(mux); err != nil { log.Fatalf("could not run server: %v", err) } } // The ErrorHandlerFunc type is an adapter to allow the use of ordinary functions as a ErrorHandler. // If f is a function with the appropriate signature, ErrorHandlerFunc(f) is a ErrorHandler that calls f. // HandleError calls fn(ctx, task, err) func HandleErrorFunc(ctx context.Context, task *asynq.Task, err error) { if asynq.IsPanicError(err) { // Handle panic error here. } } ``` fixed https://github.com/hibiken/asynq/issues/487 --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
kerem 2026-03-02 06:05:30 +03:00
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#761
No description provided.