handlers

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2024 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package handlers provides three types of handlers.

ActivationHandler provides information of a current state (active or inactive) of an application.

ConfigurationHandler provides information about changes made to configuration and allows to update it in a consistent way. Single file ConfigurationHandler is intended for solutions where only one configuration file is present. Tarred ConfigurationHandler is used when configuration contains of multiple files which are provided as a tar. Custom ConfigurationHandler is used when a user needs to run some custom actions file while updating.

ProcessHandler provides information of changes to a process (start and end) and allows to send signals to it.

Index

Constants

This section is empty.

Variables

View Source
var ErrConfigDeleted = errors.New("configuration was deleted")

Functions

This section is empty.

Types

type ActivationEvent

type ActivationEvent struct {
	State bool
	Error error
}

ActivationEvent contains a current state of an activation (active or inactive) and an error if it was observed.

type ActivationHandler

type ActivationHandler interface {
	// GetWasChangedChannel returns a read only channel with an ActivationEvent when the activation was changed.
	GetWasChangedChannel() <-chan ActivationEvent
	// Close triggers closing of the ActivationHandler.
	Close()
}

ActivationHandler provides information of a current state (active or inactive) of application.

type CmdProcessHandler

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

CmdProcessHandler executes an application and notifies when it starts and ends. The application can be started only once. To start it the second time create a new ProcessHandler. It also allows to send signals to a process while running.

func NewProcessHandler

func NewProcessHandler(cmd *exec.Cmd, logger *slog.Logger) (*CmdProcessHandler, error)

NewProcessHandler returns a pointer to a new CmdProcessHandler instance.

func (*CmdProcessHandler) GetEndedChannel

func (p *CmdProcessHandler) GetEndedChannel() <-chan error

GetEndedChannel returns a read only channel with an error when the process has finished.

func (*CmdProcessHandler) GetStartedChannel

func (p *CmdProcessHandler) GetStartedChannel() <-chan error

GetStartedChannel returns a read only channel with an error when the process has started.

func (*CmdProcessHandler) Kill

func (p *CmdProcessHandler) Kill() error

Kill sends sigkill signal to a process.

func (*CmdProcessHandler) Signal

func (p *CmdProcessHandler) Signal(signal syscall.Signal) error

Signal sends a signal to a process if it's running and returns nil on success or an error.

func (*CmdProcessHandler) Start

func (p *CmdProcessHandler) Start()

Start starts and waits for a command in a new goroutine. It returns start and wait errors to channels.

func (*CmdProcessHandler) Stop

func (p *CmdProcessHandler) Stop() error

Stop sends sigterm signal to a process.

type ConfigurationHandler

type ConfigurationHandler[T any] interface {
	// GetWasChangedChannel returns a read only channel with an error that occurred during configuration changing.
	GetWasChangedChannel() <-chan error
	// Update triggers the configuration update.
	Update()
	// GetUpdateResultChannel returns a read only channel with a T event when the configuration was updated.
	GetUpdateResultChannel() <-chan T
	// Close triggers closing of the ConfigurationHandler.
	Close()
}

ConfigurationHandler provides methods to safely update a configuration. It should be used when the configuration is written and read by different application and locking mechanism can't be used (e.g. two docker containers with shared volume). A new configuration file should only be moved to by writer and hardlinked by reader. ConfigurationHandler provides information about changes made to a configuration file. It allows to update it in a consistent way and to get update result.

type ConfigurationHandlerBase

type ConfigurationHandlerBase[T any] struct {
	// contains filtered or unexported fields
}

ConfigurationHandlerBase listens to changes of a configuration file (which should only be moved to by writer and hardlinked by reader). This triggers creation of a hardlink and pushing 'was changed' event. Then update can be done without any risk of reading/writing the same file.

func NewCustomConfigurationHandler

func NewCustomConfigurationHandler[T any](newConfigFile, hardlink string, update func() T, logger *slog.Logger) (*ConfigurationHandlerBase[T], error)

NewCustomConfigurationHandler returns a new ConfigurationHandler and an error if any occurred. Changes to a newConfigFile will be watched and a hardlink will be created of this file. The update function will be called by ConfigurationHandler.Update().

