123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- package services
- import (
- "context"
- "eta/eta_crawler/utils"
- "fmt"
- "io/fs"
- "os"
- "path/filepath"
- "strings"
- "time"
- "github.com/patrickmn/go-cache"
- )
- func CoalWatchTask(cont context.Context) (err error) {
- ReadWatchIndexFile()
- return
- }
- func ReadWatchIndexFile() {
- fmt.Println("ReadWatchIndexFile start")
- var err error
- defer func() {
- if err != nil {
- fmt.Println("ReadWatchIndexFile Err:" + err.Error())
- }
- }()
- var cacheClient *cache.Cache
- if cacheClient == nil {
- cacheClient = cache.New(365*24*time.Hour, 365*24*time.Hour)
- }
- err = filepath.Walk(utils.CoalFilePath, func(path string, info fs.FileInfo, err error) error {
- if err != nil {
- return err
- }
- if !info.IsDir() {
- fileInfo, err := os.Stat(path)
- if err != nil {
- fmt.Println("os.Stat:", err.Error())
- }
- // winFileAttr := fileInfo.Sys().(*syscall.Win32FileAttributeData)
- modifyTimeStr := fileInfo.ModTime().Format(utils.FormatDateTime)
- existModifyTime, ok := cacheClient.Get(path)
- if ok {
- existModifyTimeStr := existModifyTime.(string)
- if existModifyTimeStr != modifyTimeStr {
- if strings.Contains(path, "442家晋陕蒙煤矿周度产量数据") {
- err = Jsm(path)
- } else if strings.Contains(path, "内陆17省动力煤终端用户供耗存") {
- err = Inland(path)
- } else if strings.Contains(path, "沿海八省动力煤终端用户供耗存数据更新") {
- err = Coastal(path)
- } else if strings.Contains(path, "442家晋陕蒙历史数据") {
- err = JsmHistory(path)
- } else if strings.Contains(path, "CⅢ-8-16 25省市库存和日耗情况") {
- err = CoastalHistory(path)
- time.Sleep(time.Second * 10)
- err = InlandHistory(path)
- } else if strings.Contains(path, "分企业煤炭产量旬度数据") {
- err = Firm(path)
- }
- }
- } else {
- if strings.Contains(path, "442家晋陕蒙煤矿周度产量数据") {
- err = Jsm(path)
- } else if strings.Contains(path, "内陆17省动力煤终端用户供耗存") {
- err = Inland(path)
- } else if strings.Contains(path, "沿海八省动力煤终端用户供耗存数据更新") {
- err = Coastal(path)
- } else if strings.Contains(path, "442家晋陕蒙历史数据") {
- err = JsmHistory(path)
- } else if strings.Contains(path, "CⅢ-8-16 25省市库存和日耗情况") {
- err = CoastalHistory(path)
- time.Sleep(time.Second * 10)
- err = InlandHistory(path)
- } else if strings.Contains(path, "分企业煤炭产量旬度数据") {
- err = Firm(path)
- }
- }
- cacheClient.Delete(path)
- cacheClient.Set(path, modifyTimeStr, 24*time.Hour)
- }
- return nil
- })
- }
|