MCPcopy
hub / github.com/jasonlvhit/gocron

github.com/jasonlvhit/gocron @0.1 sqlite

repository ↗ · DeepWiki ↗ · release 0.1 ↗
117 symbols 579 edges 3 files 75 documented · 64%
README

This package is currently looking for new maintainers (cause @jasonlvhit is in ICU). Please message @jasonlvhit if you are interested.

goCron: A Golang Job Scheduling Package.

GgoDoc Go Report Card

goCron is a Golang job scheduling package which lets you run Go functions periodically at pre-determined interval using a simple, human-friendly syntax.

goCron is a Golang implementation of Ruby module clockwork and Python job scheduling package schedule, and personally, this package is my first Golang program, just for fun and practice.

See also this two great articles:

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

Back to this package, you could just use this simple API as below, to run a cron scheduler.

package main

import (
    "fmt"
    "time"

    "github.com/jasonlvhit/gocron"
)

func task() {
    fmt.Println("I am running task.")
}

func taskWithParams(a int, b string) {
    fmt.Println(a, b)
}

func main() {
    // Do jobs with params
    gocron.Every(1).Second().Do(taskWithParams, 1, "hello")

    // Do jobs safely, preventing an unexpected panic from bubbling up
    gocron.Every(1).Second().DoSafely(taskWithParams, 1, "hello")

    // Do jobs without params
    gocron.Every(1).Second().Do(task)
    gocron.Every(2).Seconds().Do(task)
    gocron.Every(1).Minute().Do(task)
    gocron.Every(2).Minutes().Do(task)
    gocron.Every(1).Hour().Do(task)
    gocron.Every(2).Hours().Do(task)
    gocron.Every(1).Day().Do(task)
    gocron.Every(2).Days().Do(task)
    gocron.Every(1).Week().Do(task)
    gocron.Every(2).Weeks().Do(task)

    // Do jobs on specific weekday
    gocron.Every(1).Monday().Do(task)
    gocron.Every(1).Thursday().Do(task)

    // Do a job at a specific time - 'hour:min'
    gocron.Every(1).Day().At("10:30").Do(task)
    gocron.Every(1).Monday().At("18:30").Do(task)

    // Begin job immediately upon start
    gocron.Every(1).Hour().From(gocron.NextTick()).Do(task)

    // Begin job at a specific date/time
    t := time.Date(2019, time.November, 10, 15, 0, 0, 0, time.Local)
    gocron.Every(1).Hour().From(&t).Do(task)

    // NextRun gets the next running time
    _, time := gocron.NextRun()
    fmt.Println(time)

    // Remove a specific job
    gocron.Remove(task)

    // Clear all scheduled jobs
    gocron.Clear()

    // Start all the pending jobs
    <- gocron.Start()

    // also, you can create a new scheduler
    // to run two schedulers concurrently
    s := gocron.NewScheduler()
    s.Every(3).Seconds().Do(task)
    <- s.Start()
}

and full test cases and document will be coming soon (help is wanted! If you want to contribute, pull requests are welcome).

If you need to prevent a job from running at the same time from multiple cron instances (like running a cron app from multiple servers), you can provide a Locker implementation and lock the required jobs.

gocron.SetLocker(lockerImplementation)
gocron.Every(1).Hour().Lock().Do(task)

Once again, thanks to the great works of Ruby clockwork and Python schedule package. BSD license is used, see the file License for detail.

Have fun!

Extension points exported contracts — how you extend this code

Locker (Interface)
Locker provides a method to lock jobs from running at the same time on multiple instances of gocron. You can provide any [2 …
gocron.go

Core symbols most depended-on inside this repo

Every
called by 47
gocron.go
Day
called by 28
gocron.go
Do
called by 24
gocron.go
NewScheduler
called by 24
gocron.go
Minute
called by 22
gocron.go
NextScheduledTime
called by 18
gocron.go
Hour
called by 17
gocron.go
At
called by 16
gocron.go

Shape

Method 60
Function 51
Struct 5
Interface 1

Languages

Go100%

Modules by API surface

gocron.go78 symbols
gocron_test.go34 symbols
example/lock.go5 symbols

Dependencies from manifests, versioned

github.com/go-redis/redisv6.15.5+incompatible · 1×
github.com/onsi/ginkgov1.10.1 · 1×

For agents

$ claude mcp add gocron \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact