feat: add completion webhook url
continuous-integration/drone/push Build is passing Details

This commit is contained in:
kolaente 2023-06-05 18:35:21 +02:00
parent e43e8c3959
commit 07c2fae209
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
4 changed files with 46 additions and 3 deletions

View File

@ -67,6 +67,11 @@ are only as many as this config variable.
Default: `12`
### `BACKUP_COMPLETION_WEBHOOK_URL`
If provided, the tool will do an empty GET request to this URL to indicate it successfully completed the backup job.
You can use this with other tools to monitor if backups are completed as they should.
## Building from source
This project uses go modules, so you'll need at least go 1.11 to compile it.

View File

@ -17,6 +17,7 @@ type conf struct {
fullCurrentBackupPath string
Schedule string
MaxBackups int
CompletionWebhookURL string
}
var (
@ -25,9 +26,10 @@ var (
)
const (
envBackupFolder = `BACKUP_FOLDER`
envSchedule = `BACKUP_SCHEDULE`
envMax = `BACKUP_MAX`
envBackupFolder = `BACKUP_FOLDER`
envSchedule = `BACKUP_SCHEDULE`
envMax = `BACKUP_MAX`
envCompletionWebhookURL = `BACKUP_COMPLETION_WEBHOOK_URL`
)
func init() {
@ -60,6 +62,11 @@ func init() {
config.MaxBackups = int(maxBackups)
}
webhookURL, has := os.LookupEnv(envCompletionWebhookURL)
if has {
config.CompletionWebhookURL = webhookURL
}
updateFullBackupPath()
}

View File

@ -32,6 +32,11 @@ func main() {
log.Fatalf("Could not dump databases: %s", err)
}
err = callWebhook()
if err != nil {
log.Fatalf("Could not call completion webhook: %s", err)
}
log.Println("Done.")
})
if err != nil {

26
webhook.go Normal file
View File

@ -0,0 +1,26 @@
package main
import (
"bytes"
"fmt"
"net/http"
)
func callWebhook() error {
if config.CompletionWebhookURL == "" {
return nil
}
res, err := http.Get(config.CompletionWebhookURL)
if err != nil {
return err
}
if res.StatusCode > 399 {
buf := bytes.Buffer{}
_, _ = buf.ReadFrom(res.Body)
return fmt.Errorf("recived an error status code while calling the webhook: %d, message: %s", res.StatusCode, buf.String())
}
return nil
}