附近小店
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 

301 рядки
14 KiB

  1. package task
  2. import (
  3. "github.com/robfig/cron/v3"
  4. "time"
  5. "applet/app/cfg"
  6. "applet/app/db"
  7. "applet/app/db/model"
  8. "applet/app/md"
  9. "applet/app/utils"
  10. "applet/app/utils/logx"
  11. "xorm.io/xorm"
  12. )
  13. var (
  14. timer *cron.Cron
  15. jobs = map[string]func(*xorm.Engine, string){}
  16. baseEntryId cron.EntryID
  17. entryIds []cron.EntryID
  18. taskCfgList map[string]*[]model.SysCfg
  19. ch = make(chan int, 50)
  20. workerNum = 50 // 智盟跟单并发数量
  21. orderStatWorkerNum = 50 // 智盟跟单并发数量
  22. tbagoworkerNum = 50 // 智盟跟单并发数量
  23. tbsettleworkerNum = 50 // 智盟跟单并发数量
  24. pddOrderWorkerNum = 50 // 拼多多跟单并发数量
  25. orderSuccessWorkerNum = 50 //
  26. tbOrderWorkerNum = 50 // 淘宝跟单并发数量
  27. jdOrderWorkerNum = 50 // 京东跟单并发数量
  28. wphOrderWorkerNum = 50 // 唯品会跟单并发数量
  29. cardWorkerNum = 20 // 权益卡并发数量
  30. tbRelationWorkerNum = 50 // 淘宝并发数量
  31. hw365WorkerNum = 50 // 海威并发数量
  32. hw365TourismWorkerNum = 50 // 海威并发数量
  33. tbpubWorkerNum = 50 // 海威并发数量
  34. liveWorkerNum = 50 // 海威并发数量
  35. tikTokOwnWorkerNum = 50 // 海威并发数量
  36. cardUpdateWorkerNum = 50 // 海威并发数量
  37. lifeWorkerNum = 50 //生活服务跟单
  38. pddWorkerNum = 50 //
  39. oilWorkerNum = 50 //
  40. otherWorkerNum = 50 // 淘宝, 苏宁, 考拉并发量
  41. jdWorkerNum = 50 //
  42. tikTokWorkerNum = 50 //
  43. teamGoodsWorkerNum = 50
  44. jdCh = make(chan int, 50)
  45. jdWorkerNum1 = 50 //
  46. orderStatCh = make(chan int, 50)
  47. jdCh1 = make(chan int, 50)
  48. oilCh = make(chan int, 50)
  49. otherCh = make(chan int, 50)
  50. otherTourismCh = make(chan int, 50)
  51. liveOtherCh = make(chan int, 50)
  52. teamGoodsCh = make(chan int, 50)
  53. tikTokOwnOtherCh = make(chan int, 50)
  54. cardUpdateCh = make(chan int, 50)
  55. tbpubCh = make(chan int, 50)
  56. cardCh = make(chan int, 20)
  57. pddCh = make(chan int, 50)
  58. tikTokCh = make(chan int, 50)
  59. tbRefundCh = make(chan int, 50)
  60. tbagodCh = make(chan int, 50)
  61. tbsettleCh = make(chan int, 50)
  62. pddFailCh = make(chan int, 50)
  63. orderSuccessCh = make(chan int, 50)
  64. tbRelationCh = make(chan int, 50)
  65. )
  66. func Init() {
  67. // 初始化任务列表
  68. initTasks()
  69. var err error
  70. timer = cron.New()
  71. if baseEntryId, err = timer.AddFunc("@every 20m", reload); err != nil {
  72. _ = logx.Fatal(err)
  73. }
  74. }
  75. func Run() {
  76. reload()
  77. timer.Start()
  78. _ = logx.Info("auto tasks running...")
  79. }
  80. func reload() {
  81. // 重新初始化数据库
  82. db.InitMapDbs(cfg.DB, cfg.Prd)
  83. if len(taskCfgList) == 0 {
  84. taskCfgList = map[string]*[]model.SysCfg{}
  85. }
  86. // 获取所有站长的配置信息
  87. for dbName, v := range db.DBs {
  88. if conf := db.MapCrontabCfg(v); conf != nil {
  89. if cfg.Debug {
  90. dbInfo := md.SplitDbInfo(v)
  91. // 去掉模版库
  92. if dbName == "000000" {
  93. continue
  94. }
  95. _ = logx.Debugf("【MasterId】%s, 【Host】%s, 【Name】%s, 【User】%s, 【prd】%v, 【Task】%v\n", dbName, dbInfo.Host, dbInfo.Name, dbInfo.User, cfg.Prd, utils.SerializeStr(*conf))
  96. }
  97. taskCfgList[dbName] = conf
  98. }
  99. }
  100. if len(taskCfgList) > 0 {
  101. // 删除原有所有任务
  102. if len(entryIds) > 0 {
  103. for _, v := range entryIds {
  104. if v != baseEntryId {
  105. timer.Remove(v)
  106. }
  107. }
  108. entryIds = nil
  109. }
  110. var (
  111. entryId cron.EntryID
  112. err error
  113. )
  114. // 添加任务
  115. for dbName, v := range taskCfgList {
  116. for _, vv := range *v {
  117. if _, ok := jobs[vv.Key]; ok && vv.Val != "" {
  118. // fmt.Println(vv.Val)
  119. if entryId, err = timer.AddFunc(vv.Val, doTask(dbName, vv.Key)); err == nil {
  120. entryIds = append(entryIds, entryId)
  121. }
  122. }
  123. }
  124. }
  125. }
  126. }
  127. func doTask(dbName, fnName string) func() {
  128. return func() {
  129. begin := time.Now().Local()
  130. jobs[fnName](db.DBs[dbName], dbName)
  131. end := time.Now().Local()
  132. logx.Infof(
  133. "[%s] AutoTask <%s> started at <%s>, ended at <%s> duration <%s>",
  134. dbName,
  135. fnName,
  136. begin.Format("2006-01-02 15:04:05.000"),
  137. end.Format("2006-01-02 15:04:05.000"),
  138. time.Duration(end.UnixNano()-begin.UnixNano()).String(),
  139. )
  140. }
  141. }
  142. // 增加自动任务队列
  143. func initTasks() {
  144. //v2
  145. //jobs[md.KEY_CFG_CRON_BUCKLE] = taskOrderBuckle //
  146. //jobs[md.KEY_CFG_CRON_CHECK_BUCKLE_ORDER] = taskCheckBuckleOrder //
  147. //jobs[md.KEY_CFG_CRON_USER_RELATE] = taskUserRelate //
  148. //v3
  149. //jobs[md.KEY_CFG_CRON_TB12] = taskOrderTaobao12 //淘宝抓单
  150. //jobs[md.KEY_CFG_CRON_TBBYAGOTIME] = taskAgoOrderTB //用于恢复个别时间丢单的
  151. //jobs[md.KEY_CFG_CRON_TB_PUNISH_REFUND] = taskTbPunishOrderRefund //淘宝退款订单处理
  152. //jobs[md.KEY_CFG_CRON_TBREFUND] = taskTbOrderRefund //淘宝退款订单处理
  153. //jobs[md.KEY_CFG_CRON_PUBLISHER_RELATION] = taskTaobaoPublisherRelation //获取渠道信息
  154. //jobs[md.KEY_CFG_CRON_PUBLISHER_RELATION_NEW] = taskTaobaoPublisherRelationNew //获取渠道信息
  155. //jobs[md.KEY_CFG_CRON_TBSETTLEORDER] = taskOrderTaobaoSettleOrder //淘宝抓单结算订单
  156. //v4
  157. //jobs[md.KEY_CFG_CRON_PDD_SUCC] = taskOrderPddSucc
  158. //jobs[md.KEY_CFG_CRON_PDDBYSTATUS] = taskOrderPddStatus
  159. //jobs[md.KEY_CFG_CRON_PDDBYSTATUSSUCCESS] = taskOrderPddStatusSuccess
  160. //jobs[md.KEY_CFG_CRON_PDDBYSTATUSFAIL] = taskOrderPddStatusFail
  161. //jobs[md.KEY_CFG_CRON_PDDBYLOOPTIME] = taskLoopOrderPdd //拼多多创建时间循环当天
  162. //jobs[md.KEY_CFG_CRON_PDDREFUND] = taskPddOrderRefund //拼多多退款订单处理
  163. //jobs[md.KEY_CFG_CRON_PDDBYAGOTIME] = taskAgoOrderPdd //用于恢复个别时间丢单的
  164. //jobs[md.KEY_CFG_CRON_PDDBYCREATETIME] = taskOrderPddByCreateTime //拼多多创建时间跟踪订单
  165. //jobs[md.KEY_CFG_CRON_PDD] = taskOrderPdd
  166. //v5-guide-settle
  167. //jobs[md.KEY_CFG_CRON_SETTLE] = taskOrderSettle // 结算
  168. //v5
  169. //jobs[md.KEY_CFG_CRON_FREE_SETTLE] = taskOrderFreeSettle // 结算
  170. //jobs[md.KEY_CFG_CRON_SECOND_FREE_SETTLE] = taskOrderSecondFreeSettle // 结算
  171. //jobs[md.KEY_CFG_CRON_THIRD_FREE_SETTLE] = taskOrderMoreFreeSettle // 结算
  172. //jobs[md.KEY_CFG_CRON_AGGREGATION_RECHARGE_SETTLE] = taskAggregationRechargeSettle //
  173. //jobs[md.KEY_CFG_CRON_PLAYLET_SETTLE] = taskAggregationPlaylet //
  174. //jobs[md.KEY_CFG_CRON_DUOYOUORD_SETTLE] = taskDuoYouSettle //
  175. //jobs[md.KEY_CFG_CRON_LIANLIAN_SETTLE] = taskLianlianSettle //
  176. //jobs[md.KEY_CFG_CRON_SWIPE_SETTLE] = taskSwipeSettle //
  177. //jobs[md.KEY_CFG_CRON_USER_LV_UP_SETTLE] = taskUserLvUpOrderSettle // 会员费订单结算
  178. //jobs[md.KEY_CFG_CRON_PRIVILEGE_CARD_SETTLE] = taskPrivilegeCardOrderSettle // 权益卡订单结算
  179. //jobs[md.KEY_CFG_CRON_ACQUISITION_CONDITION] = taskAcquisitionCondition
  180. //jobs[md.KEY_CFG_CRON_ACQUISITION_CONDITION_BY_LV] = taskAcquisitionConditionByLv
  181. //jobs[md.KEY_CFG_CRON_ACQUISITION_REWARD] = taskAcquisitionReward
  182. //jobs[md.KEY_CFG_CRON_NEW_ACQUISTION_SETTLE] = taskNewAcquisition // 拉新
  183. //jobs[md.KEY_CFG_CRON_ACQUISTION_SETTLE] = taskAcquisition // 拉新
  184. //jobs[md.KEY_CFG_VERIFY] = taskVerify //团长
  185. //v7
  186. //jobs[md.KEY_CFG_CRON_JD] = taskOrderJd
  187. //jobs[md.KEY_CFG_CRON_JDFAILBYCREATETIME] = taskOrderJDFailByCreateTime //拼多多创建时间跟踪订单
  188. //jobs[md.KEY_CFG_CRON_JDBYCREATETIME] = taskOrderJDByCreateTime //拼多多创建时间跟踪订单
  189. //jobs[md.KEY_CFG_CRON_JDBYSUCCESS] = taskOrderJDBySuccess //拼多多创建时间跟踪订单
  190. //jobs[md.KEY_CFG_CRON_ORDER_SUCCESS_CHECK] = taskOrderSuccessCheck //
  191. //jobs[md.KEY_CFG_CRON_JDBYSTATUS] = taskOrderJdStatus
  192. //jobs[md.KEY_CFG_CRON_JDREFUND] = taskJdOrderRefund //京东退款订单处理
  193. //v6
  194. jobs[md.KEY_CFG_CRON_TIKTOKLIFE] = taskOrderTikTokLife //抖音本地生活
  195. jobs[md.KEY_CFG_KUAISHOU_AUTH] = taskKuaishouAuth //团长
  196. jobs[md.KEY_CFG_TIK_TOK_TEAM_ORDER_PAY] = taskTikTokTeamOrder //团长
  197. jobs[md.KEY_CFG_TIK_TOK_TEAM_ORDER_UPDATE] = taskTikTokTeamOrderUpdate //团长
  198. jobs[md.KEY_CFG_TIK_TOK_TEAM_USER_BIND_BUYINID] = taskTikTokTeamUserBindBuyinid //达人buyin_id
  199. jobs[md.KEY_CFG_KUAISHOU_TEAM_ORDER_PAY] = taskKuaishouTeamOrder //团长
  200. jobs[md.KEY_CFG_KUAISHOU_TEAM_ORDER_UPDATE] = taskKuaishouTeamOrderUpdate //团长
  201. jobs[md.KEY_CFG_CRON_AUTO_ADD_TIKTOK_GOODS] = taskAutoAddTikTokGoods //
  202. jobs[md.KEY_CFG_CRON_KuaishouOwn] = taskOrderKuaishouOwn //
  203. jobs[md.KEY_CFG_CRON_KuaishouOwnCreate] = taskOrderKuaishouOwnCreate //
  204. jobs[md.KEY_CFG_CRON_KUAISHOU] = taskOrderKuaishou //
  205. jobs[md.KEY_CFG_CRON_KUAISHOULIVE] = taskOrderKuaishouLive //
  206. jobs[md.KEY_CFG_CRON_TIKTOKCsjp] = taskOrderTIKTokCsjp //
  207. jobs[md.KEY_CFG_CRON_TIKTOKCsjpLive] = taskOrderTIKTokCsjpLive //
  208. jobs[md.KEY_CFG_CRON_TIKTOKOwnCsjp] = taskOrderTIKTokOwnCsjp //
  209. jobs[md.KEY_CFG_CRON_TIKTOKOwnCsjpLive] = taskOrderTIKTokOwnCsjpLive //
  210. jobs[md.KEY_CFG_CRON_TIKTOKOwnCsjpActivity] = taskOrderTIKTokOwnCsjpActivity //
  211. jobs[md.KEY_CFG_CRON_KUAISHOUOFFICIAL] = taskOrderKuaishouOfficial //
  212. jobs[md.KEY_CFG_CRON_KUAISHOUOFFICIALLive] = taskOrderKuaishouOfficialLive //
  213. //v8
  214. //jobs[md.KEY_CFG_CRON_MEITUANLM_START] = taskOrderMeituanLmStart //智盟返回的美团联盟
  215. //jobs[md.KEY_CFG_CRON_MEITUAN_START] = taskOrderMeituanStart //智盟返回的美团联盟
  216. //jobs[md.KEY_CFG_CRON_MEITUAN] = taskOrderMeituan
  217. //jobs[md.KEY_CFG_CRON_MEITUANLM] = taskOrderMeituanLm //智盟返回的美团联盟
  218. //jobs[md.KEY_CFG_CRON_STATIONMEITUANLM] = taskOrderStationMeituanLm //站长自己美团联盟
  219. //jobs[md.KEY_CFG_CRON_MEITUANOFFICIAL] = taskOrderMeituanOfficial //站长自己美团联盟
  220. //v9
  221. //jobs[md.KEY_CFG_CRON_ELM] = taskOrderElm //
  222. //jobs[md.KEY_CFG_CRON_HEYTEA] = taskOrderHeytea //海威365喜茶
  223. //jobs[md.KEY_CFG_CRON_PIZZA] = taskOrderPizza //海威365
  224. //jobs[md.KEY_CFG_CRON_WALLACE] = taskOrderWallace //海威365
  225. //jobs[md.KEY_CFG_CRON_TOURISM] = taskOrderTourism //海威365
  226. //jobs[md.KEY_CFG_CRON_FLOWERCAKE] = taskOrderFlowerCake //海威365
  227. //jobs[md.KEY_CFG_CRON_DELIVERY] = taskOrderDelivery //海威365
  228. //jobs[md.KEY_CFG_CRON_BURGERKING] = taskOrderBurgerKing //海威365汉堡王
  229. //jobs[md.KEY_CFG_CRON_STARBUCKS] = taskOrderStarbucks //海威365星巴克
  230. //jobs[md.KEY_CFG_CRON_MCDONALD] = taskOrderMcdonald //海威365麦当劳
  231. //jobs[md.KEY_CFG_CRON_HWMOVIE] = taskOrderHwMovie //海威365电影票
  232. //jobs[md.KEY_CFG_CRON_NAYUKI] = taskOrderNayuki //海威365奈雪
  233. //jobs[md.KEY_CFG_CRON_TO_KFC] = taskOrderToKfc //海威365
  234. //jobs[md.KEY_CFG_CRON_PAGODA] = taskOrderPagoda //海威365
  235. //jobs[md.KEY_CFG_CRON_LUCKIN] = taskOrderLuckin //海威365
  236. //v10
  237. ////jobs[md.KEY_CFG_CRON_WPHREFUND] = taskWphOrderRefund //唯品会退款订单处理
  238. ////jobs[md.KEY_CFG_CRON_VIP] = taskOrderVip
  239. //jobs[md.KEY_CFG_CRON_KFC] = taskOrderKfc
  240. //jobs[md.KEY_CFG_CRON_CINEMA] = taskOrderCinema
  241. //jobs[md.KEY_CFG_CRON_DUOMAI] = taskOrderDuomai //多麦跟单
  242. //jobs[md.KEY_CFG_CRON_PLAYLET_ORDER] = taskPlayletOrder
  243. //jobs[md.KEY_CFG_CRON_DIDI_ENERGY] = taskOrderDidiEnergy //滴滴加油
  244. //jobs[md.KEY_CFG_CRON_T3_CAR] = taskOrderT3Car //T3打车
  245. //jobs[md.KEY_CFG_CRON_DIDI_ONLINE_CAR] = taskOrderDidiOnlineCar //滴滴网约车
  246. //jobs[md.KEY_CFG_CRON_KING_FLOWER] = taskOrderKingFlower //滴滴网约车
  247. //jobs[md.KEY_CFG_CRON_DIDI_FREIGHT] = taskOrderDidiFreight //滴滴货运
  248. //jobs[md.KEY_CFG_CRON_DIDI_CHAUFFEUR] = taskOrderDidiChauffeur //滴滴代驾
  249. //jobs[md.KEY_CFG_CRON_OILSTATION] = taskOrderOilstation
  250. //jobs[md.KEY_CFG_CRON_BRIGHTOILSTATION] = taskOrderBrightOilstation
  251. //jobs[md.KEY_CFG_CRON_SN] = taskOrderSuning
  252. //jobs[md.KEY_CFG_CRON_KL] = taskOrderKaola
  253. ////原来的
  254. //jobs[md.KEY_CFG_CRON_PlayLet_Total] = taskPlayletTotal //
  255. //jobs[md.KEY_CFG_CRON_CHECK_GUIDE_STORE_ORDER] = taskCheckGuideStoreOrder //
  256. //jobs[md.KEY_CFG_CRON_FAST_REFUND] = taskOrderFastRefund //
  257. //jobs[md.KEY_CFG_CRON_FAST_SUCCESS] = taskOrderFastSuccess //
  258. //jobs[md.KEY_CFG_CRON_DUOYOUORD] = taskOrderDuoYouOrd //
  259. //jobs[md.KEY_CFG_CRON_TASKBOX] = taskOrderTaskBoxOrd //
  260. //jobs[md.KEY_CFG_CRON_TASKBOXSECOND] = taskOrderTaskSecondOrd //
  261. //jobs[md.KEY_CFG_CRON_CARD_CHECK_UPDATE] = taskCardCheckUpdate //权益卡退款
  262. //jobs[md.KEY_CFG_CRON_CARD_UPDATE] = taskCardUpdate // 权益卡更新
  263. //jobs[md.KEY_CFG_CRON_ORDER_STAT] = taskOrderStat // 订单统计
  264. //jobs[md.KEY_CFG_CRON_GOODS_SHELF] = taskGoodsShelf //站内商品上下架
  265. //jobs[md.KEY_CFG_CRON_CARD_RETURN] = taskCardReturn //权益卡退款
  266. //jobs[md.KEY_CFG_CRON_DTKBRAND] = taskTaoKeBrandInfo // 大淘客品牌信息
  267. //jobs[md.KEY_CFG_CRON_PUBLISHER] = taskTaobaoPublisherInfo // 淘宝备案信息绑定
  268. //jobs[md.KEY_CFG_CRON_AUTO_UN_FREEZE] = taskAutoUnFreeze // 定时解冻
  269. //先不用
  270. //jobs[md.ZhimengCronPlayletVideoOrder] = taskPlayletVideoOrder //
  271. //jobs[md.ZhimengCronPlayletVideoOrderYesterDay] = taskPlayletVideoOrderYesterday //
  272. //jobs[md.ZhimengCronPlayletVideoOrderMonth] = taskPlayletVideoOrderMonth //
  273. //jobs[md.ZhimengCronPlayletAdvOrderMonth] = taskPlayletAdvOrderMonth //
  274. //jobs[md.ZhimengCronPlayletAdvOrder] = taskPlayletAdvOrder //
  275. //jobs[md.ZhimengCronPlayletAdvOrderYesterDay] = taskPlayletAdvOrderYesterday //
  276. //jobs[md.ZhimengCronPlayletAdvOrderYesterDayToMoney] = taskPlayletAdvOrderYesterdayToMoney //
  277. }