tickermod

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: May 21, 2026 License: Apache-2.0 Imports: 2 Imported by: 0

README

tickermod

tickermod runs a function on a fixed interval. The ticker stops when the function returns a non-nil error.

package main

import (
	"log/slog"
	"os"
	"time"

	"github.com/go-srvc/mods/logmod"
	"github.com/go-srvc/mods/sigmod"
	"github.com/go-srvc/mods/tickermod"
	"github.com/go-srvc/srvc"
)

func main() {
	srvc.RunAndExit(
		logmod.New(),
		sigmod.New(os.Interrupt),
		tickermod.New(
			tickermod.WithInterval(5*time.Second),
			tickermod.WithFunc(func() error {
				slog.Info("tick")
				return nil
			}),
		),
	)
}

Documentation

Overview

Package tickermod provides ticker functionality as a module.

Index

Examples

Constants

View Source
const (
	ErrMissingInterval = errStr("interval not set")
	ErrMissingTickFunc = errStr("tick function not set")
)
View Source
const ID = "tickermod"

Variables

This section is empty.

Functions

This section is empty.

Types

type Opt

type Opt func(*Ticker) error

func WithFunc

func WithFunc(fn func() error) Opt

WithFunc sets function to be called on each tick. If non nil error is returned, ticker stops.

func WithInterval

func WithInterval(d time.Duration) Opt

WithInterval sets ticker interval.

func WithIntervalFn

func WithIntervalFn(fn func() (time.Duration, error)) Opt

WithIntervalFn sets ticker interval using provided function.

type Ticker

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

func New

func New(opts ...Opt) *Ticker

New creates ticker with given options. WithInterval and WithFunc options are mandatory.

Example
package main

import (
	"errors"
	"log/slog"
	"time"

	"github.com/go-srvc/mods/tickermod"
	"github.com/go-srvc/srvc"
)

func main() {
	srvc.RunAndExit(
		tickermod.New(
			tickermod.WithInterval(time.Second),
			tickermod.WithFunc(func() error {
				slog.Info("Hello from ticker")
				return errors.New("ticker error")
			}),
		),
	)
}

func (*Ticker) ID

func (t *Ticker) ID() string

func (*Ticker) Init

func (t *Ticker) Init() error

func (*Ticker) Run

func (t *Ticker) Run() error

func (*Ticker) Stop

func (t *Ticker) Stop() error

Jump to

Keyboard shortcuts

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