gron icon indicating copy to clipboard operation
gron copied to clipboard

[feat] support cancel job.

Open fakeyanss opened this issue 3 years ago • 0 comments

Defines JobWithCancel interface. Require a unique string as jobID in each job.

Sometimes we need to cancel job after it's lifecycle finished. For example, start a monitoring job while a tcp socket alive, and cancel the monitoring job after socket closed.

So I develop the feature, support cancel job. And I defined a JobWithCancel interface, a JobID() function to index job when cancel it.

Just see this samle:

type canceledJob struct { // implements of JobWithCancel interface
	id string
}
func (j *canceledJob) JobID() {
	return id
}
func (j *canceledJob) Run() {
  fmt.Println("job run")
}
c := gron.New()
c.AddFuncWithJobID(gron.Every(1*time.Second), "job-id-1", func() {
	fmt.Println("runs every second")
})
c.AddCancelingJob(Every(1*time.Second), &canceledJob{id: "job-id-2"})
c.Start()
time.Sleep(5 * time.Second)
c.Cancel("job-id-1")
c.Cancel("job-id-2")

lastly, I using go mod in this project. I think it is compatible in higher version of golang. If you think module is not necessary, I could remove it.

fakeyanss avatar Feb 13 '23 05:02 fakeyanss