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

[FEATURE] - Allow passing in a Context and CancelFunc #814

Open
choonge opened this issue Jan 14, 2025 · 1 comment
Open

[FEATURE] - Allow passing in a Context and CancelFunc #814

choonge opened this issue Jan 14, 2025 · 1 comment
Labels
enhancement New feature or request

Comments

@choonge
Copy link

choonge commented Jan 14, 2025

Is your feature request related to a problem? Please describe

I'm very much a beginner at Go so please forgive me if this is a dumb question.

Somewhere in my application I'm creating a Context root. Would it make sense to have some methods on the Scheduler to allow passing in a Context and CancelFunc , so I can pass my root context to the scheduler?

If this makes sense, I can create a PR.

Describe the solution you'd like

Add the methods WithShutdownContext(context context.Context) and WithCancel(cancel context.CancelFunc) on the Scheduler, much like the existing WithLocation(..)

Describe alternatives you've considered

n/a

Additional context

n/a

@choonge choonge added the enhancement New feature or request label Jan 14, 2025
@JohnRoesler
Copy link
Contributor

so I can pass my root context to the scheduler?

I assume you are looking to have the scheduler shutdown if your context cancels?

It wouldn't be as simple as passing a context/cancel with the current design. If you're looking to handle a graceful shutdown, you can do without passing a context/cancel into the scheduler.

Something like:

package main

import (
	"context"
	"fmt"
	"log"
	"os"
	"os/signal"
	"syscall"

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

func main() {
	appCtx, appCancel := context.WithCancel(context.Background())
	defer appCancel()

	sig := make(chan os.Signal, 1)
	signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)

	scheduler, err := gocron.NewScheduler()
	if err != nil {
		log.Fatalf("failed to setup scheduler: %s", err.Error())
	}

	_, err = scheduler.NewJob(
		gocron.CronJob("* * * * * *", true),
		gocron.NewTask(func() {
			panic("boom!")
		}),
		gocron.WithEventListeners(
			gocron.AfterJobRunsWithPanic(
				func(jobID uuid.UUID, jobName string, recoverData any) {
					fmt.Println("job run panic", jobName, recoverData, jobID)
				},
			),
		),
	)
	if err != nil {
		log.Fatalf("failed to setup job: %s", err.Error())
	}

	scheduler.Start()
	select {
	case <-appCtx.Done():
	case <-sig:
	}

	// you can configure the timeout for how long the scheduler waits during graceful shutdown
	// https://pkg.go.dev/github.com/go-co-op/gocron/v2#WithStopTimeout
	// WithStopTimeout sets the amount of time the Scheduler should wait gracefully for jobs to complete
	// before returning when StopJobs() or Shutdown() are called. Default: 10 * time.Second
	err = scheduler.Shutdown()
	if err != nil {
		log.Fatalf("there was an error shutting down the scheduler: %s", err.Error())
	}
}

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

No branches or pull requests

2 participants