Skip to content

xgfone/gron

Repository files navigation

gron Build Status GoDoc License

Another job periodic runner like crontab supporting Go1.7+

Example

package main

import (
	"context"
	"fmt"
	"time"

	"github.com/xgfone/gron"
)

func jobRunner(name string) gron.Runner {
	return func(c context.Context, now time.Time) (result interface{}, err error) {
		fmt.Printf("Starting to run job '%s' at '%s'\n", name, now.Format(time.RFC3339Nano))
		return
	}
}

func jobResultHook(result gron.JobResult) {
	fmt.Printf("End to run job '%s', cost '%s'.\n", result.Job.Name(), result.Cost)
}

func main() {
	exe := gron.NewExecutor()
	exe.AppendResultHooks(jobResultHook) // Add the job result hook
	exe.Start()                          // Start the executor in the background goroutine.

	// Add jobs
	exe.Schedule("job1", gron.Every(time.Minute), jobRunner("job1"))
	exe.Schedule("job2", gron.MustParseWhen("@every 2m"), jobRunner("job2"))
	everyMinuteScheduler := gron.MustParseWhen("*/1 * * * *")
	exe.ScheduleJob(gron.NewJob("job3", everyMinuteScheduler, jobRunner("job3")))

	go func() {
		time.Sleep(time.Minute * 4)
		// exe.CancelJobs("job1", "job2", "job3")
		exe.Stop()
	}()

	// Wait until the executor is stopped.
	exe.Wait()
}