scheduler

package
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 17, 2026 License: MIT Imports: 5 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

scheduler — gocron v2 wrapper for periodic jobs

import "github.com/downsized-devs/sdk-go/scheduler"

Stability: Beta — no tests in package. See STABILITY.md.

Thin facade over gocron/v2 that lets you register duration-based, daily, weekly, or monthly jobs from configuration.

Features

  • Register(JobOption) — add a job
  • Start() / Shutdown() lifecycle
  • Job-type constants for duration, daily, weekly, monthly
  • Pairs naturally with redis Lock for multi-replica coordination

Installation

go get github.com/downsized-devs/sdk-go/scheduler

Quick Start

import (
    "context"
    "time"

    "github.com/downsized-devs/sdk-go/logger"
    "github.com/downsized-devs/sdk-go/scheduler"
)

log := logger.Init(logger.Config{Level: "info"})
sch := scheduler.New(scheduler.Config{}, log)

sch.Register(scheduler.JobOption{
    Name:      "heartbeat",
    Type:      scheduler.JobTypeDuration,
    Duration:  10 * time.Second,
    Task: func(ctx context.Context) {
        log.Info(ctx, "tick")
    },
})

sch.Start()
defer sch.Shutdown()

API Reference

Symbol Signature
New func New(cfg Config, log logger.Interface) Interface
Interface.Start () error
Interface.Shutdown () error
Interface.Register (JobOption) error
JobOption Name string; Type string; Duration time.Duration; Daily/Weekly/Monthly time fields; Task func(ctx)

Job-type constants are exported from scheduler.go; check the source for the current set (duration, daily, weekly, monthly at minimum).

Configuration

Field Purpose
(See Config in scheduler.go) Timezone, monitoring hooks.

Examples

Coordinate cron across replicas
sch.Register(scheduler.JobOption{
    Name:     "nightly-rollup",
    Type:     scheduler.JobTypeDaily,
    Daily:    "02:00",
    Task: func(ctx context.Context) {
        lock, err := rdb.Lock(ctx, "cron:rollup", 30*time.Minute)
        if errors.Is(err, redis.ErrNotObtained) { return }
        if err != nil { log.Error(ctx, err); return }
        defer rdb.LockRelease(ctx, lock)
        doRollup(ctx)
    },
})

Error Handling

Register returns an error for unknown job types or malformed time strings. Always check it.

Dependencies

  • Internal: logger
  • External: github.com/go-co-op/gocron/v2

Testing

The package has no tests of its own at the time of writing. When adding tests, use gocron's built-in Run semantics rather than wall-clock sleeps.

go test ./scheduler/...

Contributing

See CONTRIBUTING.md. Add tests as part of any non-trivial change — this package is Beta until coverage lands.

  • redis — locks for cron coordination.
  • logger — required at New time.
  • instrument — already exposes SchedulerRunningTimer / SchedulerRunningCounter for Prometheus.

Documentation

Index

Constants

View Source
const (
	Duration       = "duration"
	RandomDuration = "random-duration"
	Daily          = "daily"
	Weekly         = "weekly"
	Monthly        = "monthly"

	Monday    = "monday"
	Tuesday   = "tuesday"
	Wednesday = "wednesday"
	Thursday  = "thursday"
	Friday    = "friday"
	Saturday  = "saturday"
	Sunday    = "sunday"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct{}

Config is currently empty; it exists so that callers wire scheduler the same way as the other Init-style packages and so future configuration can be added without breaking the API.

type Interface

type Interface interface {
	Start(ctx context.Context)
	Shutdown(ctx context.Context)
	Register(ctx context.Context, opt JobOption, handlerFunc any) error
}

Interface is the public surface of the scheduler. Start kicks off the underlying gocron engine, Shutdown drains it, and Register adds a new job described by opt that runs handlerFunc on schedule.

func Init added in v1.0.0

func Init(cfg Config, log logger.Interface) Interface

Init constructs a scheduler. It panics (through log.Panic) if the underlying gocron engine cannot be created — fail-fast on a misconfigured environment.

type JobOption

type JobOption struct {
	JobType     string
	Duration    time.Duration
	Jitter      time.Duration
	RunningDate int
	RunningDay  time.Weekday
	RunningTime time.Time
}

JobOption describes a single registered job. JobType selects the scheduling strategy (see the package-level Duration, Daily, Weekly, Monthly, RandomDuration constants); the other fields apply conditionally based on the JobType chosen.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL