mirror of
https://github.com/offen/docker-volume-backup.git
synced 2024-11-22 13:20:29 +01:00
allow registering hooks at different levels
This commit is contained in:
parent
59660ec5c7
commit
160a47e90b
@ -63,7 +63,7 @@ type script struct {
|
|||||||
cli *client.Client
|
cli *client.Client
|
||||||
mc *minio.Client
|
mc *minio.Client
|
||||||
logger *logrus.Logger
|
logger *logrus.Logger
|
||||||
errorHooks []errorHook
|
hooks []hook
|
||||||
|
|
||||||
start time.Time
|
start time.Time
|
||||||
file string
|
file string
|
||||||
@ -72,8 +72,6 @@ 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"`
|
||||||
@ -146,7 +144,7 @@ func newScript() (*script, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if s.c.EmailNotificationRecipient != "" {
|
if s.c.EmailNotificationRecipient != "" {
|
||||||
s.errorHooks = append(s.errorHooks, func(err error, start time.Time, logOutput string) error {
|
s.hooks = append(s.hooks, hook{hookLevelFailure, func(err error, start time.Time, logOutput string) error {
|
||||||
mailer := gomail.NewDialer(
|
mailer := gomail.NewDialer(
|
||||||
s.c.EmailSMTPHost, s.c.EmailSMTPPort, s.c.EmailSMTPUsername, s.c.EmailSMTPPassword,
|
s.c.EmailSMTPHost, s.c.EmailSMTPPort, s.c.EmailSMTPUsername, s.c.EmailSMTPPassword,
|
||||||
)
|
)
|
||||||
@ -164,7 +162,7 @@ func newScript() (*script, error) {
|
|||||||
message.SetHeader("Subject", subject)
|
message.SetHeader("Subject", subject)
|
||||||
message.SetBody("text/plain", body)
|
message.SetBody("text/plain", body)
|
||||||
return mailer.DialAndSend(message)
|
return mailer.DialAndSend(message)
|
||||||
})
|
}})
|
||||||
}
|
}
|
||||||
|
|
||||||
return s, nil
|
return s, nil
|
||||||
@ -504,16 +502,33 @@ func (s *script) pruneOldBackups() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// runHooks runs all hooks that have been registered using the
|
||||||
|
// given level. In case executing a hook returns an error, the following
|
||||||
|
// hooks will still be run before the function returns an error.
|
||||||
|
func (s *script) runHooks(err error, targetLevel string) error {
|
||||||
|
var actionErrors []error
|
||||||
|
for _, hook := range s.hooks {
|
||||||
|
if hook.level != targetLevel {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if err := hook.action(err, s.start, s.output.String()); err != nil {
|
||||||
|
actionErrors = append(actionErrors, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(actionErrors) != 0 {
|
||||||
|
return join(actionErrors...)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// 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. If error hooks are present on the script object, they
|
// is non-nil. If failure hooks have been registered on the script object, they
|
||||||
// will be called, passing the failure and previous log output.
|
// 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 {
|
||||||
s.logger.Errorf("Fatal error running backup: %s", err)
|
s.logger.Errorf("Fatal error running backup: %s", err)
|
||||||
for _, hook := range s.errorHooks {
|
if hookErr := s.runHooks(err, hookLevelFailure); hookErr != nil {
|
||||||
if hookErr := hook(err, s.start, s.output.String()); hookErr != nil {
|
s.logger.Errorf("An error occurred calling the registered failure hooks: %s", hookErr)
|
||||||
s.logger.Errorf("An error occurred calling an error hook: %s", hookErr)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
@ -588,3 +603,14 @@ func (b *bufferingWriter) Write(p []byte) (n int, err error) {
|
|||||||
}
|
}
|
||||||
return b.writer.Write(p)
|
return b.writer.Write(p)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// hook contains a queued action that can be trigger them when the script
|
||||||
|
// reaches a certain point (e.g. unsuccessful backup)
|
||||||
|
type hook struct {
|
||||||
|
level string
|
||||||
|
action func(err error, start time.Time, logOutput string) error
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
hookLevelFailure = "failure"
|
||||||
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user