workers

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2026 License: MIT Imports: 7 Imported by: 0

README

workers

Package for managing periodic tasks (workers) in Go applications.

Features

  • Run workers with specified intervals
  • Enable/disable workers via settings file
  • Worker result logging
  • Graceful shutdown

Installation

go get github.com/alexeevp/workers

Usage

package main

import (
    "context"
    "log/slog"
    "time"

    "github.com/alexeevp/workers"
    "github.com/alexeevp/workerlog"
)

// Example worker
type MyWorker struct{}

func (w *MyWorker) Name() string                    { return "my_worker" }
func (w *MyWorker) Description() string             { return "My worker" }
func (w *MyWorker) Interval() time.Duration         { return time.Minute }
func (w *MyWorker) Run(ctx context.Context) error   { /* logic */ return nil }

func main() {
    log := slog.Default()
    logWriter := workerlog.NewWriter("var/workers/logs")
    
    scheduler := workers.NewScheduler(log, []workers.Worker{&MyWorker{}}, logWriter)
    
    // Start
    scheduler.Start(context.Background())
    
    // Stop
    defer scheduler.Stop(context.Background())
}

Settings

Worker status is stored in var/workers/settings.json:

{
  "my_worker": true,
  "another_worker": false
}

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WorkerStatus

func WorkerStatus(name string) bool

WorkerStatus returns the worker status (true = enabled, false = disabled) If the worker is not found in settings, returns false (disabled by default)

func WorkerToggle

func WorkerToggle(name string, enabled bool) error

WorkerToggle sets the worker status

Types

type LoggableWorker

type LoggableWorker interface {
	Worker
	// GetResultLogs returns accumulated result logs
	GetResultLogs() []string
	// ClearResultLogs clears accumulated result logs
	ClearResultLogs()
}

LoggableWorker - optional interface for workers with result logging

type Scheduler

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

Scheduler manages worker execution

func NewScheduler

func NewScheduler(log *slog.Logger, workers []Worker, logWriter *workerlog.Writer) *Scheduler

NewScheduler creates a new scheduler

func (*Scheduler) GetLogWriter

func (s *Scheduler) GetLogWriter() *workerlog.Writer

GetLogWriter returns the writer for worker logs

func (*Scheduler) GetWorkers

func (s *Scheduler) GetWorkers(ctx context.Context) []WorkerInfo

GetWorkers returns information about all workers

func (*Scheduler) Start

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

Start starts all workers

func (*Scheduler) Stop

func (s *Scheduler) Stop(ctx context.Context) error

Stop stops all workers and waits for them to complete

type Worker

type Worker interface {
	// Name returns a unique worker name (for logs and settings key)
	Name() string

	// Description returns a worker description for admin panel
	Description() string

	// Interval returns the execution interval
	Interval() time.Duration

	// Run executes the task
	Run(ctx context.Context) error
}

Worker represents a periodic task

type WorkerInfo

type WorkerInfo struct {
	Name        string
	Description string
	Interval    time.Duration
	Enabled     bool
	LogFiles    []workerlog.FileInfo // recent log files (for LoggableWorker)
}

WorkerInfo contains information about a worker for display in the admin panel

Jump to

Keyboard shortcuts

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