-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.go
115 lines (89 loc) · 2.02 KB
/
application.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package procyon
import (
"codnect.io/procyon/component/filter"
"codnect.io/procyon/runtime"
"codnect.io/procyon/web"
"os"
"os/signal"
goruntime "runtime"
"syscall"
"time"
)
type Application struct {
}
func New() *Application {
return &Application{}
}
func (a *Application) Run(args ...string) error {
startTime := time.Now()
banner, err := resolveBanner()
if err != nil {
return err
}
err = banner.PrintBanner(os.Stdout)
if err != nil {
return err
}
var arguments *runtime.Arguments
arguments, err = runtime.ParseArguments(args)
if err != nil {
return err
}
log.Info("Starting application using Go {} ({}/{})", goruntime.Version()[2:], goruntime.GOOS, goruntime.GOARCH)
log.Info("Running with Procyon {}", Version)
ctx := createContext(arguments)
err = ctx.Start()
if err != nil {
return err
}
timeTakenToStartup := time.Now().Sub(startTime)
log.Info("Started application in {} seconds", timeTakenToStartup.Seconds())
if err != nil {
return err
}
err = callCommandLineRunners(ctx, arguments)
if err != nil {
return err
}
if isServerApplication(ctx) {
waitForShutdown(ctx)
}
if ctx.IsRunning() {
return ctx.Stop()
}
return nil
}
func callCommandLineRunners(ctx runtime.Context, args *runtime.Arguments) error {
runners := ctx.Container().ListObjects(ctx, filter.ByTypeOf[runtime.CommandLineRunner]())
for _, runner := range runners {
cmdRunner := runner.(runtime.CommandLineRunner)
err := cmdRunner.Run(ctx, args)
if err != nil {
return err
}
}
return nil
}
func isServerApplication(ctx runtime.Context) bool {
container := ctx.Container()
servers := container.ListObjects(ctx, filter.ByTypeOf[web.Server]())
return len(servers) != 0
}
func waitForShutdown(ctx runtime.Context) {
shutdownChannel := make(chan os.Signal, 1)
signal.Notify(shutdownChannel, syscall.SIGINT, syscall.SIGTERM)
shutdown := false
for {
select {
case <-shutdownChannel:
shutdown = true
break
case <-ctx.Done():
shutdown = true
break
}
if shutdown {
break
}
}
}