@@ -32,30 +32,31 @@ func (r *Runtime) HealthCheck(name string) (define.HealthCheckStatus, error) {
32
32
}
33
33
34
34
hcStatus , err := checkHealthCheckCanBeRun (container )
35
- if err == nil {
36
- hcStatus , err := container .runHealthCheck ()
37
- if err := container .processHealthCheckStatus (hcStatus ); err != nil {
38
- return hcStatus , err
39
- }
35
+ if err != nil {
36
+ return hcStatus , err
37
+ }
38
+
39
+ hcStatus , logStatus , err := container .runHealthCheck ()
40
+ if err := container .processHealthCheckStatus (logStatus ); err != nil {
40
41
return hcStatus , err
41
42
}
42
43
return hcStatus , err
43
44
}
44
45
45
46
// runHealthCheck runs the health check as defined by the container
46
- func (c * Container ) runHealthCheck () (define.HealthCheckStatus , error ) {
47
+ func (c * Container ) runHealthCheck () (define.HealthCheckStatus , string , error ) {
47
48
var (
48
49
newCommand []string
49
50
returnCode int
50
51
inStartPeriod bool
51
52
)
52
53
hcCommand := c .HealthCheckConfig ().Test
53
54
if len (hcCommand ) < 1 {
54
- return define .HealthCheckNotDefined , fmt .Errorf ("container %s has no defined healthcheck" , c .ID ())
55
+ return define .HealthCheckNotDefined , "" , fmt .Errorf ("container %s has no defined healthcheck" , c .ID ())
55
56
}
56
57
switch hcCommand [0 ] {
57
58
case "" , define .HealthConfigTestNone :
58
- return define .HealthCheckNotDefined , fmt .Errorf ("container %s has no defined healthcheck" , c .ID ())
59
+ return define .HealthCheckNotDefined , "" , fmt .Errorf ("container %s has no defined healthcheck" , c .ID ())
59
60
case define .HealthConfigTestCmd :
60
61
newCommand = hcCommand [1 :]
61
62
case define .HealthConfigTestCmdShell :
@@ -66,11 +67,11 @@ func (c *Container) runHealthCheck() (define.HealthCheckStatus, error) {
66
67
newCommand = hcCommand
67
68
}
68
69
if len (newCommand ) < 1 || newCommand [0 ] == "" {
69
- return define .HealthCheckNotDefined , fmt .Errorf ("container %s has no defined healthcheck" , c .ID ())
70
+ return define .HealthCheckNotDefined , "" , fmt .Errorf ("container %s has no defined healthcheck" , c .ID ())
70
71
}
71
72
rPipe , wPipe , err := os .Pipe ()
72
73
if err != nil {
73
- return define .HealthCheckInternalError , fmt .Errorf ("unable to create pipe for healthcheck session: %w" , err )
74
+ return define .HealthCheckInternalError , "" , fmt .Errorf ("unable to create pipe for healthcheck session: %w" , err )
74
75
}
75
76
defer wPipe .Close ()
76
77
defer rPipe .Close ()
@@ -135,15 +136,16 @@ func (c *Container) runHealthCheck() (define.HealthCheckStatus, error) {
135
136
}
136
137
137
138
hcl := newHealthCheckLog (timeStart , timeEnd , returnCode , eventLog )
138
- if err := c .updateHealthCheckLog (hcl , inStartPeriod ); err != nil {
139
- return hcResult , fmt .Errorf ("unable to update health check log %s for %s: %w" , c .healthCheckLogPath (), c .ID (), err )
139
+ logStatus , err := c .updateHealthCheckLog (hcl , inStartPeriod )
140
+ if err != nil {
141
+ return hcResult , "" , fmt .Errorf ("unable to update health check log %s for %s: %w" , c .healthCheckLogPath (), c .ID (), err )
140
142
}
141
143
142
- return hcResult , hcErr
144
+ return hcResult , logStatus , hcErr
143
145
}
144
146
145
- func (c * Container ) processHealthCheckStatus (status define. HealthCheckStatus ) error {
146
- if status == define .HealthCheckSuccess {
147
+ func (c * Container ) processHealthCheckStatus (status string ) error {
148
+ if status != define .HealthCheckUnhealthy {
147
149
return nil
148
150
}
149
151
@@ -211,10 +213,13 @@ func (c *Container) updateHealthStatus(status string) error {
211
213
}
212
214
213
215
// UpdateHealthCheckLog parses the health check results and writes the log
214
- func (c * Container ) updateHealthCheckLog (hcl define.HealthCheckLog , inStartPeriod bool ) error {
216
+ func (c * Container ) updateHealthCheckLog (hcl define.HealthCheckLog , inStartPeriod bool ) (string , error ) {
217
+ c .lock .Lock ()
218
+ defer c .lock .Unlock ()
219
+
215
220
healthCheck , err := c .getHealthCheckLog ()
216
221
if err != nil {
217
- return err
222
+ return "" , err
218
223
}
219
224
if hcl .ExitCode == 0 {
220
225
// set status to healthy, reset failing state to 0
@@ -239,9 +244,9 @@ func (c *Container) updateHealthCheckLog(hcl define.HealthCheckLog, inStartPerio
239
244
}
240
245
newResults , err := json .Marshal (healthCheck )
241
246
if err != nil {
242
- return fmt .Errorf ("unable to marshall healthchecks for writing: %w" , err )
247
+ return "" , fmt .Errorf ("unable to marshall healthchecks for writing: %w" , err )
243
248
}
244
- return os .WriteFile (c .healthCheckLogPath (), newResults , 0700 )
249
+ return healthCheck . Status , os .WriteFile (c .healthCheckLogPath (), newResults , 0700 )
245
250
}
246
251
247
252
// HealthCheckLogPath returns the path for where the health check log is
0 commit comments