-
Notifications
You must be signed in to change notification settings - Fork 40
/
lifecycle.go
36 lines (33 loc) · 1.37 KB
/
lifecycle.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package lifecycle
import (
"context"
"os"
)
// Service provides abstract methods to control the lifecycle of a service
// Every service must implement Service interface.
type Service interface {
// Name describe service name
Name() string
// Start a service, this method should be used in non-block form
Start(ctx context.Context) error
// Stop a service, this method should be used in non-block form
Stop(ctx context.Context) error
}
// Lifecycle is an interface to describe how service is managed.
// The Lifecycle tracks the Service lifecycle, listens for signals from
// the process to ensure a graceful shutdown.
//
// All managed services must firstly call RegisterServices to register with Lifecycle.
type Lifecycle interface {
// RegisterServices registers service to ServiceLifecycle for managing.
RegisterServices(modular ...Service)
// StartServices starts all registered services by calling Service.Start method.
StartServices(ctx context.Context) Lifecycle
// StopServices stops all registered services by calling Service.Stop method.
StopServices(ctx context.Context)
// Signals listens the system signals for gracefully stop the registered services.
Signals(sigs ...os.Signal) Lifecycle
// Wait waits the signal for stopping the ServiceLifecycle, before stopping
// the ServiceLifecycle will call StopServices stops all registered services.
Wait(ctx context.Context)
}