Code Monkey home page Code Monkey logo

gron's Introduction

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

gron's People

Contributors

xgfone avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.