main.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "log"
  6. . "online_datasync/config"
  7. . "online_datasync/entity"
  8. "sync"
  9. "time"
  10. . "app.yhyue.com/moapp/jybase/common"
  11. . "app.yhyue.com/moapp/jybase/date"
  12. )
  13. var (
  14. wait = &sync.WaitGroup{}
  15. pool = make(chan bool, Config.SyncPool)
  16. allMap = map[string]Entity{
  17. "raw_user": Raw_user,
  18. "raw_product": Raw_product,
  19. "action_order": Action_order,
  20. "action_order_spec": Action_order,
  21. "action_product_record": Action_product_record,
  22. "user_prize": User_prize,
  23. "integral_flow": Integral_flow,
  24. "point_type": Point_type,
  25. }
  26. )
  27. func main() {
  28. m := flag.Int("m", 0, "模式 0:定时任务 1:非定时任务")
  29. t := flag.String("t", "", "表名")
  30. flag.Parse()
  31. all := []Entity{}
  32. for _, v := range allMap {
  33. all = append(all, v)
  34. }
  35. if *m == 1 {
  36. if *t == "" {
  37. run(all...)
  38. } else if allMap[*t] != nil {
  39. run(allMap[*t])
  40. } else {
  41. log.Fatalln(*t, "表的同步数据任务不存在")
  42. }
  43. } else {
  44. SimpleCrontab(false, Config.RunTime, func() {
  45. run(all...)
  46. })
  47. select {}
  48. }
  49. }
  50. //
  51. func run(es ...Entity) {
  52. now := time.Now()
  53. //
  54. var start_unix int64
  55. if TimeTask.Datetime != "" {
  56. start, err := time.ParseInLocation(Date_Full_Layout, TimeTask.Datetime, time.Local)
  57. if err != nil {
  58. log.Fatalln(err)
  59. }
  60. start_unix = start.Unix()
  61. }
  62. //
  63. end_unix := now.Unix()
  64. end_layout := FormatDate(&now, Date_Full_Layout)
  65. log.Println("同步任务开始。。。", fmt.Sprintf("%+v", TimeTask))
  66. //
  67. for _, e := range es {
  68. pool <- true
  69. wait.Add(1)
  70. go func(v Entity) {
  71. defer func() {
  72. <-pool
  73. wait.Done()
  74. }()
  75. v.Run(start_unix, end_unix, TimeTask.Datetime, end_layout)
  76. }(e)
  77. }
  78. //
  79. wait.Wait()
  80. TimeTask.Datetime = end_layout
  81. WriteSysConfig("./timetask.json", &TimeTask)
  82. log.Println("同步任务结束。。。")
  83. }