Resource/Service pattern for Go applications.
serviceStarter := rscsrv.NewServiceStarter(
&rscsrv.ColorServiceReporter{}, // First, the reporter
&Service1, &Service2, &Service3, // Here all services that should be started.
)
err := serviceStarter.Start()
if err != nil {
serviceStarter.Stop(true)
}
// ... Service1, Service2 and Service3 were startedSee /examples for more usage examples.
The Service is a interface that only provides:
- Name():
string;
But, it is the base of this library. Along side Service you must implement
Startable, StartableWithContext and/or Stoppable.
Startable represent services that its start process cannot be cancelled.
- Start():
error
StartableWithContext provides the functionality for services that can have its start process aborted.
- StartWithContext(
context.Context)error;
The StartRetrier is a mechanism that retries starting a Service when it
fails. No special Service implementation is required. The StartRetrier is a
proxy that wraps the real Service.
The NewStartRetrier wraps the Service providing the desired repeatability.
Example:
serviceStarter := rscsrv.NewServiceStarter(
&rscsrv.ColorServiceReporter{}, // First, the reporter
rscsrv.NewStartRetrier(&Service1, rscsrv.StartRetrierOptions{
MaxTries: 5,
DelayBetweenTries: time.Second * 5,
Timeout: time.Second * 60,
}), &Service2, &Service3, // Here all services that should be started.
)
err := serviceStarter.Start()
if err != nil {
serviceStarter.Stop(true)
}In this example, the Service1 is wrapped by a StartRetrier. The retrier will
keep trying to start Service1 until it reaches 5 failures. Between each try,
the retrier will wait 5 seconds before try again.
The way StartRetrier was designed is for opt-in, so when the library gets
updated, the behaviour do not change. So a helper was designed to
Retriers will apply the StartRetrier to many services at once:
retriers := rscsrv.Retriers(rscsrv.StartRetrierOptions{
MaxTries: 5,
DelayBetweenTries: time.Second,
Timeout: time.Second * 60,
})
serviceStarter := rscsrv.NewServiceStarter(
&rscsrv.ColorServiceReporter{}, // First, the reporter
retriers(&Service1, &Service2, &Service3)...,
)
err := serviceStarter.Start()
if err != nil {
serviceStarter.Stop(true)
}