计划任务
Go to file
John Roesler 77dba0ad18
correct lock to use job.name instead of id (#623)
Co-authored-by: Le Ha Khiem <hakhiem.va@gmail.com>
2023-11-16 07:49:43 -06:00
.github test needs to output coverage 2023-11-14 10:22:45 -06:00
mocks update docs and add mocks pkg (#610) 2023-11-09 16:00:18 -06:00
.gitignore initial clean v2 commit history 2023-11-08 11:11:42 -06:00
.golangci.yaml initial clean v2 commit history 2023-11-08 11:11:42 -06:00
.pre-commit-config.yaml initial clean v2 commit history 2023-11-08 11:11:42 -06: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 test needs to output coverage 2023-11-14 10:22:45 -06:00
README.md Update README.md (#619) 2023-11-14 10:26:44 -06:00
SECURITY.md initial clean v2 commit history 2023-11-08 11:11:42 -06:00
codecov.yml don't fail codecov 2023-11-14 13:28:36 -06:00
distributed.go add distributed locker for v2 (#614) 2023-11-14 09:56:05 -06:00
errors.go add distributed locker for v2 (#614) 2023-11-14 09:56:05 -06:00
example_test.go add job name to event listners (#621) 2023-11-14 11:39:27 -06:00
executor.go correct lock to use job.name instead of id (#623) 2023-11-16 07:49:43 -06:00
go.mod support go 1.20+ (#608) 2023-11-09 09:49:21 -06:00
go.sum initial clean v2 commit history 2023-11-08 11:11:42 -06:00
job.go add job name to event listners (#621) 2023-11-14 11:39:27 -06:00
job_test.go add job name to event listners (#621) 2023-11-14 11:39:27 -06:00
logger.go don't touch global logger in log tests 2023-11-14 13:45:46 -06:00
logger_test.go don't touch global logger in log tests 2023-11-14 13:45:46 -06:00
scheduler.go add distributed locker for v2 (#614) 2023-11-14 09:56:05 -06:00
scheduler_test.go race in test 2023-11-14 15:09:02 -06:00
util.go support go 1.20+ (#608) 2023-11-09 09:49:21 -06:00
util_test.go initial clean v2 commit history 2023-11-08 11:11:42 -06: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.

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

Concepts

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

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

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

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.
  • Limited Concurrency: 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).
  • 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.
    • Locker: A locker can be used to lock each run of a job to a single instance of gocron.
  • 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.
    • 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.
  • Mocking: The gocron library is set up to enable testing.

Supporters

Jetbrains supports this project with Intellij licenses. We appreciate their support for free and open source software!

Star History

Star History Chart