cron icon indicating copy to clipboard operation
cron copied to clipboard

Able to supply a name to a job

Open jrouzierinverse opened this issue 5 years ago • 10 comments

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?

jrouzierinverse avatar May 04 '20 14:05 jrouzierinverse

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?

robfig avatar May 04 '20 16:05 robfig

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.

jrouzierinverse avatar May 04 '20 17:05 jrouzierinverse

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

mghifariyusuf avatar May 20 '20 07:05 mghifariyusuf

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

UZER0 avatar Dec 03 '20 03:12 UZER0

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

robfig avatar Oct 01 '21 21:10 robfig

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 avatar Jan 10 '22 12:01 Reasno

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.

Reasno avatar Jan 14 '22 05:01 Reasno

+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.

ivanjaros avatar Jan 10 '23 10:01 ivanjaros