计划任务
Go to file
iyashjayesh cb4c427338 add blog post section 2025-10-23 00:08:44 +05:30
.github Bump github/codeql-action from 3 to 4 (#883) 2025-10-14 14:31:37 +00:00
assets docs: adapt README to the dark theme (#844) 2025-04-03 12:27:12 -05:00
examples/elector Use `errors.New` for non-formatted strings (#870) 2025-09-02 08:48:40 -05:00
mocks Add comprehensive GitHub Copilot instructions for gocron development (#866) 2025-08-27 10:56:47 -05:00
.gitignore initial clean v2 commit history 2023-11-08 11:11:42 -06:00
.golangci.yaml go 1.23 & golangci-lint v2 (#843) 2025-04-29 10:43:10 -05:00
.pre-commit-config.yaml Add go1.25 tests (#869) 2025-09-02 09:32:29 -05:00
CODE_OF_CONDUCT.md initial clean v2 commit history 2023-11-08 11:11:42 -06:00
CONTRIBUTING.md test all remaining With*, and test logger (#609) 2023-11-09 15:04:18 -06:00
LICENSE initial clean v2 commit history 2023-11-08 11:11:42 -06:00
Makefile re-enable goleak detection in ci (#832) 2025-02-17 15:18:43 -06:00
README.md add blog post section 2025-10-23 00:08:44 +05:30
SECURITY.md Fix typo (#759) 2024-07-17 19:03:10 -05:00
distributed.go chore: document the limitations with the locker design (#848) 2025-05-16 14:15:33 -05:00
errors.go feat: add WithStartDateTimePast WithStartAt option (#882) 2025-10-09 21:45:13 -05:00
example_test.go feat: Add option to calculate intervals from job completion time for interval-based scheduling (fixes #565) (#884) 2025-10-21 22:09:46 -05:00
executor.go feat: Add option to calculate intervals from job completion time for interval-based scheduling (fixes #565) (#884) 2025-10-21 22:09:46 -05:00
go.mod chore: go 1.23 is end of life - now go 1.24 (#879) 2025-10-08 16:06:26 -05:00
go.sum Bump testify (#868) 2025-08-27 15:10:53 -05:00
job.go feat: Add option to calculate intervals from job completion time for interval-based scheduling (fixes #565) (#884) 2025-10-21 22:09:46 -05:00
job_test.go feat: Add option to calculate intervals from job completion time for interval-based scheduling (fixes #565) (#884) 2025-10-21 22:09:46 -05:00
logger.go Update docs, tests, and release prep (#629) 2023-12-11 10:39:59 -06:00
logger_test.go don't touch global logger in log tests 2023-11-14 13:45:46 -06:00
monitor.go feat(monitor): introduce MonitorWithStatus (#780) 2024-12-11 22:23:57 -06:00
scheduler.go feat: Add option to calculate intervals from job completion time for interval-based scheduling (fixes #565) (#884) 2025-10-21 22:09:46 -05:00
scheduler_test.go feat: add WithStartDateTimePast WithStartAt option (#882) 2025-10-09 21:45:13 -05:00
util.go Fix memory consumption issue by changing jobOutRequest channels to use pointers and reducing buffer size (#864) 2025-08-27 11:03:34 -05:00
util_test.go Use `errors.New` for non-formatted strings (#870) 2025-09-02 08:48:40 -05:00

README.md

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.

Looking for a visual interface?
Check out gocron-ui — a lightweight web dashboard to monitor, trigger, and manage your gocron jobs in real time.

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

Articles & Blog Posts

Community articles and tutorials about using gocron:

Concepts

  • Job: The job encapsulates a "task", which is made up of a go function and any function parameters. The Job then provides the scheduler with the time the job should next be scheduled to run.
  • Scheduler: The scheduler keeps track of all the jobs and sends each job to the executor when it is ready to be run.
  • Executor: The executor calls the job's task and manages the complexities of different job execution timing requirements (e.g. singletons that shouldn't overrun each other, limiting the max number of jobs running)

Features

Job types

Jobs can be run at various intervals.

  • Duration: Jobs can be run at a fixed time.Duration.
  • Random duration: Jobs can be run at a random time.Duration between a min and max.
  • Cron: Jobs can be run using a crontab.
  • Daily: Jobs can be run every x days at specific times.
  • Weekly: Jobs can be run every x weeks on specific days of the week and at specific times.
  • Monthly: Jobs can be run every x months on specific days of the month and at specific times.
  • One time: Jobs can be run at specific time(s) (either once or many times).

Interval Timing

Jobs can be scheduled with different interval timing modes.

  • Interval from scheduled time (default): By default, jobs calculate their next run time from when they were scheduled to start, resulting in fixed intervals regardless of execution time. Good for cron-like scheduling at predictable times.
  • Interval from completion time: Jobs can calculate their next run time from when they complete, ensuring consistent rest periods between executions. Ideal for rate-limited APIs, resource-intensive jobs, and scenarios where execution time varies.

Concurrency Limits

Jobs can be limited individually or across the entire scheduler.

  • Per job limiting with singleton mode: Jobs can be limited to a single concurrent execution that either reschedules (skips overlapping executions) or queues (waits for the previous execution to finish).
  • Per scheduler limiting with limit mode: Jobs can be limited to a certain number of concurrent executions across the entire scheduler using either reschedule (skip when the limit is met) or queue (jobs are added to a queue to wait for the limit to be available).
  • Note: A scheduler limit and a job limit can both be enabled.

Distributed instances of gocron

Multiple instances of gocron can be run.

  • Elector: An elector can be used to elect a single instance of gocron to run as the primary with the other instances checking to see if a new leader needs to be elected.
    • Implementations: go-co-op electors (don't see what you need? request on slack to get a repo created to contribute it!)
  • Locker: A locker can be used to lock each run of a job to a single instance of gocron. Locker can be at job or scheduler, if it is defined both at job and scheduler then locker of job will take precedence.
    • See Notes in the doc for Locker for details and limitations of the locker design.
    • Implementations: go-co-op lockers (don't see what you need? request on slack to get a repo created to contribute it!)

Events

Job events can trigger actions.

Options

Many job and scheduler options are available.

  • Job options: Job options can be set when creating a job using NewJob.
  • Global job options: Global job options can be set when creating a scheduler using NewScheduler and the WithGlobalJobOptions option.
  • Scheduler options: Scheduler options can be set when creating a scheduler using NewScheduler.

Logging

Logs can be enabled.

  • Logger: The Logger interface can be implemented with your desired logging library. The provided NewLogger uses the standard library's log package.

Metrics

Metrics may be collected from the execution of each job.

  • Monitor:
  • MonitorStatus (includes status and error (if any) of the Job) A monitor can be used to collect metrics for each job from a scheduler.
    • Implementations: go-co-op monitors (don't see what you need? request on slack to get a repo created to contribute it!)

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:

JetBrains

JetBrains logo

Sentry

Sentry logo

Star History

Star History Chart