cron

package
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Example (Pipeline)

Example_pipeline demonstrates driving a pipe.Scheduler with a periodic cron trigger.

package main

import (
	"context"
	"time"

	"github.com/xoctopus/concx/pkg/orch/cron"
	"github.com/xoctopus/concx/pkg/orch/pipe"
	"github.com/xoctopus/concx/pkg/schedx"
)

func main() {
	ctx := context.Background()

	p := pipe.FromJob[string, string, string](
		"fetch",
		pipe.TransformFunc[string, string](func(_ context.Context, v string) (string, error) {
			return v, nil
		}),
	).EndJob(
		"persist",
		pipe.TransformFunc[string, string](func(_ context.Context, v string) (string, error) {
			return v, nil
		}),
	).Build(
		pipe.WithMaxPending(4),
		pipe.WithParallel(2),
	)

	_ = p.Run(ctx)
	defer func() { _ = p.Close() }()

	c := cron.NewWithSchedule(
		ctx,
		cron.MustSpec("@every 10s"),
		schedx.JobFunc[time.Time](func(ctx context.Context, t time.Time) error {
			ret, err := p.Push(ctx, t.Format(time.RFC3339))
			if err != nil {
				return err
			}
			_, err = ret.Result(ctx)
			return err
		}),
		cron.WithOverlapSkip(),
	)
	defer func() { _ = c.Close() }()
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cron

type Cron interface {
	// Done closes when the cron orchestrator is shut down.
	Done() <-chan struct{}
	// Close stops the cron orchestrator and waits for active jobs to exit gracefully up to shutdownTimeout.
	Close() error
}

Cron is an orchestrator recipe for a single recurring scheduled job.

func New

func New(ctx context.Context, spec string, job schedx.Job[time.Time], opts ...Option) (Cron, error)

New creates and starts a single-task Cron orchestrator parsing spec. spec supports standard cron expression, Quartz 6-field with WithSeconds(), and "@every <duration>".

Example

ExampleNew demonstrates registering a recurring job.

package main

import (
	"context"
	"fmt"
	"time"

	"github.com/xoctopus/concx/pkg/orch/cron"
	"github.com/xoctopus/concx/pkg/schedx"
)

func main() {
	ctx := context.Background()

	c, _ := cron.New(ctx, "@every 50ms", schedx.JobFunc[time.Time](func(_ context.Context, _ time.Time) error {
		fmt.Println("periodic tick")
		return nil
	}), cron.WithName("periodic"), cron.WithShutdownTimeout(2*time.Second))
	defer func() { _ = c.Close() }()

	time.Sleep(60 * time.Millisecond)

}
Output:
periodic tick

func NewWithSchedule

func NewWithSchedule(ctx context.Context, schedule Schedule, job schedx.Job[time.Time], opts ...Option) Cron

NewWithSchedule creates and starts a single-task Cron orchestrator with a custom Schedule.

type Option

type Option func(*option)

Option configures a single-task Cron orchestrator.

func WithCallback

func WithCallback(cb schedx.HandlerCallback[time.Time]) Option

WithCallback sets a callback invoked after each job execution finishes (with tick time and error/panic).

func WithLocation

func WithLocation(loc *time.Location) Option

WithLocation sets the timezone for parsing expressions and scheduling. Defaults to time.Local.

func WithMaxPending

func WithMaxPending(maxPending int) Option

WithMaxPending sets the maximum queued tick backlog for the entry. Defaults to 1.

func WithName

func WithName(name string) Option

WithName assigns a human-readable name/label to the cron orchestrator.

func WithOverlapQueue

func WithOverlapQueue(maxPending int) Option

WithOverlapQueue sets the overlap policy to queue new ticks up to maxPending.

func WithOverlapSkip

func WithOverlapSkip() Option

WithOverlapSkip sets the overlap policy to skip new ticks when busy. This is the default policy.

func WithParallel

func WithParallel(parallel int) Option

WithParallel sets how many instances of the job may run concurrently. Defaults to 1.

func WithParser

func WithParser(p robfig.ScheduleParser) Option

WithParser sets a custom cron ScheduleParser.

func WithSeconds

func WithSeconds() Option

WithSeconds enables the 6-field parser where seconds is required (Quartz style).

func WithShutdownTimeout

func WithShutdownTimeout(timeout time.Duration) Option

WithShutdownTimeout sets the maximum duration to wait for running jobs to finish during Close. Defaults to 5 seconds.

func WithoutDetached

func WithoutDetached() Option

WithoutDetached causes the job execution context to be canceled immediately when Cron is canceled.

func WithoutPendingLimitation

func WithoutPendingLimitation() Option

WithoutPendingLimitation disables the queue backlog limit for the entry.

type OverlapPolicy

type OverlapPolicy int

OverlapPolicy controls behavior when a new tick occurs while the job is still executing.

const (
	// OverlapSkip drops the new tick if all parallel slots for the job are busy.
	OverlapSkip OverlapPolicy = iota

	// OverlapQueue pushes the new tick into the job's scheduler queue up to maxPending.
	OverlapQueue
)

type Schedule

type Schedule interface {
	Next(time.Time) time.Time
}

Schedule calculates the next execution time given the current time. It is compatible with robfig/cron/v3.Schedule.

func Every

func Every(duration time.Duration) Schedule

Every returns a Schedule that repeats after a constant duration. Unlike robfig/cron, it supports arbitrary precision including sub-second durations.

func MustSpec

func MustSpec(spec string) Schedule

MustSpec parses spec with the package default parser. Supports standard cron expressions, optional-seconds form, descriptors, and "@every <duration>". Panics if spec is invalid.

Jump to

Keyboard shortcuts

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