service

package
v0.1.7 Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2021 License: AGPL-3.0 Imports: 7 Imported by: 0

Documentation

Overview

Package service provides a framework to easily implement services for the application. Services are background components that execute in parallel with the handling of requests.

Index

Constants

This section is empty.

Variables

View Source
var (
	NothingToDoYet = errors.New("Nothing to do yet")
)

Functions

func SQLProcessOne

func SQLProcessOne(query string, id uint32) error

SQLProcessOne is a helper function to implement Service.ProcessOne. The given query is executed with id as parameter. If the query succeed but no row is affected, NothingToDoYet is returned.

Types

type AlarmInjector

type AlarmInjector = func(int, ...alarm.Option) alarm.Alarm

AlarmInjector is the injector of an alarm into services.

type EventReceiver

type EventReceiver interface {
	FilterEvent(events.Event) bool
	ReceiveEvent(events.Event, RunnerControler)
}

EventReceiver is the interface implemented by services willing to react to some events.

type Iterator

type Iterator interface {

	// Next goes to the next entry if it can, returning false otherwise.
	// Returning true guarantees that a call to IdAndDate will succeed.
	// Next must be called once before the first call to IdAndDate.
	Next() bool

	IdAndDate() (uint32, time.Time)
	Err() error
	Close() error
}

Iterator iterates on a list of Id and Date representing tasks for a service.

func IteratorFromRows

func IteratorFromRows(rows *sql.Rows) Iterator

IteratorFromRows constructs an Iterator from an *sql.Rows. Each rows must have exacly two cells: one that can be scanned as an uint32 and one that can be scanned as a time.Time.

func SQLCheckAll

func SQLCheckAll(query string) Iterator

SQLCheckAll is a helper function to implement Service.CheckAll. It executes the query and return an iterator from the returned rows. The query must return a list of task, each task consisting in an id and a date. See IteratorFromRows for details.

type RunnerControler

type RunnerControler interface {

	// Schedule asks the runner to schedule the object with the given id for being processed.
	Schedule(id uint32)

	// StopService asks the runner to stop the service as soon as possible.
	StopService()
}

RunnerControler allows to control the service runner from the service. It should be used only from EventReceiver.ReceiveEvent().

type Service

type Service interface {

	// ProcessOne performs the operation on the object with the given id.
	// If no operation has to be done on that object yet, ProcessOne must return NothingToDoYet.
	ProcessOne(id uint32) error

	// CheckAll returns a list of all objects on which the operation will have to be done.
	// The list must be sorted in ascending order on the date.
	// In case of error, Next() called on the returned iterator must return false and Error() must
	// return the error.
	CheckAll() Iterator

	// CheckOne returns the time at which the operation must be done on the object with the given id.
	// If no operation has to be done on that object, CheckOne must return zero time.Time.
	CheckOne(id uint32) time.Time

	// Interval returns the maximal duration between two calls to CheckAll.
	Interval() time.Duration

	Logger() slog.Leveled
}

Service is the interface for sercives ran by RunService. Such a service must perform some operations on objects identified by an uint32 value. Those operations must be performed at some time.

type StopFunction

type StopFunction func()

StopFunction must be called to cleanly stop a service.

func Run

func Run(service Service, alarmInjector AlarmInjector, evtManager events.Manager) StopFunction

RunService runs a service in the background.

All methods of the service are called from the same goroutine, wich is different from the goroutine RunService was run from. If the service implements the EventReceiver interface, the runner installs an AsyncForwarder on events.DefaultManager and calls EventReceiver.ReceiveEvent for each received event. The returned function must be called to stop the service and free the resources associated with the runner.

Jump to

Keyboard shortcuts

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