Documentation ¶
Overview ¶
Package runner provides an interface to define the lifecycle of service like components and a function to manage them.
Brief ¶
This library provides a way to manage the lifecycle of service like components.
Usage
$ go get -u github.com/adzr/runner
Then, import the package:
import ( "github.com/adzr/runner" )
Finally, just implement the Runnable interface and pass it to the Run() function.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Run ¶
Run receives Runnable implementations and a health check time interval then and calls the Start() method of each Runnable in a separate go-routine and then the Check() method of each in a separate go-routine periodically based on the time interval received, and then the function blocks waiting for one of the system signals (syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT). If a signal is received then it doesn't call Runnable.Check() anymore and calls the Stop() method of each Runnable in a separate go-routine and wait them all to finish and finally returns.
Types ¶
type Runnable ¶
type Runnable interface { // Start is being called in a separate go-routine as soon as this object // is passed to the Run() function to switch the implementation // to "running" mode. // It must return an error if it fails to start for any reason, nil otherwise. Start() error // Stop is being called in a separate go-routine once one of the termination // signals (syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT) has been sent // to the application. // It must return an error if it fails to stop for any reason, nil otherwise. Stop() error // Check is being called in a separate go-routine after all the "Runnable"s // have started, this is called periodically every certain time duration // passed as a parameter to the Run() function, the periodic call stops when // Stop() method is called. // It must return an error if the check fails for any reason, nil otherwise. Check() error // OnError handles any error returned by the other methods. OnError(err error) }
Runnable represents anything that can be run in a separate go-routine, this is an ideal interface for a service type components that can run concurrently in the same application.