TODO: 持久化战略

This commit is contained in:
huangsimin 2020-04-21 18:05:11 +08:00
parent 3263a297fe
commit a5a30391af
2 changed files with 68 additions and 0 deletions

View File

@ -95,9 +95,15 @@ func (hunter *Hunter) SetShare(key string, value interface{}) {
// Execute 执行任务
func (hunter *Hunter) Execute() {
if hunter.executes.Size() != 0 {
hunter.recursionTasks()
}
for _, task := range hunter.tasks {
hunter.execute(task)
}
}
// Execute 执行任务
@ -187,6 +193,23 @@ func (hunter *Hunter) AddTask(task ITask) {
hunter.tasks = append(hunter.tasks, task)
}
// SavePoint 持续
func (hunter *Hunter) savePoint() {
// f, err := os.OpenFile("./gob.log", os.O_CREATE|os.O_TRUNC|os.O_RDWR, os.ModePerm)
// if err != nil {
// panic(err)
// }
// enc := gob.NewEncoder(f)
// err = enc.Encode(hunter.executes.Values())
// if err != nil {
// panic(err)
// }
// for _, iv := range hunter.executes.Values() {
// tc := iv.(*TaskContext)
// }
}
// Execute 执行
// func (hunter *Hunter) Execute() {
// if itask, ok := hunter.task.Children().Top(); ok {

View File

@ -99,3 +99,48 @@ func TestCaseWebSub(t *testing.T) {
}
}
type WebSavePoint struct {
PrePostUrl
}
func (web *WebSavePoint) Execute(cxt *TaskContext) {
wf := cxt.Workflow()
wf.SetBodyAuto(`{"a": "1","url":["http://httpbin.org/post","http://httpbin.org/get"]}`)
resp, err := wf.Execute()
if err != nil {
panic(err)
}
cxt.SetShare("test", resp.Content())
data := make(map[string]interface{})
json.Unmarshal(resp.Content(), &data)
if urlList, ok := data["json"].(map[string]interface{})["url"].([]interface{}); ok {
for _, is := range urlList {
s := is.(string)
cxt.AddTask(&WebSavePoint1{PrePostUrl(s)})
}
}
}
type WebSavePoint1 struct {
PrePostUrl
}
func (web *WebSavePoint1) Execute(cxt *TaskContext) {
cxt.SetShare("test", cxt.Path()+"/"+cxt.TaskID())
cxt.hunter.savePoint()
}
func TestSavePoint(t *testing.T) {
hunter := NewHunter()
hunter.AddTask(&WebSavePoint{"http://httpbin.org/post"})
hunter.Execute()
content := hunter.GetShare("test").(string)
if content != "/0/1" {
t.Error(content)
}
}