Configs2026年7月17日·1 分钟阅读

gocron — Fluent Job Scheduling Library for Go

A Go library for scheduling recurring jobs using a human-readable fluent API, supporting cron expressions, fixed intervals, one-time execution, distributed locking, and timezone-aware scheduling.

Agent 就绪

Agent 可直接安装

这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
gocron
直接安装命令
npx -y tokrepo@latest install a65cc174-8177-11f1-9bc6-00163e2b0d79 --target codex

先 dry-run 确认安装计划,再运行此命令。

Introduction

gocron is a Go job scheduling library that lets developers define recurring tasks using a fluent API or standard cron expressions. It handles timing, concurrency control, and job lifecycle management so applications can schedule background work without deploying a separate scheduler service like cron or Celery.

What gocron Does

  • Schedules jobs at fixed intervals, cron expressions, or specific times
  • Provides a fluent builder API for readable schedule definitions
  • Supports distributed locking to prevent duplicate execution across instances
  • Handles job concurrency limits, singleton mode, and error recovery
  • Offers event listeners for job start, completion, and failure hooks

Architecture Overview

gocron v2 uses a scheduler that maintains a priority queue of jobs sorted by next execution time. A goroutine loop sleeps until the next job is due, then dispatches it to a worker pool. Each job wraps a Go function and its arguments. The scheduler supports pluggable distributed lockers (Redis, PostgreSQL, MongoDB) for multi-instance deployments, ensuring only one instance runs a given job.

Self-Hosting & Configuration

  • Create a scheduler with gocron.NewScheduler() and optional configuration
  • Define jobs with DurationJob, CronJob, DailyJob, WeeklyJob, or MonthlyJob
  • Set timezone with gocron.WithLocation for locale-aware scheduling
  • Use gocron.WithDistributedLocker to prevent duplicate execution in clusters
  • Call s.StopJobs() for graceful shutdown that waits for running jobs to complete

Key Features

  • Multiple schedule types: duration, cron, daily, weekly, monthly, and one-time jobs
  • Distributed locking: pluggable lockers for Redis, PostgreSQL, and MongoDB
  • Singleton mode: ensures only one instance of a job runs at a time
  • Event listeners: hooks for before/after job execution and error handling
  • Elector mode: elect a single scheduler instance to run jobs in a cluster

Comparison with Similar Tools

  • robfig/cron — the original Go cron library; gocron adds fluent API, distributed locking, and v2 improvements
  • Asynq — Redis-based task queue with retry and scheduling; heavier but better for distributed job processing
  • Temporal — full workflow orchestration platform; overkill for simple recurring tasks that gocron handles
  • APScheduler (Python) — similar feature set in Python; gocron is the Go equivalent with native goroutine integration

FAQ

Q: Can I use cron expressions? A: Yes. Use gocron.CronJob("*/5 * * * *", false) for standard 5-field cron or pass true for 6-field (with seconds) expressions.

Q: How does distributed locking work? A: Configure a locker (e.g., Redis-based) via gocron.WithDistributedLocker. Before each job runs, the scheduler acquires a lock. Only the instance that gets the lock executes the job.

Q: What happens if a job takes longer than its interval? A: By default, a new instance starts on schedule. Use SingletonMode to skip the next run if the previous one is still executing.

Q: Can I dynamically add or remove jobs? A: Yes. Call s.NewJob() to add jobs and s.RemoveJob(id) to remove them while the scheduler is running.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产