-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
45 lines (37 loc) · 1.44 KB
/
service.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
37
38
39
40
41
42
43
44
45
package appetizer
import (
"context"
"github.com/homier/appetizer/log"
"github.com/homier/appetizer/retry"
)
// Service logic.
// No explicit `Stop` method is required, so if you want to gracefully stop your service,
// consider handling context cancellation.
//
//go:generate mockery --name Servicer
type Servicer interface {
// An initial stage for every service lifecycle.
// It could be possible called more than once, so
// you need to decide by yourself, whether you'll support this or not.
Init(log log.Logger) error
// Run your logic here.
// If this method returns `nil`, a service is considered stopped,
// it won't be restarted event if the `Service.RestartEnabled` is true.
// If this method returns some kind of error, a service is considered failed,
// and it'll be restarted depending on the `Service.RestartEnabled` and
// `Service.RestartOpts` policy.
Run(ctx context.Context) error
}
// Service descriptor. Here you describe your service and specify the target `Servicer`.
type Service struct {
// Service name. This name will be used as the "service" field value in logger.
Name string
// Servicer value. Actual logic for the service.
Servicer Servicer
// Whether to restart failed service or not.
RestartEnabled bool
// If `RestartEnabled` is `true`, this must be defined to describe
// a restart policy you need.
// NOTE: `RestartOpts.Opts` must be defined, otherwise service won't be restarted.
RestartOpts retry.Opts
}