goticks

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 4, 2025 License: GPL-3.0 Imports: 6 Imported by: 0

README

Goticks

CI codecov Go Reference Go Report Card License

Goticks is a lightweight Go library for building and managing periodic tasks with support for cancellable contexts, customizable tickers, error handling, and more.

Table of Contents

Features

  • Modular design: clear separation between tasks, tickers, and runners.
  • Cancellable contexts for graceful shutdown.
  • Customizable tick generators that are restartable and stoppable.
  • Immediate first tick on start.
  • Built-in retry and error handling support.
  • Zero dependencies outside the Go standard library.

Installation

go get github.com/parametalol/goticks

Usage

package main

import (
    "fmt"
    "time"

    "github.com/parametalol/goticks"
    "github.com/parametalol/goticks/ticker"
)

func main() {
    start := time.Now()
    t := ticker.NewTimer(time.Second)
    goticks.NewTask(t, func(t time.Time) {
        fmt.Println("Current time:", t.Sub(start).Round(time.Second))
    }).Start()

    // Let it run for 3 seconds.
    time.Sleep(3 * time.Second)
    t.Stop()
}

API Reference

Detailed documentation is available on pkg.go.dev.

Contributing

Contributions, issues, and feature requests are welcome! Please check issues or submit a pull request.

License

This project is licensed under the GPL-3.0 License - see the LICENSE file for details.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewTask

func NewTask[TickType any, Fn utils.Func[TickType]](ticker ticker.Tickable[TickType], fn Fn) ticker.Restartable
Example
counter := func(ctx context.Context, tick int) error {
	fmt.Println("tick #", tick)
	switch tick {
	case 2:
		return fmt.Errorf("non-stop error")
	case 3:
		return fmt.Errorf("stop error: %w", utils.ErrStopped)
	}
	return nil
}

// This ticker issues ticks of type int.
ticker := ticker.New[int]()

// NewTask initializes a procedure to call counter on the ticker ticks, log
// the attempts and the errors, and retry 2 times on non-permanent errors.
NewTask(ticker,
	utils.WithRetry[int](utils.SimpleRetryPolicy(3),
		utils.WithLog[int](os.Stdout, os.Stdout, "example",
			counter))).
	// Start the ticker loop in a goroutine:
	Start()

for tick := range 10 {
	ticker.Tick(tick).
		// Wait for the tick to be processed
		// to ensure stable sequential output:
		Wait()
}
Output:

Calling example
tick # 0
Calling example
tick # 1
Calling example
tick # 2
Execution of example failed with error: non-stop error
Retry 1 of example
tick # 2
Execution of example failed after retry 1 with error: non-stop error
Retry 2 of example
tick # 2
Execution of example failed after retry 2 with error: non-stop error
Calling example
tick # 3
Execution of example stopped with error: stop error: stopped

Types

type Task

type Task interface {
	Start()
	Stop()
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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