Jelajahi Sumber

上下架修改

maxiaoshan 3 tahun lalu
induk
melakukan
fd41ca5927
1 mengubah file dengan 53 tambahan dan 49 penghapusan
  1. 53 49
      src/spider/handler.go

+ 53 - 49
src/spider/handler.go

@@ -29,8 +29,8 @@ var Allspiders sync.Map = sync.Map{}
 var Allspiders2 sync.Map = sync.Map{}
 var LoopListPath sync.Map = sync.Map{}
 
-var ChanDels = map[int]string{}
-var lock sync.Mutex
+//var ChanDels = map[int]string{}
+//var lock sync.Mutex
 
 var CC chan *lua.LState
 var CC2 chan *lua.LState
@@ -42,16 +42,14 @@ var InitAllLuaOver = make(chan bool, 1) //所有脚本是否加载完毕
 func InitSpider() {
 	scriptMap := getSpiderScriptDB("all") //加载爬虫,初始化模板
 	scriptMapFile := getSpiderScriptFile(false)
-	k := 0
-	for _, v := range scriptMap {
-		LoopListPath.Store(k, v)
-		k++
+	for code, v := range scriptMap {
+		LoopListPath.Store(code, v)
+		InitCount++
 	}
-	for _, v := range scriptMapFile {
-		LoopListPath.Store(k, v)
-		k++
+	for code, v := range scriptMapFile {
+		LoopListPath.Store(code, v)
+		InitCount++
 	}
-	InitCount = k
 	if util.Config.Working == 0 {
 		NoQueueScript() //高性能模式
 	} else {
@@ -74,8 +72,8 @@ func NoQueueScript() {
 			sp, errstr := NewSpider(code, script)
 			if errstr == "" && sp != nil && sp.Code != "nil" { //脚本加载成功
 				sp2, _ := NewSpider(code, script)
-				sp.Index = qu.IntAll(key)
-				sp2.Index = qu.IntAll(key)
+				//sp.Index = qu.IntAll(key)
+				//sp2.Index = qu.IntAll(key)
 				if info["createuser"] != "" {
 					sp.UserName = info["createuser"]
 					sp2.UserName = info["createuser"]
@@ -174,7 +172,7 @@ func QueueUpScriptList() {
 					sp, errstr := NewSpider_New(code, script, false)
 					//logger.Info("初始化脚本是否成功:", sp != nil, e.Value)
 					if errstr == "" && sp != nil && sp.Code != "nil" { //初始化脚本成功
-						sp.Index = qu.IntAll(key)
+						//sp.Index = qu.IntAll(key)
 						if info["createuser"] != "" {
 							sp.UserName = info["createuser"]
 						}
@@ -274,7 +272,7 @@ func QueueUpScriptDetail() {
 					script := info["script"]
 					sp, errstr := NewSpider_New(code, script, true)
 					if errstr == "" && sp != nil && sp.Code != "nil" { //初始化脚本成功
-						sp.Index = qu.IntAll(key)
+						//sp.Index = qu.IntAll(key)
 						if info["createuser"] != "" {
 							sp.UserName = info["createuser"]
 						}
@@ -425,6 +423,7 @@ func UpdateSpiderByCodeState(code, state string) (bool, error) {
 		// 		"updatetime": time.Now().Unix(),
 		// 	},
 		// }, false, false)
+		logger.Info("下架脚本", code)
 		if util.Config.Working == 1 {
 			for i, as := range []sync.Map{Allspiders, Allspiders2} {
 				if i == 1 && util.Config.Modal == 0 { //队列模式原始模式采集Allspiders2无用(7700下架爬虫)
@@ -443,15 +442,20 @@ func UpdateSpiderByCodeState(code, state string) (bool, error) {
 				}
 			}
 			LoopListPath.Range(func(k, v interface{}) bool {
-				if v != nil {
-					info, _ := v.(map[string]string)
-					if info["code"] == code {
-						LoopListPath.Store(k, nil)
-						lock.Lock()
-						defer lock.Unlock()
-						ChanDels[qu.IntAll(k)] = code
-						logger.Info("下架脚本,LoopListPath更新为nil,ChanDels中位置:", k)
-					}
+				//if v != nil {
+				//	info, _ := v.(map[string]string)
+				//	if info["code"] == code {
+				//		LoopListPath.Store(k, nil)
+				//		lock.Lock()
+				//		defer lock.Unlock()
+				//		ChanDels[qu.IntAll(k)] = code
+				//		logger.Info("下架脚本,LoopListPath更新为nil,ChanDels中位置:", k)
+				//	}
+				//}
+				if k == code {
+					LoopListPath.Delete(k)
+					logger.Info(code, "脚本下架成功")
+					return false //跳出循环
 				}
 				return true
 			})
@@ -467,7 +471,6 @@ func UpdateSpiderByCodeState(code, state string) (bool, error) {
 				}
 			}
 		}
-		logger.Info("下架脚本", code)
 		up = true
 		err = nil
 	} else if state == "-1" { //爬虫重采更新线上爬虫
@@ -482,7 +485,7 @@ func UpdateSpiderByCodeState(code, state string) (bool, error) {
 					listsize++
 					if tmp, ok := val.(map[string]string); ok {
 						count_ok++
-						if tmp["code"] == code { //队列存在,重载脚本
+						if tmp["code"] == code && key == code { //队列存在,重载脚本
 							logger.Info("上架新增脚本,队列中以有该脚本,进行更新")
 							listHas = true
 							LoopListPath.Store(key, v)
@@ -561,9 +564,9 @@ func UpdateSpiderByCodeState(code, state string) (bool, error) {
 				count_ok, count_no := 0, 0
 				LoopListPath.Range(func(key, val interface{}) bool {
 					listsize++
-					if tmp, ok := val.(map[string]string); ok {
+					if tmp, ok := val.(map[string]string); ok { //此处判断仅仅为了得到count_ok的值,可直接判断key==code
 						count_ok++
-						if tmp["code"] == code { //队列存在,重载脚本
+						if tmp["code"] == code && code == key { //队列存在,重载脚本
 							logger.Info("上架新增脚本,队列中以有该脚本,进行更新")
 							listHas = true
 							LoopListPath.Store(key, v)
@@ -576,21 +579,22 @@ func UpdateSpiderByCodeState(code, state string) (bool, error) {
 					return true
 				})
 				logger.Info("上架新增脚本,队列中共有爬虫", listsize, "当前在线数量:", count_ok, "下线数量:", count_no)
-				if !listHas { //队列不存在
+				if !listHas { //队列不存在,新增
 					logger.Info("上架新增脚本,队列中不存在")
-					lock.Lock()
-					defer lock.Unlock()
-					if len(ChanDels) > 0 {
-						for i, _ := range ChanDels {
-							logger.Info("上架新增脚本,替补队列中位置", i)
-							LoopListPath.Store(i, v)
-							delete(ChanDels, i)
-							break
-						}
-					} else {
-						logger.Info("上架新增脚本,新增队列中位置", listsize)
-						LoopListPath.Store(listsize, v) //上架
-					}
+					LoopListPath.Store(code, v) //上架
+					// lock.Lock()
+					// defer lock.Unlock()
+					// if len(ChanDels) > 0 {
+					// 	for i, _ := range ChanDels {
+					// 		logger.Info("上架新增脚本,替补队列中位置", i)
+					// 		LoopListPath.Store(i, v)
+					// 		delete(ChanDels, i)
+					// 		break
+					// 	}
+					// } else {
+					// 	logger.Info("上架新增脚本,新增队列中位置", listsize)
+					// 	LoopListPath.Store(listsize, v) //上架
+					// }
 					//校验是否上架成功
 					saveList := false //记录是否上架成功
 					listsize, count_ok, count_no = 0, 0, 0
@@ -598,9 +602,9 @@ func UpdateSpiderByCodeState(code, state string) (bool, error) {
 						listsize++
 						if tmp, ok := val.(map[string]string); ok {
 							count_ok++
-							if tmp["code"] == code { //队列存在
+							if tmp["code"] == code && key == code { //队列存在
 								saveList = true
-								logger.Info("上架脚本成功", code, "队列中位置", key)
+								logger.Info("上架脚本成功", code)
 							}
 						} else {
 							count_no++
@@ -759,12 +763,12 @@ func ReloadSpiderFile() {
 				var errstr string
 				if util.Config.Working == 1 { //排队模式
 					if i == 0 {
-						length := 0
-						LoopListPath.Range(func(k, v interface{}) bool {
-							length++
-							return true
-						})
-						LoopListPath.Store(length, v) //排队模式Allspiders,Allspiders2共用一个LoopListPath,新增一次即可
+						//length := 0
+						//LoopListPath.Range(func(k, v interface{}) bool {
+						//	length++
+						//	return true
+						//})
+						LoopListPath.Store(k, v) //排队模式Allspiders,Allspiders2共用一个LoopListPath,新增一次即可
 						sp, errstr = NewSpider_New(k, v["script"], false)
 					} else {
 						sp, errstr = NewSpider_New(k, v["script"], true)