startstop

package
v0.0.10 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2023 License: MPL-2.0 Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BaseStartStop

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

BaseStartStop is a helper that can be embedded on a queue maintenance service and which will provide the basic necessities to safely implement the Service interface in a way that's not racy and can tolerate a number of edge cases. It's packaged separately so that it doesn't leak its internal variables into services that use it.

Services should implement their own Start function which invokes StartInit first thing, return if told not to start, spawn a goroutine with their main run block otherwise, and make sure to defer a close on the stop channel returned by StartInit within that goroutine.

A Stop implementation is provided automatically and it's not necessary to override it.

func (*BaseStartStop) StartInit

func (s *BaseStartStop) StartInit(ctx context.Context) (context.Context, bool, chan struct{})

StartInit should be invoked at the beginning of a service's Start function. It returns a context for the service to use, a boolean indicating whether it should start (which will be false if the service is already started), and a stopped channel. Services should defer a close on the stop channel in their main run loop.

ctx, shouldStart, stopped := s.StartInit(ctx)
if !shouldStart {
    return nil
}

go func() {
     defer close(stopped)

     ...

func (*BaseStartStop) Stop

func (s *BaseStartStop) Stop()

Stop is an automatically provided implementation for the maintenance Service interface's Stop.

func (*BaseStartStop) Stopped

func (s *BaseStartStop) Stopped() <-chan struct{}

Stopped returns a channel that can be waited on for the service to be stopped. This function is only safe to invoke after successfully waiting on a service's Start.

Jump to

Keyboard shortcuts

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