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

context in cron.Job.Run Is it possible? #318

Open
Breezess opened this issue Jun 5, 2020 · 5 comments
Open

context in cron.Job.Run Is it possible? #318

Breezess opened this issue Jun 5, 2020 · 5 comments

Comments

@Breezess
Copy link

Breezess commented Jun 5, 2020

I want to pass in the context in cron.Job.Run, is there any way?

return cron.WithChain(func(job cron.Job) cron.Job {
	return cron.FuncJob(func() {
                // There is a way to write context 
                  job.Run()
                 // use context 
	})
})
@ex-tag
Copy link

ex-tag commented Mar 4, 2022

@robfig

The AddFunc method should pass a context to the added function, as there might be websocket connections, or long-polling HTTP requests, that are open - and need to be cancelled when the task closes. Without this cancellation, these connections would remain open, and they would not be closed until the server was able to respond back, and therefore blocking additional connections that could be made. Please read https://go.dev/blog/context and https://pkg.go.dev/context for more information.

Example implementation

cron.New().AddFunc("", function (context c.Context) {

  request.Context(context)

})

Example source code change

func (c *Cron) AddFunc(spec string, cmd func(context c.Context)) error {
	return c.AddJob(spec, FuncJob(cmd))
}

@Slava02
Copy link

Slava02 commented Oct 19, 2024

@robfig

Can you merge this pr?

#516

@tooptoop4
Copy link

robfig dipped out, dis abandonware ☠️

@flc1125
Copy link

flc1125 commented Oct 27, 2024

Based on this project, I forked a project and kept the record of original historical submissions. At the same time, the 4.x version is under development, and if you are interested, you can join it together.
https://github.com/flc1125/go-cron

I added context.Context to this version, as well as error returns.

ex:

package main

import (
	"context"

	"github.com/flc1125/go-cron/v4"
	"github.com/flc1125/go-cron/v4/middleware/recovery"
)

func main() {
	c := cron.New()
	c.Use(recovery.New())

	c.AddFunc("* * * * * ?", func(ctx context.Context) error {
		panic("YOLO")
	})

	c.Start()
	defer c.Stop()
}

@flc1125
Copy link

flc1125 commented Oct 29, 2024

V4.0.0 has been released, welcome to use it.👏🏻

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

No branches or pull requests

5 participants