百科.dev
全部条目AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
G

gocron

> 编程语言
开源

简单流畅的 Go Cron 计划。这是一个分支,来自 https://GitHub.com/jasonlvhit/gocron

7.1K stars0 点赞0 次浏览
访问官网GitHub

工具介绍

简单流畅的 Go Cron 计划。这是一个分支,来自 https://GitHub.com/jasonlvhit/gocron

gocron: A Golang Job Scheduling Package

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
…

Examples

  • Go doc examples
  • Examples directory

Articles & Blog Posts

Community articles and tutorials about using gocron:

  • Building a dynamic, highly available scheduler in Go - A deep dive into building a highly available scheduler using gocron, MongoDB change streams, and leader election patterns for the FACEIT Watch platform.

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.

  • Listeners: Can be added to a job, with event listeners, or all jobs across the scheduler to listen for job events and 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 and scheduler lifecycle events.

  • Monitor: A monitor can be used to collect metrics for each job from a scheduler.

    • Implementations: There are currently no open source implementations of the Monitor interface available. We'd love for you to be the first to contribute one! Check out the Monitor interface documentation to get started, or reach out on Slack if you'd like to discuss your implementation.
  • MonitorStatus: Extends Monitor with status and error tracking for each job.

    • Implementations: There are currently no open source implementations of the MonitorStatus interface available. We'd love for you to be the first to contribute one! Check out the MonitorStatus interface documentation to get started, or reach out on Slack if you'd like to discuss your implementation.
  • SchedulerMonitor: A scheduler monitor provides comprehensive observability into scheduler and job lifecycle events.

    Available Metrics:

    • Scheduler Lifecycle: SchedulerStarted, SchedulerStopped, SchedulerShutdown
    • Job Management: JobRegistered, JobUnregistered - track jobs added/removed from scheduler
    • Job Execution: JobStarted, JobRunning, JobCompleted, JobFailed - monitor job execution flow
    • Performance: JobExecutionTime, JobSchedulingDelay - measure job duration and scheduling lag
    • Concurrency: ConcurrencyLimitReached - detect when singleton or limit mode constraints are hit

    Derived Metrics (calculable from events):

    • Error rate: JobFailed / (JobCompleted + JobFailed)
    • Average execution time: from JobExecutionTime events
    • Active jobs: JobRegistered - JobUnregistered
    • Current queue depth: JobStarted - (JobCompleted + JobFailed)

    Example - Prometheus Integration:

…

Use Cases: Prometheus metrics, custom dashboards, alerting systems, performance monitoring

  • Implementations: There are currently no open source implementations of the SchedulerMonitor interface available. We'd love for you to be the first to contribute one! Check out the SchedulerMonitor interface documentation to get started, or reach out on Slack if you'd like to discuss your implementation.

Testing

The gocron library is set up to enable testing.

  • Mocks are provided in the mock package using gomock.
  • Time can be mocked by passing in a FakeClock to WithClock - see the example on WithClock.

Supporters

We appreciate the support for free and open source software!

This project is supported by:

JetBrains

Sentry

GitHub Issues· 0 开放

在 GitHub 查看全部

暂无开放 Issues,或尚未同步最近议题。

核心特点

  • •Go doc examples
  • •Examples directory
  • •Job: The job encapsulates a "task", which is made up of a go function and any function parameters. The Job then
  • •Scheduler: The scheduler keeps track of all the jobs and sends each job to the executor when
  • •Executor: The executor calls the job's task and manages the complexities of different job
  • •Duration:
  • •Random duration:
  • •Monthly:
  • •One time:
  • •Interval from scheduled time (default):

> 标签

Goclockworkcrongocrongolang

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言