go-co-op / gocron

Easy and fluent Go cron scheduling. This is a fork from https://github.com/jasonlvhit/gocron
MIT License
5.61k stars 308 forks source link
clockwork cron gocron golang golang-job-scheduling hacktoberfest schedule scheduler

gocron: A Golang Job Scheduling Package

CI State Go Report Card Go Doc

gocron is a job scheduling package which lets you run Go functions at pre-determined intervals.

If you want to chat, you can find us on Slack at

Quick Start

go get github.com/go-co-op/gocron/v2
package main

import (
    "fmt"
    "time"

    "github.com/go-co-op/gocron/v2"
)

func main() {
    // create a scheduler
    s, err := gocron.NewScheduler()
    if err != nil {
        // handle error
    }

    // add a job to the scheduler
    j, err := s.NewJob(
        gocron.DurationJob(
            10*time.Second,
        ),
        gocron.NewTask(
            func(a string, b int) {
                // do things
            },
            "hello",
            1,
        ),
    )
    if err != nil {
        // handle error
    }
    // each job has a unique id
    fmt.Println(j.ID())

    // start the scheduler
    s.Start()

    // block until you are ready to shut down
    select {
    case <-time.After(time.Minute):
    }

    // when you're done, shut it down
    err = s.Shutdown()
    if err != nil {
        // handle error
    }
}

Examples

Concepts

Features

Job types

Jobs can be run at various intervals.

Concurrency Limits

Jobs can be limited individually or across the entire scheduler.

Distributed instances of gocron

Multiple instances of gocron can be run.

Events

Job events can trigger actions.

Options

Many job and scheduler options are available.

Logging

Logs can be enabled.

Metrics

Metrics may be collected from the execution of each job.

Testing

The gocron library is set up to enable testing.

Supporters

We appreciate the support for free and open source software!

This project is supported by:

Star History

Star History Chart