MCPcopy
hub / github.com/andeya/pholcus / flush

Method flush

app/aid/history/failure.go:51–108  ·  view source on GitHub ↗

flush clears historical failure records first, then updates.

(provider string)

Source from the content-addressed store, hash-verified

49
50// flush clears historical failure records first, then updates.
51func (f *Failure) flush(provider string) (r result.Result[int]) {
52 defer r.Catch()
53 f.RWMutex.Lock()
54 defer f.RWMutex.Unlock()
55 fLen := len(f.list)
56
57 switch provider {
58 case "mgo":
59 result.RetVoid(mgo.Error()).Unwrap()
60 mgo.Call(func(src pool.Src) error {
61 c := src.(*mgo.MgoSrc).DB(config.Conf().DBName).C(f.tabName)
62 c.DropCollection()
63 if fLen == 0 {
64 return nil
65 }
66 var docs = []interface{}{}
67 for key, req := range f.list {
68 docs = append(docs, map[string]interface{}{"_id": key, "failure": req.Serialize().Unwrap()})
69 }
70 c.Insert(docs...)
71 return nil
72 }).Unwrap()
73
74 case "mysql":
75 _, err := mysql.DB()
76 result.RetVoid(err).Unwrap()
77 table, ok := getWriteMysqlTable(f.tabName)
78 if !ok {
79 table = mysql.New().Unwrap()
80 table.SetTableName(f.tabName).CustomPrimaryKey(`id VARCHAR(255) NOT NULL PRIMARY KEY`).AddColumn(`failure MEDIUMTEXT`)
81 setWriteMysqlTable(f.tabName, table)
82 table.Create().Unwrap()
83 } else {
84 table.Truncate().Unwrap()
85 }
86 for key, req := range f.list {
87 table.AutoInsert([]string{key, req.Serialize().Unwrap()})
88 table.FlushInsert().Unwrap()
89 }
90
91 default:
92 os.Remove(f.fileName)
93 if fLen == 0 {
94 return result.Ok(0)
95 }
96 file, err := os.OpenFile(f.fileName, os.O_CREATE|os.O_WRONLY, 0777)
97 result.RetVoid(err).Unwrap()
98 docs := make(map[string]string, len(f.list))
99 for key, req := range f.list {
100 docs[key] = req.Serialize().Unwrap()
101 }
102 b, _ := json.Marshal(docs)
103 b = bytes.Replace(b, []byte(`\u0026`), []byte(`&`), -1)
104 file.Write(b)
105 file.Close()
106 }
107 return result.Ok(fLen)
108}

Callers 5

TestFailure_Flush_FileFunction · 0.95
FlushSuccessMethod · 0.45
FlushFailureMethod · 0.45

Calls 15

ErrorFunction · 0.92
CallFunction · 0.92
ConfFunction · 0.92
DBFunction · 0.92
NewFunction · 0.92
getWriteMysqlTableFunction · 0.85
setWriteMysqlTableFunction · 0.85
UnwrapMethod · 0.80
SerializeMethod · 0.80
AddColumnMethod · 0.80
CustomPrimaryKeyMethod · 0.80
SetTableNameMethod · 0.80

Tested by 3

TestFailure_Flush_FileFunction · 0.76