call error hooks on script failure

This commit is contained in:
Frederik Ring 2021-09-09 08:12:07 +02:00
parent 2c06f81503
commit e46968ed79

View File

@ -62,6 +62,7 @@ type script struct {
cli *client.Client cli *client.Client
mc *minio.Client mc *minio.Client
logger *logrus.Logger logger *logrus.Logger
errorHooks []errorHook
start time.Time start time.Time
file string file string
@ -70,6 +71,8 @@ type script struct {
c *config c *config
} }
type errorHook func(err error, start time.Time, logOutput string) error
type config struct { type config struct {
BackupSources string `split_words:"true" default:"/backup"` BackupSources string `split_words:"true" default:"/backup"`
BackupFilename string `split_words:"true" default:"backup-%Y-%m-%dT%H-%M-%S.tar.gz"` BackupFilename string `split_words:"true" default:"backup-%Y-%m-%dT%H-%M-%S.tar.gz"`
@ -473,9 +476,15 @@ func (s *script) pruneOldBackups() error {
} }
// must exits the script run non-zero and prematurely in case the given error // must exits the script run non-zero and prematurely in case the given error
// is non-nil. // is non-nil. If error hooks are present on the script object, they
// will be called, passing the failure and previous log output.
func (s *script) must(err error) { func (s *script) must(err error) {
if err != nil { if err != nil {
for _, hook := range s.errorHooks {
if hookErr := hook(err, s.start, s.output.String()); hookErr != nil {
s.logger.Errorf("An error occurred calling an error hook: %s", err)
}
}
s.logger.Fatalf("Fatal error running backup: %s", err) s.logger.Fatalf("Fatal error running backup: %s", err)
} }
} }