-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
83 lines (70 loc) · 2.2 KB
/
main.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main
import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
"github.com/ONSdigital/dp-search-data-importer/config"
"github.com/ONSdigital/dp-search-data-importer/service"
"github.com/ONSdigital/log.go/v2/log"
)
const serviceName = "dp-search-data-importer"
var (
// BuildTime represents the time in which the service was built
BuildTime string
// GitCommit represents the commit (SHA-1) hash of the service that is running
GitCommit string
// Version represents the version of the service that is running
Version string
)
func main() {
log.Namespace = serviceName
ctx := context.Background()
if err := run(ctx); err != nil {
log.Fatal(ctx, "fatal runtime error", err)
os.Exit(1)
}
}
func run(ctx context.Context) (err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("panic: %s", r)
fmt.Printf("\n%s\n", err)
}
}()
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
svcErrors := make(chan error, 1)
// Read config
cfg, err := config.Get()
if err != nil {
return fmt.Errorf("unable to retrieve service configuration: %w", err)
}
log.Info(ctx, "config on startup", log.Data{"config": cfg, "build_time": BuildTime, "git-commit": GitCommit})
// Make sure that context is cancelled when 'run' finishes its execution.
// Any remaining go-routine that was not terminated during svc.Close (graceful shutdown) will be terminated by ctx.Done()
var cancel context.CancelFunc
ctx, cancel = context.WithCancel(ctx)
defer cancel()
// Run the service
svc := service.New()
if err := svc.Init(ctx, cfg, BuildTime, GitCommit, Version); err != nil {
return fmt.Errorf("service int failed with error: %w", err)
}
if err := svc.Start(ctx, svcErrors); err != nil {
return fmt.Errorf("service start failed with error: %w", err)
}
// Blocks until an os interrupt or a fatal error occurs
select {
case err := <-svcErrors:
err = fmt.Errorf("service error received: %w", err)
if errClose := svc.Close(ctx); errClose != nil {
log.Error(ctx, "service close error during error handling", errClose)
}
return err
case sig := <-signals:
log.Info(ctx, "os signal received", log.Data{"signal": sig})
}
return svc.Close(ctx)
}