Skip to content

Commit a5dc9be

Browse files
authored
Merge pull request #411 from cloudfoundry/readiness-checks
Add CF app readiness checks
2 parents a3e4aba + 1b60395 commit a5dc9be

File tree

3 files changed

+103
-12
lines changed

3 files changed

+103
-12
lines changed

client/process_test.go

+21-3
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func TestProcesses(t *testing.T) {
108108
},
109109
},
110110
{
111-
Description: "Update a process",
111+
Description: "Update a process health and readiness check",
112112
Route: testutil.MockRoute{
113113
Method: "PATCH",
114114
Endpoint: "/v3/processes/ec4ff362-60c5-47a0-8246-2a134537c606",
@@ -119,7 +119,18 @@ func TestProcesses(t *testing.T) {
119119
"health_check": {
120120
"type": "http",
121121
"data": {
122-
"timeout": 60
122+
"timeout": 60,
123+
"invocation_timeout": 5,
124+
"interval": 10,
125+
"endpoint": "/health"
126+
}
127+
},
128+
"readiness_health_check": {
129+
"type": "http",
130+
"data": {
131+
"invocation_timeout": 15,
132+
"interval": 30,
133+
"endpoint": "/ready"
123134
}
124135
}
125136
}`,
@@ -129,7 +140,14 @@ func TestProcesses(t *testing.T) {
129140
r := resource.NewProcessUpdate().
130141
WithCommand("rackup").
131142
WithHealthCheckType("http").
132-
WithHealthCheckTimeout(60)
143+
WithHealthCheckTimeout(60).
144+
WithHealthCheckInterval(10).
145+
WithHealthCheckInvocationTimeout(5).
146+
WithHealthCheckEndpoint("/health").
147+
WithReadinessCheckType("http").
148+
WithReadinessCheckInterval(30).
149+
WithReadinessCheckInvocationTimeout(15).
150+
WithReadinessCheckEndpoint("/ready")
133151
return c.Processes.Update(context.Background(), "ec4ff362-60c5-47a0-8246-2a134537c606", r)
134152
},
135153
},

resource/process.go

+69-7
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ type Process struct {
2121
// The log rate in bytes per second allocated per instance
2222
LogRateLimitInBytesPerSecond int `json:"log_rate_limit_in_bytes_per_second"`
2323

24-
HealthCheck ProcessHealthCheck `json:"health_check"`
25-
Relationships ProcessRelationships `json:"relationships"`
24+
HealthCheck ProcessHealthCheck `json:"health_check"`
25+
ReadinessCheck ProcessReadinessCheck `json:"readiness_health_check"`
26+
Relationships ProcessRelationships `json:"relationships"`
2627

2728
Metadata *Metadata `json:"metadata"`
2829
Resource `json:",inline"`
@@ -36,8 +37,9 @@ type ProcessList struct {
3637
type ProcessUpdate struct {
3738
Command *string `json:"command"`
3839

39-
HealthCheck *ProcessHealthCheck `json:"health_check,omitempty"`
40-
Metadata *Metadata `json:"metadata,omitempty"`
40+
HealthCheck *ProcessHealthCheck `json:"health_check,omitempty"`
41+
ReadinessCheck *ProcessReadinessCheck `json:"readiness_health_check,omitempty"`
42+
Metadata *Metadata `json:"metadata,omitempty"`
4143
}
4244

4345
type ProcessStats struct {
@@ -80,21 +82,41 @@ type ProcessScale struct {
8082

8183
type ProcessHealthCheck struct {
8284
// The type of health check to perform; valid values are http, port, and process; default is port
83-
Type string `json:"type"`
84-
Data ProcessData `json:"data"`
85+
Type string `json:"type"`
86+
Data ProcessHealthCheckData `json:"data"`
8587
}
8688

87-
type ProcessData struct {
89+
type ProcessHealthCheckData struct {
8890
// The duration in seconds that health checks can fail before the process is restarted
8991
Timeout *int `json:"timeout"`
9092

9193
// The timeout in seconds for individual health check requests for http and port health checks
9294
InvocationTimeout *int `json:"invocation_timeout,omitempty"`
9395

96+
// The interval in seconds between health check requests
97+
Interval *int `json:"interval,omitempty"`
98+
9499
// The endpoint called to determine if the app is healthy; this key is only present for http health check
95100
Endpoint *string `json:"endpoint,omitempty"`
96101
}
97102

103+
type ProcessReadinessCheck struct {
104+
// The type of health check to perform; valid values are http, port, and process; default is process
105+
Type string `json:"type"`
106+
Data ProcessReadinessCheckData `json:"data"`
107+
}
108+
109+
type ProcessReadinessCheckData struct {
110+
// The timeout in seconds for individual readiness check requests for http and port health checks
111+
InvocationTimeout *int `json:"invocation_timeout,omitempty"`
112+
113+
// The interval in seconds between readiness check requests
114+
Interval *int `json:"interval,omitempty"`
115+
116+
// The endpoint called to determine if the app is ready; this key is only present for http readiness checks
117+
Endpoint *string `json:"endpoint,omitempty"`
118+
}
119+
98120
type ProcessRelationships struct {
99121
App ToOneRelationship `json:"app"` // The app the process belongs to
100122
Revision ToOneRelationship `json:"revision"` // The app revision the process is currently running
@@ -164,10 +186,50 @@ func (p *ProcessUpdate) WithHealthCheckInvocationTimeout(timeout int) *ProcessUp
164186
return p
165187
}
166188

189+
func (p *ProcessUpdate) WithHealthCheckInterval(interval int) *ProcessUpdate {
190+
if p.HealthCheck == nil {
191+
p.HealthCheck = &ProcessHealthCheck{}
192+
}
193+
p.HealthCheck.Data.Interval = &interval
194+
return p
195+
}
196+
167197
func (p *ProcessUpdate) WithHealthCheckEndpoint(endpoint string) *ProcessUpdate {
168198
if p.HealthCheck == nil {
169199
p.HealthCheck = &ProcessHealthCheck{}
170200
}
171201
p.HealthCheck.Data.Endpoint = &endpoint
172202
return p
173203
}
204+
205+
func (p *ProcessUpdate) WithReadinessCheckType(hcType string) *ProcessUpdate {
206+
if p.ReadinessCheck == nil {
207+
p.ReadinessCheck = &ProcessReadinessCheck{}
208+
}
209+
p.ReadinessCheck.Type = hcType
210+
return p
211+
}
212+
213+
func (p *ProcessUpdate) WithReadinessCheckInvocationTimeout(timeout int) *ProcessUpdate {
214+
if p.ReadinessCheck == nil {
215+
p.ReadinessCheck = &ProcessReadinessCheck{}
216+
}
217+
p.ReadinessCheck.Data.InvocationTimeout = &timeout
218+
return p
219+
}
220+
221+
func (p *ProcessUpdate) WithReadinessCheckInterval(interval int) *ProcessUpdate {
222+
if p.ReadinessCheck == nil {
223+
p.ReadinessCheck = &ProcessReadinessCheck{}
224+
}
225+
p.ReadinessCheck.Data.Interval = &interval
226+
return p
227+
}
228+
229+
func (p *ProcessUpdate) WithReadinessCheckEndpoint(endpoint string) *ProcessUpdate {
230+
if p.ReadinessCheck == nil {
231+
p.ReadinessCheck = &ProcessReadinessCheck{}
232+
}
233+
p.ReadinessCheck.Data.Endpoint = &endpoint
234+
return p
235+
}

testutil/template/process.json

+13-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,20 @@
77
"disk_in_mb": 1024,
88
"log_rate_limit_in_bytes_per_second": 1024,
99
"health_check": {
10-
"type": "port",
10+
"type": "http",
1111
"data": {
12-
"timeout": null
12+
"timeout": 60,
13+
"invocation_timeout": 5,
14+
"interval": 10,
15+
"endpoint": "/health"
16+
}
17+
},
18+
"readiness_health_check": {
19+
"type": "http",
20+
"data": {
21+
"invocation_timeout": 15,
22+
"interval": 30,
23+
"endpoint": "/ready"
1324
}
1425
},
1526
"relationships": {

0 commit comments

Comments
 (0)