func NewSingleFileConfigurationHandler

func NewSingleFileConfigurationHandler(newConfig, oldConfig string, logger *slog.Logger) (*ConfigurationHandlerBase[error], error)

NewSingleFileConfigurationHandler returns a new ConfigurationHandler and an error if any occurred. Changes to a newConfig will be watched and when Update is called it will be copied to oldConfig which is safe to read and write if no update is ongoing.

func NewTarredConfigurationHandler

func NewTarredConfigurationHandler(newConfigFile, newConfigDir, oldConfigDir string, logger *slog.Logger) (*ConfigurationHandlerBase[UpdateResult], error)

NewTarredConfigurationHandler returns a new ConfigurationHandler and an error if any occurred. Changes to a newConfigFile will be watched and when Update is called it will extract newConfigFile to newConfigDir and compare and update its content to an oldConfigDir. newConfigDir and oldConfigDir must be on the same device.

func (*ConfigurationHandlerBase[_]) Close

func (c *ConfigurationHandlerBase[_]) Close()

Close triggers closing of the ConfigurationHandlerBase.

func (*ConfigurationHandlerBase[T]) GetUpdateResultChannel

func (c *ConfigurationHandlerBase[T]) GetUpdateResultChannel() <-chan T

GetUpdateResultChannel returns a read only channel with a T event when the configuration was updated. When the handler is closed it returns a nil channel.

func (*ConfigurationHandlerBase[_]) GetWasChangedChannel

func (c *ConfigurationHandlerBase[_]) GetWasChangedChannel() <-chan error

GetWasChangedChannel returns a read only channel with an error that occurred during configuration changing. The error is nil when the configuration was changed successfully. When the handler is closed it returns a nil channel.

func (*ConfigurationHandlerBase[_]) Update

func (c *ConfigurationHandlerBase[_]) Update()

Update triggers the configuration update. When the handler is closed it only logs an error.

type FileActivationHandler

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

FileActivationHandler implements ActivationHandler interface. It uses provided file as a source for ActivationEvents.

func NewActivationHandler

func NewActivationHandler(activationFile string, logger *slog.Logger) (*FileActivationHandler, error)

NewActivationHandler returns a new ActivationHandler and an error if any occurred. Activation is changed based on presence of an activationFile.

func (*FileActivationHandler) Close

func (a *FileActivationHandler) Close()

Close triggers closing of the FileActivationHandler.

func (*FileActivationHandler) GetWasChangedChannel

func (a *FileActivationHandler) GetWasChangedChannel() <-chan ActivationEvent

GetWasChangedChannel returns a read only channel with an ActivationEvent when the activation was changed. When the handler is closed it returns a nil channel.

type Modification

type Modification int

Modification specifies type of modification made to a file while updating.

const (
	Deleted Modification = iota + 1
	Modified
	Created
)

func (Modification) ToString

func (m Modification) ToString() string

ToString returns string name of modification.

type ProcessHandler

type ProcessHandler interface {
	// GetStartedChannel returns a read only channel with an error that occurred during process startup.
	GetStartedChannel() <-chan error
	// GetEndedChannel returns a read only channel with an error that occurred during process termination.
	GetEndedChannel() <-chan error
	// Start starts a process.
	Start()
	// Stop stops a process.
	Stop() error
	// Kill kills a process.
	Kill() error
	// Signal sends a signal to a process.
	Signal(syscall.Signal) error
}

ProcessHandler executes an application and notifies when it starts and ends. It also allows to send signals to a process while running.

type UpdateResult

type UpdateResult struct {
	ChangedFiles map[string]Modification
	Err          error
}

UpdateResult contains a map of file names with modification that was made to them and an error if it was observed.

Directories

Path Synopsis
internal
filesystem
* Copyright (c) 2023 Samsung Electronics Co., Ltd All Rights Reserved * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License.
* Copyright (c) 2023 Samsung Electronics Co., Ltd All Rights Reserved * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License.
global
Package global is created to contain common variables and functions used by many packages.
Package global is created to contain common variables and functions used by many packages.
mocks
Package mocks is a generated GoMock package.
Package mocks is a generated GoMock package.

Jump to

Keyboard shortcuts

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