Skip to content

opt: output detailed stack trace information when a panic occurs. #850

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

Open
wants to merge 1 commit into
base: v2
Choose a base branch
from

Conversation

coticom
Copy link

@coticom coticom commented May 28, 2025

No description provided.

@JohnRoesler
Copy link
Contributor

hi @coticom rather than putting this directly in, as not all users may desire this verbose output, does setting up the panic handler to log the stack meet your needs?

package main

import (
	"log"
	"runtime/debug"
	"time"

	"github.com/google/uuid"

	"github.com/go-co-op/gocron/v2"
)

func main() {
	s, err := gocron.NewScheduler(
		gocron.WithGlobalJobOptions(
			gocron.WithEventListeners(
				gocron.AfterJobRunsWithPanic(
					func(jobID uuid.UUID, jobName string, recoverData any) {
						log.Printf("Job %s with ID %s panicked with data: %v\n", jobName, jobID, recoverData)
						log.Printf("stack: %s\n", string(debug.Stack()))
					},
				),
			),
		),
	)
	if err != nil {
		log.Fatal(err)
	}

	_, err = s.NewJob(
		gocron.DurationJob(time.Second),
		gocron.NewTask(func() {
			log.Println("job ran")
			panic("this is a test panic to see if the job will recover and continue running after a panic")
		}),
	)
	if err != nil {
		log.Fatal(err)
	}

	s.Start()

	select {}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants