services

package
v6.6.5 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2025 License: GPL-3.0 Imports: 20 Imported by: 0

README

Services Package

The services package provides a framework for managing long-running background services with lifecycle management, pause/unpause functionality, and restart capabilities.

Basic Usage

Creating and Starting Services
package main

import (
    "log/slog"
    "os"
    "os/signal"
    "syscall"
    
    "github.com/TrueBlocks/trueblocks-core/sdk/services"
)

func main() {
    logger := slog.New(slog.NewTextHandler(os.Stdout, nil))
    
    // Create services
    scraper := services.NewScrapeService(logger, "blooms", []string{"mainnet"}, 13, 2000)
    monitor := services.NewMonitorService(logger)
    api := services.NewApiService(logger)
    control := services.NewControlService(logger)
    
    // Create service manager
    serviceList := []services.Servicer{scraper, monitor, api, control}
    manager := services.NewServiceManager(serviceList, logger)
    
    // Attach manager to control service for HTTP API
    control.AttachServiceManager(manager)
    
    // Start all services
    manager.StartAllServices()
    
    // Handle shutdown signals
    manager.HandleSignals()
    
    // Keep main goroutine alive
    select {}
}
HTTP Control API

The control service provides REST endpoints for managing services:

  • GET /status - Check service status
  • POST /pause?name=service_name - Pause a service
  • POST /unpause?name=service_name - Unpause a service
  • POST /restart?name=service_name - Restart a service

Use name=all to operate on all applicable services.

Service Interfaces

Services implement different interfaces based on their capabilities:

  • Servicer - Basic service interface (all services)
  • Pauser - Services that can be paused/unpaused (scraper, monitor)
  • Restarter - Services that can be restarted (scraper, monitor)
Creating Custom Services
type MyService struct {
    logger *slog.Logger
    ctx    context.Context
    cancel context.CancelFunc
}

func (s *MyService) Name() string { return "my-service" }

func (s *MyService) Initialize() error {
    // Setup logic here
    return nil
}

func (s *MyService) Process(ready chan bool) error {
    ready <- true
    for {
        select {
        case <-s.ctx.Done():
            return nil
        default:
            // Main service logic here
        }
    }
}

func (s *MyService) Cleanup() {
    s.cancel()
    // Reset context for potential restart
    s.ctx, s.cancel = context.WithCancel(context.Background())
}

func (s *MyService) Logger() *slog.Logger { return s.logger }

For more information about the TrueBlocks SDK, see the parent README.

Documentation

Overview

Package services provides a framework for managing long-running background services with lifecycle management, pause/unpause functionality, and restart capabilities.

Basic Usage

Create and start services using the ServiceManager:

logger := slog.New(slog.NewTextHandler(os.Stdout, nil))

// Create services
scraper := services.NewScrapeService(logger, "blooms", []string{"mainnet"}, 13, 2000)
monitor := services.NewMonitorService(logger)
api := services.NewApiService(logger)
control := services.NewControlService(logger)

// Create service manager
serviceList := []services.Servicer{scraper, monitor, api, control}
manager := services.NewServiceManager(serviceList, logger)

// Attach manager to control service for HTTP API
control.AttachServiceManager(manager)

// Start all services
manager.StartAllServices()

// Handle shutdown signals
manager.HandleSignals()

HTTP Control API

The control service provides REST endpoints for managing services:

GET /status                     - Check service status
POST /pause?name=service_name   - Pause a service
POST /unpause?name=service_name - Unpause a service
POST /restart?name=service_name - Restart a service

Use name=all to operate on all applicable services.

Service Interfaces

Services implement different interfaces based on their capabilities:

Servicer  - Basic service interface (all services)
Pauser    - Services that can be paused/unpaused (scraper, monitor)
Restarter - Services that can be restarted (scraper, monitor)

Creating Custom Services

Implement the Servicer interface to create custom services:

type MyService struct {
	logger *slog.Logger
	ctx    context.Context
	cancel context.CancelFunc
}

func (s *MyService) Name() string { return "my-service" }

func (s *MyService) Initialize() error {
	// Setup logic here
	return nil
}

func (s *MyService) Process(ready chan bool) error {
	ready <- true
	for {
		select {
		case <-s.ctx.Done():
			return nil
		default:
			// Main service logic here
		}
	}
}

func (s *MyService) Cleanup() {
	s.cancel()
	// Reset context for potential restart
	s.ctx, s.cancel = context.WithCancel(context.Background())
}

func (s *MyService) Logger() *slog.Logger { return s.logger }

For more information about the TrueBlocks SDK, see the parent README.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func StartService

func StartService(svc Servicer, stopChan chan os.Signal)

Types

type ApiService

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

ApiService implements Servicer and Restarter interfaces

func NewApiService

func NewApiService(logger *slog.Logger) *ApiService

func (*ApiService) ApiUrl

func (s *ApiService) ApiUrl() string

func (*ApiService) Cleanup

func (s *ApiService) Cleanup()

func (*ApiService) Initialize

func (s *ApiService) Initialize() error

func (*ApiService) IsPausable

func (s *ApiService) IsPausable() bool

Pauser interface implementation (UI consistency - tracks pause state but doesn't stop service)

func (*ApiService) IsPaused

func (s *ApiService) IsPaused() bool

func (*ApiService) Logger

func (s *ApiService) Logger() *slog.Logger

func (*ApiService) Name

func (s *ApiService) Name() string

func (*ApiService) Pause

func (s *ApiService) Pause() bool

func (*ApiService) Process

func (s *ApiService) Process(ready chan bool) error

func (*ApiService) Unpause

func (s *ApiService) Unpause() bool

type ChildManager

type ChildManager interface {
	Pauser
	HasChild() bool
	KillChild() bool
	RestartChild() bool
}

type ControlService

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

func NewControlService

func NewControlService(logger *slog.Logger) *ControlService

func (*ControlService) AddHandler

func (s *ControlService) AddHandler(pattern string, h http.HandlerFunc)

Extension points

func (*ControlService) AttachServiceManager

func (s *ControlService) AttachServiceManager(manager *ServiceManager)

func (*ControlService) Cleanup

func (s *ControlService) Cleanup()

func (*ControlService) DefaultRootHandler

func (s *ControlService) DefaultRootHandler() http.HandlerFunc

func (*ControlService) Initialize

func (s *ControlService) Initialize() error

func (*ControlService) Logger

func (s *ControlService) Logger() *slog.Logger

func (*ControlService) Name

func (s *ControlService) Name() string

func (*ControlService) Port

func (s *ControlService) Port() int

func (*ControlService) Process

func (s *ControlService) Process(ready chan bool) error

func (*ControlService) SetRootHandler

func (s *ControlService) SetRootHandler(h http.HandlerFunc)

type IpfsService

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

IpfsService implements Servicer and Restarter interfaces

func NewIpfsService

func NewIpfsService(logger *slog.Logger) *IpfsService

func (*IpfsService) ApiMultiaddr

func (s *IpfsService) ApiMultiaddr() string

func (*IpfsService) ApiPort

func (s *IpfsService) ApiPort() string

func (*IpfsService) Cleanup

func (s *IpfsService) Cleanup()

func (*IpfsService) Initialize

func (s *IpfsService) Initialize() error

func (*IpfsService) IsPausable

func (s *IpfsService) IsPausable() bool

Pauser interface implementation (UI consistency - tracks pause state but doesn't stop service)

func (*IpfsService) IsPaused

func (s *IpfsService) IsPaused() bool

func (*IpfsService) Logger

func (s *IpfsService) Logger() *slog.Logger

func (*IpfsService) Name

func (s *IpfsService) Name() string

func (*IpfsService) Pause

func (s *IpfsService) Pause() bool

func (*IpfsService) Process

func (s *IpfsService) Process(ready chan bool) error

func (*IpfsService) Unpause

func (s *IpfsService) Unpause() bool

func (*IpfsService) WasRunning

func (s *IpfsService) WasRunning() bool

type MonitorService

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

MonitorService implements Servicer, Pauser, and Restarter interfaces

func NewMonitorService

func NewMonitorService(logger *slog.Logger) *MonitorService

func (*MonitorService) Cleanup

func (s *MonitorService) Cleanup()

func (*MonitorService) Initialize

func (s *MonitorService) Initialize() error

func (*MonitorService) IsPaused

func (s *MonitorService) IsPaused() bool

func (*MonitorService) Logger

func (s *MonitorService) Logger() *slog.Logger

func (*MonitorService) Name

func (s *MonitorService) Name() string

func (*MonitorService) Pause

func (s *MonitorService) Pause() bool

func (*MonitorService) Process

func (s *MonitorService) Process(ready chan bool) error

func (*MonitorService) Unpause

func (s *MonitorService) Unpause() bool

type Pauser

type Pauser interface {
	Servicer
	IsPaused() bool
	Pause() bool
	Unpause() bool
}

type Restarter

type Restarter interface {
	Servicer
}

Restarter is a marker interface for services that can be restarted

type ScrapeService

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

ScrapeService implements Servicer, Pauser, and Restarter interfaces

func NewScrapeService

func NewScrapeService(logger *slog.Logger, initMode string, configTargets []string, sleep int, blockCnt int) *ScrapeService

func (*ScrapeService) Cleanup

func (s *ScrapeService) Cleanup()

func (*ScrapeService) Initialize

func (s *ScrapeService) Initialize() error

func (*ScrapeService) IsPausable

func (s *ScrapeService) IsPausable() bool

func (*ScrapeService) IsPaused

func (s *ScrapeService) IsPaused() bool

func (*ScrapeService) Logger

func (s *ScrapeService) Logger() *slog.Logger

func (*ScrapeService) Name

func (s *ScrapeService) Name() string

func (*ScrapeService) Pause

func (s *ScrapeService) Pause() bool

func (*ScrapeService) Process

func (s *ScrapeService) Process(ready chan bool) error

func (*ScrapeService) Unpause

func (s *ScrapeService) Unpause() bool

type ServiceManager

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

func NewServiceManager

func NewServiceManager(services []Servicer, logger *slog.Logger) *ServiceManager

func (*ServiceManager) HandleSignals

func (sm *ServiceManager) HandleSignals()

func (*ServiceManager) IsPaused

func (sm *ServiceManager) IsPaused(name string) ([]map[string]string, error)

func (*ServiceManager) Pause

func (sm *ServiceManager) Pause(name string) ([]map[string]string, error)

func (*ServiceManager) Restart

func (sm *ServiceManager) Restart(name string) ([]map[string]string, error)

------------------------------------------------------------------------------------- Restart restarts services

func (*ServiceManager) StartAllServices

func (sm *ServiceManager) StartAllServices() error

func (*ServiceManager) Unpause

func (sm *ServiceManager) Unpause(name string) ([]map[string]string, error)

type Servicer

type Servicer interface {
	Name() string
	Initialize() error
	Process(chan bool) error
	Cleanup()
	Logger() *slog.Logger
}

Jump to

Keyboard shortcuts

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