schedule

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package schedule provides a lightweight cron-style task scheduler for Nimbus.

Usage:

s := schedule.New()
s.Every(5*time.Minute, "cleanup-temp", func(ctx context.Context) error {
    return os.RemoveAll("/tmp/nimbus-cache")
})
s.Daily("03:00", "send-reports", func(ctx context.Context) error {
    return reportService.SendDailyDigest(ctx)
})
s.Start(ctx)        // non-blocking, runs in background
defer s.Stop()

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func OnTaskRun

func OnTaskRun(fn func(name, expression, status string, duration time.Duration, output string))

OnTaskRun registers a hook after each scheduled task attempt (Telescope, metrics).

Types

type Locker

type Locker interface {
	TryLock(ctx context.Context, key string, ttl time.Duration) (unlock func(), acquired bool, err error)
}

Locker coordinates schedule execution across multiple app instances. When configured, each task tick tries to acquire a distributed lock and runs only on the lock holder.

type RedisLocker

type RedisLocker struct {
	// contains filtered or unexported fields
}

RedisLocker uses Redis SETNX + TTL for distributed task locking.

func NewRedisLocker

func NewRedisLocker(client *redis.Client) *RedisLocker

NewRedisLocker creates a Redis-backed scheduler locker.

func (*RedisLocker) TryLock

func (l *RedisLocker) TryLock(ctx context.Context, key string, ttl time.Duration) (func(), bool, error)

TryLock attempts to acquire a lock for key with ttl.

type Scheduler

type Scheduler struct {
	// contains filtered or unexported fields
}

Scheduler manages periodic tasks.

func New

func New() *Scheduler

New creates a new Scheduler.

func (*Scheduler) Count

func (s *Scheduler) Count() int

Count returns the number of registered tasks.

func (*Scheduler) Daily

func (s *Scheduler) Daily(at string, name string, fn Task) *Scheduler

Daily registers a task that runs once per day at the given time (HH:MM in local timezone).

s.Daily("03:00", "nightly-cleanup", cleanupHandler)

func (*Scheduler) Every

func (s *Scheduler) Every(interval time.Duration, name string, fn Task) *Scheduler

Every registers a task that runs at a fixed interval.

s.Every(30*time.Second, "ping-check", pingHandler)

func (*Scheduler) EveryFiveMinutes

func (s *Scheduler) EveryFiveMinutes(name string, fn Task) *Scheduler

EveryFiveMinutes registers a task that runs every 5 minutes.

func (*Scheduler) EveryMinute

func (s *Scheduler) EveryMinute(name string, fn Task) *Scheduler

EveryMinute registers a task that runs once per minute.

func (*Scheduler) Hourly

func (s *Scheduler) Hourly(name string, fn Task) *Scheduler

Hourly registers a task that runs once per hour.

func (*Scheduler) Start

func (s *Scheduler) Start(ctx context.Context)

Start begins running all scheduled tasks in the background. Call Stop or cancel the parent context to halt.

func (*Scheduler) Stop

func (s *Scheduler) Stop()

Stop halts all scheduled tasks.

func (*Scheduler) WithLocker

func (s *Scheduler) WithLocker(locker Locker) *Scheduler

WithLocker enables distributed lock coordination for scheduled task runs. Use this in multi-instance deployments to avoid duplicate task execution.

type Task

type Task func(ctx context.Context) error

Task is a scheduled function.

Jump to

Keyboard shortcuts

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