scheduled

package module
v0.0.0-...-c4fea85 Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2024 License: MIT Imports: 7 Imported by: 0

README

scheduled

scheduled implements an in-memory scheduler to run functions in a time loop. It also supports running functions with a CRON syntax scheduling.


Usage

Get
> go get github.com/hum/scheduled
Basic

Allows for a creation of simple functions, which get executed every Interval tick.

import "github.com/hum/scheduled"

// Create a new task via scheduled.NewTask
task := scheduled.NewTask(scheduled.TaskOpts{
  // The actual function to be ran
  Fn: func() error {
    fmt.Println("Hello from task!")
    return nil
  },
  // How often should the function be ran
  Interval: 10*time.Second,
})

// Initialise a new scheduler
scheduler := scheduled.NewScheduler()
defer scheduler.Stop()

// Add the task to the scheduler
scheduler.RegisterTask(task)

You can also bound the Interval with MaxIter to only allow N intervals to pass.

task := scheduled.NewTask(scheduled.TaskOpts{
  Fn: func() error {
    fmt.Println("Hello from task!")
    return nil
  },
  Interval: 10*time.Second,
  // Stops executing after 10 runs
  MaxIter: 10,
})
With start time and end time

You can also specify StartTime and/or EndTime to time bound the task's execution. StartTime allows the scheduler to wait until the specified time to execute the function, while EndTime is used with interval or CRON to end the execution of the task after a certain period of time passes.

task := scheduled.NewTask(scheduled.TaskOpts{
  Fn: func() error {
    fmt.Println("Hello from task!")
    return nil
  },
  Interval: 10*time.Second,
  // Starts the function in 5 hours, then runs it every 10 seconds
  StartTime: time.Now().Add(5*time.Hour),
  // Stops executing after 24 hours
  EndTime: time.Now().Add(24*time.Hour),
})
With CRON

A task supports a CRON syntax to act as the interval function. Use Cron to pass in a CRON string.

NOTE: Cron has a priority over Interval. If you pass both, Cron will be used.

task := scheduled.NewTask(scheduled.TaskOpts{
  Fn: func() error {
    fmt.Println("Hello from task!")
    return nil
  },
  // Run every minute
  Cron: "* * * * *",
})

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Logger *slog.Logger = slog.Default()

Functions

func NewTask

func NewTask(opts TaskOpts) *task

Types

type Scheduler

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

Scheduler is the high-level structure to manage the in-memory tasks. Do not create the structure manually. Always use `scheduled.NewScheduler()`

func NewScheduler

func NewScheduler() *Scheduler

NewScheduler creates a new instance of the scheduler with an empty initialised slice of tasks.

To use the scheduler, add a task via `scheduler.RegisterTask` or `scheduler.RunOnce`

func (*Scheduler) GetTask

func (s *Scheduler) GetTask(idx uuid.UUID) (*task, error)

GetTask returns a task registered under provided uuid.UUID. If the task is not registered, the function returns an error.

func (*Scheduler) RegisterTask

func (s *Scheduler) RegisterTask(t *task) error

RegisterTask allows for a task to be added within the execution loop of the scheduler

func (*Scheduler) RemoveTask

func (s *Scheduler) RemoveTask(idx uuid.UUID) error

RemoveTask removes a given task from the scheduler while also stopping its execution if it were already scheduled

func (*Scheduler) RunOnce

func (s *Scheduler) RunOnce(t *task) error

RunOnce allows a one-time execution of a task directly within the runtime of the scheduler

func (*Scheduler) Stop

func (s *Scheduler) Stop()

Stop removes all scheduled tasks from the scheduler

type TaskErrFunc

type TaskErrFunc func(err error)

type TaskFunc

type TaskFunc func() error

type TaskOpts

type TaskOpts struct {
	// Underlying function to be ran within the scheduler.
	Fn TaskFunc

	// In case of an error in Fn, ErrFn will be executed if provided.
	ErrFn TaskErrFunc

	// Offsets the initial startup to a given start time. By default it will start immediately on schedule.
	StartTime time.Time

	// Restricts the time boundary of the runtime to an end date. By default it is unbound.
	EndTime time.Time

	// Interval for Fn's execution within the scheduler. This is the function's tick.
	Interval time.Duration

	// Limits the amount of times a single task can be executed in the runtime. After `MaxIter` steps, it will end.
	// By default, it is 0 (infinite). Negative values are not allowed.
	MaxIter int

	// Allows the scheduling based on a CRON string. Overrides `Interval`
	Cron string
}

Jump to

Keyboard shortcuts

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