robfig / cron

a cron library for go
MIT License
13.1k stars 1.62k forks source link

Able to supply a name to a job #307

Open jrouzierinverse opened 4 years ago

jrouzierinverse commented 4 years ago

To improve debugging and logging allow Jobs to have a name. I would like to propose the following changes.

func (c *Cron)  AddFuncNamed(name string, spec string, cmd func()) EntryID {}
func (c *Cron)  AddJobNamed(name string, spec string, cmd Job) EntryID {}
func (c *Cron)  ScheduleNamed(name string, schedule Schedule, cmd Job) EntryID {}

If the name is empty. It will be set to the EntryID.

Would you mind if I send you a pull request for this?

robfig commented 4 years ago

Thanks for the thoughts, but I think you can do this today, because Job is an interface:

type MyJob struct {
  Name string
  Func func()
}

func (j MyJob) Run() {
  j.Func()
}

cron.AddJob(MyJob{"My Name", fn}, ...)

Does that work for your use case?

jrouzierinverse commented 4 years ago

My issue is with logging and knowing which job is running or going to run. https://github.com/robfig/cron/blob/master/cron.go#L276 for example. Reviewing the log entries can be confusing because it tells me the job Id was wake/added/scheduled/run/removed but that does not give me any clue which job it is. For long-running services, it will not be practical for me to look the name to id mapping in the log. Just to figure out which service is running badly.

mghifariyusuf commented 4 years ago

Job is an interface, you can made your own and use method Schedule()

UZER0 commented 3 years ago

I think this is a good idea, because I need to find the Job by name

robfig commented 3 years ago

I suppose this could be implemented as an optional interface that is tested for and used by the logger..

Reasno commented 2 years ago

e this could be implemented as an optional interface that is tested for and used by the logger..

would you accept a PR for this? I'd like to add this. The log output becomes much more readable with a name. The chained middleware may also benefit from a name, for example when collecting metrics.

Reasno commented 2 years ago

I suppose this could be implemented as an optional interface that is tested for and used by the logger..

The optional interface approach doesn't play nice with job wrappers though.

ivanjaros commented 1 year ago

+1 for job names. The cron logs are garbage. Not only they pollute the stdout but they are utterly useless since they lack job names. I had to write my own logging to dev/null this garbage generation. VERY poor design.

fallais commented 8 months ago

+1 for job.Name()

This library is the historical one, and I am really sorry to say that, but you can use this library instead : https://pkg.go.dev/github.com/go-co-op/gocron/v2#Job