Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions jobrunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,18 @@ type Job struct {

const UNNAMED = "(unnamed)"

func New(job cron.Job) *Job {
name := reflect.TypeOf(job).Name()
if name == "Func" {
name = UNNAMED
// Support user-defined job name.
func New(job cron.Job, n ...string) *Job {
name := UNNAMED
if len(n) > 0 {
name = n[0]
} else {
name = reflect.TypeOf(job).Name()
if name == "Func" {
name = UNNAMED
}
}

return &Job{
Name: name,
inner: job,
Expand Down
11 changes: 7 additions & 4 deletions runjob.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,24 @@ type Func func()

func (r Func) Run() { r() }

func Schedule(spec string, job cron.Job) error {
// You can add a job using user-defined job name.
// For example:
// jobrunner.Schedule("cron.frequent", jobs.Func(myFunc), "my-job")
func Schedule(spec string, job cron.Job, n ...string) error {
sched, err := cron.ParseStandard(spec)
if err != nil {
return err
}
MainCron.Schedule(sched, New(job))
MainCron.Schedule(sched, New(job, n...))
return nil
}

// Run the given job at a fixed interval.
// The interval provided is the time between the job ending and the job being run again.
// The time that the job takes to run is not included in the interval.
func Every(duration time.Duration, job cron.Job) {
func Every(duration time.Duration, job cron.Job, n ...string) {

MainCron.Schedule(cron.Every(duration), New(job))
MainCron.Schedule(cron.Every(duration), New(job, n...))
}

// Run the given job right now.
Expand Down