-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.go
138 lines (121 loc) · 3.04 KB
/
task.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package nginx
import (
"context"
"errors"
"os"
"sync"
"syscall"
"time"
// Packages
server "github.com/mutablelogic/go-server"
)
///////////////////////////////////////////////////////////////////////////////
// TYPES
// Check interfaces are satisfied
var _ server.Task = (*nginx)(nil)
/////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
// Return the label
func (task *nginx) Label() string {
// TODO
return defaultName
}
// Run the http server until the context is cancelled
func (task *nginx) Run(ctx context.Context) error {
var wg sync.WaitGroup
var result error
// TODO Remove the temporary directies (Config,Data) if they were created
defer func() {
var result error
for _, path := range task.deletePaths {
if _, err := os.Stat(path); err == nil {
task.log(ctx, "Removing temporary path: "+path)
if err := os.RemoveAll(path); err != nil {
result = errors.Join(result, err)
}
}
}
if result != nil {
task.log(ctx, result.Error())
}
}()
// We need to copy the configuration files to the temporary directory
// then reload the folder configuration
if err := fsCopyTo(task.configPath); err != nil {
return err
} else if err := task.folders.Reload(); err != nil {
return err
}
// Get the version of nginx
if version, err := task.getVersion(); err != nil {
return err
} else {
task.version = version
}
// Add stdout, stderr for the nginx commands
fn := func(data []byte) {
task.log(ctx, string(data))
}
task.run.Out, task.run.Err = fn, fn
task.test.Out, task.test.Err = fn, fn
// Run the nginx server in the background
wg.Add(1)
go func() {
defer wg.Done()
if err := task.run.Run(); err != nil {
result = errors.Join(result, err)
}
}()
// Runloop for nginx
timer := time.NewTicker(time.Second)
defer timer.Stop()
RUN_LOOP:
for {
select {
case <-ctx.Done():
break RUN_LOOP
case <-timer.C:
if task.run.Exited() {
break RUN_LOOP
}
}
}
// Perform shutdown, escalating signals
checkTicker := time.NewTicker(500 * time.Millisecond)
signalTicker := time.NewTimer(100 * time.Millisecond)
termSignal := syscall.SIGQUIT
defer checkTicker.Stop()
defer signalTicker.Stop()
FOR_LOOP:
for {
select {
case <-checkTicker.C:
if task.run.Exited() {
break FOR_LOOP
}
case <-signalTicker.C:
if !task.run.Exited() {
if err := task.run.Signal(termSignal); err != nil {
result = errors.Join(result, err)
}
}
switch termSignal {
case syscall.SIGQUIT:
task.log(ctx, "Sending graceful shutdown signal")
termSignal = syscall.SIGTERM
signalTicker.Reset(20 * time.Second) // Escalate after 20 seconds
case syscall.SIGTERM:
task.log(ctx, "Sending immediate shutdown signal")
termSignal = syscall.SIGKILL
signalTicker.Reset(5 * time.Second) // Escalate after 5 seconds
case syscall.SIGKILL:
task.log(ctx, "Sending kill signal")
signalTicker.Reset(5 * time.Second) // Continue sending every 5 seconds
}
}
}
// Wait for nginx to exit
wg.Wait()
// Return any errors
return result
}