scheduler

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: May 15, 2026 License: MIT Imports: 5 Imported by: 0

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{}

type Interface

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

func New

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

type JobOption

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

Jump to

Keyboard shortcuts

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