package extract

import (
	log "github.com/donnie4w/go-logger/logger"
	ju "jy/util"
	"sync"
	"time"
)

var getasklock sync.Mutex

// 监听-获取-分发抽取任务
func GetExtractUdpTaskInfo() {
	for {
		if len(ju.TaskUdpList) > 0 && !ju.IsGetask {
			getasklock.Lock()
			ju.IsGetask = true
			l := len(ju.TaskUdpList)
			if l > 1 {
				s_nomal := ju.TaskUdpList[0]["nomal"]
				s_file := ju.TaskUdpList[0]["file"]
				e_nomal := ju.TaskUdpList[l-1]["nomal"]
				e_file := ju.TaskUdpList[l-1]["file"]
				n_sid, f_sid := s_nomal["sid"], s_file["sid"]
				n_eid, f_eid := e_nomal["eid"], e_file["eid"]
				ju.TaskUdpList = ju.TaskUdpList[l:]
				log.Debug("合并段落...", n_sid, "~", n_eid, "~", f_sid, "~", f_eid)
				ExtractByUdpPre(n_sid, n_eid, f_sid, f_eid)
			} else {
				nomal := ju.TaskUdpList[0]["nomal"]
				file := ju.TaskUdpList[0]["file"]
				ju.TaskUdpList = ju.TaskUdpList[1:]
				ExtractByUdpPre(nomal["sid"], nomal["eid"], file["sid"], file["eid"])
			}
			ju.IsGetask = false
			getasklock.Unlock()
		} else {
			time.Sleep(3 * time.Second)
		}
	}
}
