proc

package
v0.4.3 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2025 License: BSD-3-Clause Imports: 13 Imported by: 0

README


This package provides utilities for managing tasklets with lifecycle control. tasklets are utilized in routines and processes. It provides features for safely starting, stopping, and terminating tasklets, handling system signals, and managing tasklets state through various handlers.

Features

  • TaskletHandler: Handles tasklet lifecycle, including initialization, execution, and graceful termination.
  • ProcessHandler: Extends TaskletHandler to manage system signals like SIGINT, SIGTERM, and others.

Installation

go get github.com/exonlabs/go-utils/pkg/proc

Usage Examples

https://github.com/exonlabs/go-utils/tree/master/examples/proc

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CommandHandler

type CommandHandler func(string) string

CommandHandler defines the function handling commands.

type Process

type Process struct {
	*TaskletHandler
	// contains filtered or unexported fields
}

Process manages OS signal handling in addition to Tasklet management.

func NewProcessHandler

func NewProcessHandler(log *logging.Logger, tsk Tasklet) *Process

NewProcessHandler creates a new ProcessHandler with signal handlers for common signals like SIGINT and SIGTERM.

func (*Process) SetCmdHandler

func (h *Process) SetCmdHandler(l comm.Listener, f CommandHandler)

SetCmdHandler sets the command handling function and comm listener to enable command handling feature on process.

func (*Process) SetSignalHandler

func (h *Process) SetSignalHandler(sig os.Signal, fn func())

SetSignalHandler allows the user to define custom handlers for specific signals.

func (*Process) Start

func (h *Process) Start()

Start begins the process and sets up signal handling.

func (*Process) Stop

func (h *Process) Stop()

Stop stop the process.

type Routine

type Routine interface {
	IsEnabled() bool
	IsAlive() bool
	IsInitialized() bool
	Enable()
	Disable()
	Start()
	Stop()
	Kill()
}

Routine defines the methods that must be implemented by any routine managed by routine manager.

type RoutineHandler

type RoutineHandler = TaskletHandler

func NewRoutineHandler

func NewRoutineHandler(log *logging.Logger, tsk Tasklet) *RoutineHandler

type RoutineManager

type RoutineManager struct {
	*Process

	// MonitoringInterval specifies the routines monitoring interval in sec.
	MonitoringInterval float64
	// StoppingDelay specifies the duration to wait for routines to stop.
	StoppingDelay float64
	// contains filtered or unexported fields
}

RoutineManager manages the lifecycle of routines, allowing them to be started, stopped, and monitored.

func NewRoutineManager

func NewRoutineManager(log *logging.Logger) *RoutineManager

New creates a new routine manager instance.

func (*RoutineManager) AddRoutine

func (m *RoutineManager) AddRoutine(name string, rt Routine, enabled bool) error

AddRoutine adds a new routine to the routine manager.

func (*RoutineManager) DelRoutine

func (m *RoutineManager) DelRoutine(name string) error

DelRoutine removes a routine from the routine manager.

func (*RoutineManager) Execute

func (m *RoutineManager) Execute() error

Execute runs the routine check and waits for the specified monitor interval.

func (*RoutineManager) Initialize

func (m *RoutineManager) Initialize() error

Initialize prepares the routine manager.

func (*RoutineManager) ListRoutines

func (m *RoutineManager) ListRoutines() []string

ListRoutines returns a slice of names of all routines managed by routine manager.

func (*RoutineManager) RestartRoutine

func (m *RoutineManager) RestartRoutine(name string) error

RestartRoutine restarts a routine, stopping it if it's currently running.

func (*RoutineManager) StartRoutine

func (m *RoutineManager) StartRoutine(name string) error

StartRoutine activates a routine, allowing it to run.

func (*RoutineManager) StopRoutine

func (m *RoutineManager) StopRoutine(name string) error

StopRoutine deactivates a routine, preventing it from running.

func (*RoutineManager) Terminate

func (m *RoutineManager) Terminate() error

Terminate stops all activated routines and waits for them to finish.

type Tasklet

type Tasklet interface {
	Initialize() error
	Execute() error
	Terminate() error
}

Tasklet defines the interface for tasklets.

type TaskletHandler

type TaskletHandler struct {
	// Log is the logger instance for application logging.
	Log *logging.Logger
	// contains filtered or unexported fields
}

TaskletHandler manages a Tasklet's lifecycle.

func NewTaskletHandler

func NewTaskletHandler(log *logging.Logger, tsk Tasklet) *TaskletHandler

NewTaskletHandler creates a new tasklet handler.

func (*TaskletHandler) Disable

func (h *TaskletHandler) Disable()

Disable sets the tasklet as disabled

func (*TaskletHandler) Enable

func (h *TaskletHandler) Enable()

Enable sets the tasklet as enabled

func (*TaskletHandler) IsAlive

func (h *TaskletHandler) IsAlive() bool

IsAlive returns whether the tasklet is currently active and running.

func (*TaskletHandler) IsEnabled

func (h *TaskletHandler) IsEnabled() bool

IsEnabled returns whether the tasklet is currently enabled.

func (*TaskletHandler) IsInitialized

func (h *TaskletHandler) IsInitialized() bool

IsInitialized returns whether the tasklet is currently initialized.

func (*TaskletHandler) Kill

func (h *TaskletHandler) Kill()

Kill terminates the tasklet by setting both kill and termination events.

func (*TaskletHandler) Run

func (h *TaskletHandler) Run()

Run initiates the tasklet lifecycle, handling initialization, execution, and termination.

func (*TaskletHandler) Sleep

func (h *TaskletHandler) Sleep(timeout float64) bool

Sleep pauses execution for the given timeout duration (in seconds), and waits for either a termination or kill event.

func (*TaskletHandler) Start

func (h *TaskletHandler) Start()

Start initiates the tasklet lifecycle, handling initialization, execution, and termination.

func (*TaskletHandler) Stop

func (h *TaskletHandler) Stop()

Stop gracefully stops the tasklet by setting the termination event.

func (*TaskletHandler) WaitStop

func (h *TaskletHandler) WaitStop(timeout float64) bool

WaitStop waits for tasklet to stop for the given timeout duration (in seconds), returns true if tasklet is stopped before timeout is reached.

Jump to

Keyboard shortcuts

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