reugn/go-quartz

How to get job context info in NewFunctionJob callback ?

zhangjie2012 opened this issue · 3 comments

e.g:

functionJob := job.NewFunctionJob(func(ctx context.Context) (int, error) {
  // get which job id tiggered in here
  return 42, nil
})

in fact, I want add more job params to callback.

@zhangjie2012, it makes sense to me to add the JobKey details of the job to the context, but may I ask the reason you need it? JobDetailOptions also can be made available. What other information would you expect to be accessible within the context?

Finally, I solved it using this way:

callbackJob := job.NewFunctionJob(func(_ context.Context) (n int, err error) {
	ctx := CronJobCtx{
		// cronjob runtime info, like name/id/params
	}
	err = handleCronJob(&ctx)
	return
})

package gocron's implementation is elegant. like:

gocron.NewTask(
	func(a string, b int) {
			// do things
		},
		"hello",
		1,
	),
)

a common requirement is to inject some runtime parameters to my callback function.