watch.go 907 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package services
  2. import (
  3. "fmt"
  4. "hongze/hongqi_watch/cache"
  5. "path/filepath"
  6. "time"
  7. )
  8. // HandleFileUpdate
  9. // @Description: 处理文件变化
  10. // @author: Roc
  11. // @datetime2023-10-31 09:52:23
  12. func HandleFileUpdate() {
  13. defer func() {
  14. if err := recover(); err != nil {
  15. fmt.Println("[AutoHandle]", err)
  16. }
  17. }()
  18. for {
  19. el := cache.HandleExcelList.Front()
  20. // 如果没取到,那么就睡眠1s
  21. if el == nil {
  22. time.Sleep(1 * time.Second)
  23. continue
  24. }
  25. filePath := el.Value.(string)
  26. fileExt := filepath.Ext(filePath)
  27. if fileExt != ".xlsx" && fileExt != ".xls" {
  28. //fmt.Println("不是excel文件")
  29. return
  30. }
  31. // 读取excel内容并操作入库
  32. ReadHqExcel(filePath)
  33. // 处理完后就移除该list
  34. cache.HandleExcelList.Remove(el)
  35. cache.HandleExcelFilePathMutex.Lock()
  36. delete(cache.HandleExcelFilePathMap, filePath)
  37. cache.HandleExcelFilePathMutex.Unlock()
  38. }
  39. }