rxd

package module
v1.1.3 Latest Latest
Warning

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

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

README

RxDaemon (RxD)

codecov

A simple (alpha) reactive services daemon

NOTE: Since RxD uses Intracom internally and because Intracom recently went through a large refactor from using mutex locks to no-locks, both Intracom and RxD should still be treated as an alpha project until further testing can be done.

RxD leverages Intracom for internal comms and allowing the ability for your individual RxD services to subscribe interest to the lifecycle states of other RxD services running alongside each other. This means each service can be notified independently of what state another service is in and since you control the logic of the lifecycle method and returning the next state you wish to enter. You can ultimately have your service "watch and react" to a state change of another service.

A good example to imagine here would be something like ServiceA that has subscribed interest in another service, ServiceB, where ServiceB happens to be a health check service. It might maintain a live TCP connection to an external service, run interval queries against a database engine, or health check a docker container, it doesnt really matter. The goal here is to NOT have alot of services individually doing their own health checks against the same resource because the more services you have the more checks you are potentially doing against the same resource which might be creating socket connections or doing file I/O not to mention the potential code duplication lines for each service to do their own check. Why not instead, write a service that can do the main logic of the check then signal using its own lifecycle states to anyone who is interested in that health check logic. RxD gives us that ability.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AllServicesEnterState added in v1.1.0

func AllServicesEnterState(sc *ServiceContext, target State, serviceNames ...string) (<-chan States, context.CancelFunc)

func AllServicesStates added in v1.0.0

func AllServicesStates(sc *ServiceContext) (<-chan States, context.CancelFunc)

func AnyServicesEnterState added in v1.1.0

func AnyServicesEnterState(sc *ServiceContext, target State, serviceNames ...string) (<-chan States, context.CancelFunc)

func AnyServicesExitState added in v1.0.0

func AnyServicesExitState(sc *ServiceContext, target State, serviceNames ...string) (<-chan States, context.CancelFunc)

AnyServicesExitState

func NewDaemon

func NewDaemon(conf DaemonConfig) *daemon

NewDaemon creates and return an instance of the reactive daemon

func NewServiceOpts

func NewServiceOpts(options ...ServiceOption) *serviceOpts

NewServiceOpts will apply all options in the order given and return the final options back.

Types

type DaemonConfig added in v1.1.0

type DaemonConfig struct {
	Name               string       // name of the daemon will be used in logging
	Signals            []os.Signal  // OS signals you want your daemon to listen for
	LogHandler         slog.Handler // log handler for daemon/manager/services
	IntracomLogHandler slog.Handler // log handler for embedded intracom instances (debugging)
}

type RunPolicy

type RunPolicy int

RunPolicy service option type representing the run policy of a given service basically controlling different ways of stopping a service like running only once when it succeeds without an error on Run

const (
	// RunUntilStoppedPolicy will continue to run the service until a StopState is returned at some point
	RunUntilStoppedPolicy RunPolicy = iota
	// RetryUntilSuccessPolicy will continue to re-run the service as long fails happen, use for running a service once successfully
	RetryUntilSuccessPolicy
	// RunOncePolicy will only allow the a single Run to take place regardless of success/failure
	RunOncePolicy
)

func (RunPolicy) String added in v1.1.2

func (r RunPolicy) String() string

type ServiceContext

type ServiceContext struct {
	Name string
	Log  *slog.Logger

	ShutdownCtx context.Context
	// contains filtered or unexported fields
}

ServiceContext all services will require a config as a *ServiceContext in their service struct. This config contains preconfigured shutdown channel,

func NewService

func NewService(name string, service Service, opts *serviceOpts) *ServiceContext

NewServiceContext creates a new service context instance given a name, service, and service options.

type ServiceOption

type ServiceOption func(*serviceOpts)

ServiceOption are simply using an Option pattern to customize options such as restart policies, timeouts for a given service and how it should run.

func UsingContext added in v1.1.2

func UsingContext(parent context.Context) ServiceOption

UsingContext creates a new context using the given context as its parent context

func UsingLogHandler added in v1.1.2

func UsingLogHandler(h slog.Handler) ServiceOption

UsingLogHandler applies a given slog Handler to the underlying service options

func UsingRunPolicy

func UsingRunPolicy(policy RunPolicy) ServiceOption

UsingRunPolicy applies a given policy to the ServiceOption instance

type ServiceResponse

type ServiceResponse struct {
	Error     error
	NextState State
}

ServiceResponse is used as a return response for services to use so the Manager can determine the "next state" based on return and any error that needs to be broadcasted up the logging channel

func NewResponse

func NewResponse(err error, nextState State) ServiceResponse

NewResponse will create a new ServiceResponse given an error and next state.

type State

type State int

State is used to determine the "next state" the service should enter when the current state has completed/errored returned. State should reflect different states that the interface can enter.

const (
	// InitState is in the ServiceResponse to inform manager to move us to the Init state (Initial Default).
	InitState State = iota
	// IdleState is in the ServiceResponse to inform manager to move us to the Idle state
	IdleState
	// RunState is in the ServiceResponse to inform manager to move us to the Run state
	RunState
	// StopState is in the ServiceResponse to inform manager to move us to the Stop state
	StopState
	// ExitState is in the ServiceResponse to inform manager to act as the final response type for Stop.
	ExitState
)

func (State) String added in v1.1.2

func (s State) String() string

type StateUpdate added in v1.0.0

type StateUpdate struct {
	Name  string
	State State
}

StateUpdate reflects any given update of lifecycle state at a given time.

type States added in v1.0.0

type States map[string]State

States is a map of service name to service state which reflects the service name and its lifecycle state.

Directories

Path Synopsis
examples
multi_service command

Jump to

Keyboard shortcuts

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