-
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: initial support for healthchecks.io notifications (#480)
- Loading branch information
Showing
14 changed files
with
410 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package types | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"reflect" | ||
|
||
v1 "github.com/garethgeorge/backrest/gen/go/v1" | ||
"github.com/garethgeorge/backrest/internal/hook/hookutil" | ||
"github.com/garethgeorge/backrest/internal/orchestrator/tasks" | ||
"github.com/garethgeorge/backrest/internal/protoutil" | ||
"go.uber.org/zap" | ||
) | ||
|
||
type healthchecksHandler struct{} | ||
|
||
func (healthchecksHandler) Name() string { | ||
return "healthchecks" | ||
} | ||
|
||
func (healthchecksHandler) Execute(ctx context.Context, cmd *v1.Hook, vars interface{}, runner tasks.TaskRunner, event v1.Hook_Condition) error { | ||
payload, err := hookutil.RenderTemplateOrDefault(cmd.GetActionHealthchecks().GetTemplate(), hookutil.DefaultTemplate, vars) | ||
if err != nil { | ||
return fmt.Errorf("template rendering: %w", err) | ||
} | ||
|
||
l := runner.Logger(ctx) | ||
l.Sugar().Infof("Sending healthchecks message to %s", cmd.GetActionHealthchecks().GetWebhookUrl()) | ||
l.Debug("Sending healthchecks message", zap.String("payload", payload)) | ||
|
||
PingUrl := cmd.GetActionHealthchecks().GetWebhookUrl() | ||
|
||
// Send a "start" signal to healthchecks.io when the hook is starting. | ||
if protoutil.IsStartCondition(event) { | ||
PingUrl += "/start" | ||
} | ||
|
||
// Send a "fail" signal to healthchecks.io when the hook is failing. | ||
if protoutil.IsErrorCondition(event) { | ||
PingUrl += "/fail" | ||
} | ||
|
||
// Send a "log" signal to healthchecks.io when the hook is ending. | ||
if protoutil.IsLogCondition(event) { | ||
PingUrl += "/log" | ||
} | ||
|
||
type Message struct { | ||
Text string `json:"text"` | ||
} | ||
|
||
request := Message{ | ||
Text: payload, | ||
} | ||
|
||
requestBytes, _ := json.Marshal(request) | ||
|
||
body, err := hookutil.PostRequest(PingUrl, "application/json", bytes.NewReader(requestBytes)) | ||
if err != nil { | ||
return fmt.Errorf("sending healthchecks message to %q: %w", PingUrl, err) | ||
} | ||
|
||
l.Debug("Healthchecks response", zap.String("body", body)) | ||
return nil | ||
} | ||
|
||
func (healthchecksHandler) ActionType() reflect.Type { | ||
return reflect.TypeOf(&v1.Hook_ActionHealthchecks{}) | ||
} | ||
|
||
func init() { | ||
DefaultRegistry().RegisterHandler(&healthchecksHandler{}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package protoutil | ||
|
||
import ( | ||
v1 "github.com/garethgeorge/backrest/gen/go/v1" | ||
) | ||
|
||
var startConditionsMap = map[v1.Hook_Condition]bool{ | ||
v1.Hook_CONDITION_CHECK_START: true, | ||
v1.Hook_CONDITION_PRUNE_START: true, | ||
v1.Hook_CONDITION_SNAPSHOT_START: true, | ||
} | ||
|
||
var errorConditionsMap = map[v1.Hook_Condition]bool{ | ||
v1.Hook_CONDITION_ANY_ERROR: true, | ||
v1.Hook_CONDITION_CHECK_ERROR: true, | ||
v1.Hook_CONDITION_PRUNE_ERROR: true, | ||
v1.Hook_CONDITION_SNAPSHOT_ERROR: true, | ||
v1.Hook_CONDITION_UNKNOWN: true, | ||
} | ||
|
||
var logConditionsMap = map[v1.Hook_Condition]bool{ | ||
v1.Hook_CONDITION_SNAPSHOT_END: true, | ||
} | ||
|
||
var successConditionsMap = map[v1.Hook_Condition]bool{ | ||
v1.Hook_CONDITION_CHECK_SUCCESS: true, | ||
v1.Hook_CONDITION_PRUNE_SUCCESS: true, | ||
v1.Hook_CONDITION_SNAPSHOT_SUCCESS: true, | ||
} | ||
|
||
// IsErrorCondition returns true if the event is an error condition. | ||
func IsErrorCondition(event v1.Hook_Condition) bool { | ||
return errorConditionsMap[event] | ||
} | ||
|
||
// IsLogCondition returns true if the event is a log condition. | ||
func IsLogCondition(event v1.Hook_Condition) bool { | ||
return logConditionsMap[event] | ||
} | ||
|
||
// IsStartCondition returns true if the event is a start condition. | ||
func IsStartCondition(event v1.Hook_Condition) bool { | ||
return startConditionsMap[event] | ||
} | ||
|
||
// IsSuccessCondition returns true if the event is a success condition. | ||
func IsSuccessCondition(event v1.Hook_Condition) bool { | ||
return successConditionsMap[event] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters