package monitors import ( "github.com/robfig/cron/v3" "log" ) type FileMonitor struct { Directory string FilenameExtension string MonitorHelper } func (the *FileMonitor) Initial() { the.Cron = cron.New() log.Printf("文件目录监听 cronStr=%s", the.CronStr) } func (the *FileMonitor) Subscribe(task func()) { entryID, err := the.Cron.AddFunc(the.CronStr, task) if err != nil { log.Printf("cron 定时任务[%v]添加异常:%s", entryID, err.Error()) } the.Cron.Start() } func FileMonitorInitial(Directory string, FilenameExtension string, cron string) *FileMonitor { fileMonitorHelper := FileMonitor{ Directory: Directory, FilenameExtension: FilenameExtension, MonitorHelper: MonitorHelper{ CronStr: cron, }, } fileMonitorHelper.Initial() return &fileMonitorHelper }