forked from Shopify/ghostferry
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patherror_handler.go
More file actions
109 lines (94 loc) · 3 KB
/
Copy patherror_handler.go
File metadata and controls
109 lines (94 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package ghostferry
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"sync/atomic"
"github.com/sirupsen/logrus"
)
type ErrorHandler interface {
// Usually called from Fatal. When called from Fatal, if this method returns
// true, Fatal should panic, otherwise it should not.
ReportError(from string, err error)
Fatal(from string, err error)
}
type PanicErrorHandler struct {
Ferry *Ferry
ErrorCallback HTTPCallback
DumpState bool
DumpStateFilename string
errorCount int32
}
func (this *PanicErrorHandler) ReportError(from string, err error) {
logger := logrus.WithField("tag", "error_handler")
stateFilename := this.DumpStateFilename
stateJSON, jsonErr := this.Ferry.SerializeStateToJSON()
if jsonErr != nil {
logger.WithError(jsonErr).Error("failed to dump state to JSON...")
} else if this.DumpState {
if stateFilename != "" {
logger.Infof("writing state to %s", stateFilename)
writeErr := ioutil.WriteFile(stateFilename, []byte(stateJSON), 0640)
if writeErr != nil {
logger.WithError(writeErr).Errorf("failed to write state to %s. Dumping to stdout", stateFilename)
// if the write to file failed, write to stdout as last resort
// so the data is not lost entirely
stateFilename = ""
}
}
if stateFilename == "" {
logger.Info("writing state to stdout")
fmt.Fprintln(os.Stdout, stateJSON)
}
}
if this.Ferry.StateTracker != nil {
logger.Debug("storing state to target DB...")
dbErr := this.Ferry.StateTracker.SerializeToDB(this.Ferry.TargetDB)
if dbErr != nil {
logger.WithError(dbErr).Error("failed to store state to target DB...")
} else {
logger.Info("stored state to target DB")
}
}
// Invoke ErrorCallback if defined
if this.ErrorCallback != (HTTPCallback{}) {
client := &http.Client{}
errorData := make(map[string]string)
errorData["ErrFrom"] = from
errorData["ErrMessage"] = err.Error()
errorData["StateDump"] = stateJSON
errorDataBytes, jsonErr := json.MarshalIndent(errorData, "", " ")
if jsonErr != nil {
logger.WithField("error", jsonErr).Errorf("ghostferry failed to marshal error data")
} else {
this.ErrorCallback.Payload = string(errorDataBytes)
postErr := this.ErrorCallback.Post(client)
if postErr != nil {
logger.WithField("error", postErr).Errorf("ghostferry failed to notify error")
}
}
}
errmsg := "fatal error detected"
if this.DumpState {
errmsg += ", state dump "
if jsonErr != nil {
errmsg += "missing"
} else if stateFilename == "" {
errmsg += "in <stdout>"
} else {
errmsg += "written to " + stateFilename
}
}
// Print error to STDERR
logger.WithError(err).WithField("errfrom", from).Error(errmsg)
}
func (this *PanicErrorHandler) Fatal(from string, err error) {
if atomic.AddInt32(&this.errorCount, 1) > 1 {
logrus.WithField("tag", "error_handler").WithError(err).WithField("errfrom", from).Error("multiple fatal errors detected, not reporting again")
return
}
this.ReportError(from, err)
panic("fatal error detected, see logs for details")
}