Skip to content

xgfone/gron

Folders and files

NameName
Last commit message
Last commit date

Latest commit

6289b1b · Oct 17, 2021

History

34 Commits
Oct 16, 2021
Oct 16, 2021
Jul 1, 2019
Jun 28, 2019
Oct 16, 2021
Oct 17, 2021
Oct 17, 2021
Oct 16, 2021
Oct 16, 2021
Oct 16, 2021
May 14, 2020
May 14, 2020
Oct 16, 2021
Oct 16, 2021

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()
}