Skip to content

Commit

Permalink
Add readiness health check
Browse files Browse the repository at this point in the history
  • Loading branch information
sneal committed May 16, 2024
1 parent 960ebce commit 1b60395
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 9 deletions.
16 changes: 14 additions & 2 deletions client/process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func TestProcesses(t *testing.T) {
},
},
{
Description: "Update a process",
Description: "Update a process health and readiness check",
Route: testutil.MockRoute{
Method: "PATCH",
Endpoint: "/v3/processes/ec4ff362-60c5-47a0-8246-2a134537c606",
Expand All @@ -124,6 +124,14 @@ func TestProcesses(t *testing.T) {
"interval": 10,
"endpoint": "/health"
}
},
"readiness_health_check": {
"type": "http",
"data": {
"invocation_timeout": 15,
"interval": 30,
"endpoint": "/ready"
}
}
}`,
},
Expand All @@ -135,7 +143,11 @@ func TestProcesses(t *testing.T) {
WithHealthCheckTimeout(60).
WithHealthCheckInterval(10).
WithHealthCheckInvocationTimeout(5).
WithHealthCheckEndpoint("/health")
WithHealthCheckEndpoint("/health").
WithReadinessCheckType("http").
WithReadinessCheckInterval(30).
WithReadinessCheckInvocationTimeout(15).
WithReadinessCheckEndpoint("/ready")
return c.Processes.Update(context.Background(), "ec4ff362-60c5-47a0-8246-2a134537c606", r)
},
},
Expand Down
65 changes: 58 additions & 7 deletions resource/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ type Process struct {
// The log rate in bytes per second allocated per instance
LogRateLimitInBytesPerSecond int `json:"log_rate_limit_in_bytes_per_second"`

HealthCheck ProcessHealthCheck `json:"health_check"`
Relationships ProcessRelationships `json:"relationships"`
HealthCheck ProcessHealthCheck `json:"health_check"`
ReadinessCheck ProcessReadinessCheck `json:"readiness_health_check"`
Relationships ProcessRelationships `json:"relationships"`

Metadata *Metadata `json:"metadata"`
Resource `json:",inline"`
Expand All @@ -36,8 +37,9 @@ type ProcessList struct {
type ProcessUpdate struct {
Command *string `json:"command"`

HealthCheck *ProcessHealthCheck `json:"health_check,omitempty"`
Metadata *Metadata `json:"metadata,omitempty"`
HealthCheck *ProcessHealthCheck `json:"health_check,omitempty"`
ReadinessCheck *ProcessReadinessCheck `json:"readiness_health_check,omitempty"`
Metadata *Metadata `json:"metadata,omitempty"`
}

type ProcessStats struct {
Expand Down Expand Up @@ -80,11 +82,11 @@ type ProcessScale struct {

type ProcessHealthCheck struct {
// The type of health check to perform; valid values are http, port, and process; default is port
Type string `json:"type"`
Data ProcessData `json:"data"`
Type string `json:"type"`
Data ProcessHealthCheckData `json:"data"`
}

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

Expand All @@ -98,6 +100,23 @@ type ProcessData struct {
Endpoint *string `json:"endpoint,omitempty"`
}

type ProcessReadinessCheck struct {
// The type of health check to perform; valid values are http, port, and process; default is process
Type string `json:"type"`
Data ProcessReadinessCheckData `json:"data"`
}

type ProcessReadinessCheckData struct {
// The timeout in seconds for individual readiness check requests for http and port health checks
InvocationTimeout *int `json:"invocation_timeout,omitempty"`

// The interval in seconds between readiness check requests
Interval *int `json:"interval,omitempty"`

// The endpoint called to determine if the app is ready; this key is only present for http readiness checks
Endpoint *string `json:"endpoint,omitempty"`
}

type ProcessRelationships struct {
App ToOneRelationship `json:"app"` // The app the process belongs to
Revision ToOneRelationship `json:"revision"` // The app revision the process is currently running
Expand Down Expand Up @@ -182,3 +201,35 @@ func (p *ProcessUpdate) WithHealthCheckEndpoint(endpoint string) *ProcessUpdate
p.HealthCheck.Data.Endpoint = &endpoint
return p
}

func (p *ProcessUpdate) WithReadinessCheckType(hcType string) *ProcessUpdate {
if p.ReadinessCheck == nil {
p.ReadinessCheck = &ProcessReadinessCheck{}
}
p.ReadinessCheck.Type = hcType
return p
}

func (p *ProcessUpdate) WithReadinessCheckInvocationTimeout(timeout int) *ProcessUpdate {
if p.ReadinessCheck == nil {
p.ReadinessCheck = &ProcessReadinessCheck{}
}
p.ReadinessCheck.Data.InvocationTimeout = &timeout
return p
}

func (p *ProcessUpdate) WithReadinessCheckInterval(interval int) *ProcessUpdate {
if p.ReadinessCheck == nil {
p.ReadinessCheck = &ProcessReadinessCheck{}
}
p.ReadinessCheck.Data.Interval = &interval
return p
}

func (p *ProcessUpdate) WithReadinessCheckEndpoint(endpoint string) *ProcessUpdate {
if p.ReadinessCheck == nil {
p.ReadinessCheck = &ProcessReadinessCheck{}
}
p.ReadinessCheck.Data.Endpoint = &endpoint
return p
}
8 changes: 8 additions & 0 deletions testutil/template/process.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@
"endpoint": "/health"
}
},
"readiness_health_check": {
"type": "http",
"data": {
"invocation_timeout": 15,
"interval": 30,
"endpoint": "/ready"
}
},
"relationships": {
"app": {
"data": {
Expand Down

0 comments on commit 1b60395

Please sign in to comment.