package icsbatch import ( "batchmodule/icsconf" "batchmodule/icshttp" "batchmodule/icslog" "batchmodule/icsstt" "batchmodule/icsutil" "fmt" "io" "log" "os" "path/filepath" "strconv" "strings" "time" "github.com/robfig/cron/v3" ) var ( cfg icsconf.AppInfo l *log.Logger fList icshttp.FailedList ) type FailedList struct { No int `json:"no"` ConnId string `json:"connid"` Ext string `json:"ext"` StartTime string `json:"starttime"` } func init() { cfg = icsconf.Getconfig() l = icslog.InitializeLogger() } func BatchForFailedSTT() { wavFilePath := cfg.Directories.WAVDirectory loc, err := time.LoadLocation("Asia/Seoul") if err != nil { l.Fatalln("타임존 로드 실패: ", err) } cronInstance := cron.New(cron.WithLocation(loc)) // Adding Cron Instance which runs at every 1am _, err = cronInstance.AddFunc("0 1 * * *", func() { l.Println("cron is working...calling ProcessPostRequestForDataList()...at ", time.Now().Format("2006-01-02_15:04:05")) // request for failed datalist response, err := icshttp.PostReqForFailedDataList() if err != nil { l.Printf("batch_everyday.go>BatchForFailedSTT> icshttp.PostReqForFailedDataList() failed. err: %+v", err) } totalCnt, _ := strconv.ParseInt(response.TotalCnt, 10, 64) if int(totalCnt) <= 0 { l.Printf("batch_everyday.go>BatchForFailedSTT> icshttp.PostReqForFailedDataList()> The Failed Data is Zero. totalCnt: %d", totalCnt) } else { for _, item := range response.List { reqDataForVoiceFile := icshttp.FailedDataListReqBody{ StartTime: item.StartTime, Ext: item.Ext, ConnId: item.ConnId, } // request for each voice file on the failed datalist response, err := icshttp.PostReqForEachVoiceFile(reqDataForVoiceFile) if err != nil { l.Printf("batch_everyday.go>BatchForFailedSTT> icshttp.PostReqForEachVoiceFile() failed. err: %+v", err) } voiceFile := response.VoiceFile if voiceFile == "" { l.Printf("batch_everyday.go>BatchForFailedSTT> voiceFile is empty on %+v", reqDataForVoiceFile) break } fileName := fmt.Sprintf(`%s/%s/%s.wav`, wavFilePath, item.StartTime, item.ConnId) file, error := os.Create(fileName) if error != nil { l.Println("Error at batch_everyday.go>BatchForFailedSTT> os.Create() err: ", error) break } defer file.Close() voiceFileReader := strings.NewReader(voiceFile) _, error = io.Copy(file, voiceFileReader) if error != nil { l.Println("Error at batch_everyday.go>BatchForFailedSTT> io.Copy(file, voiceFileReader) err: ", error) break } // devide the wav file to 2 channels pcmResult, folderName := icsutil.DevideWavTo2Pcm(fileName, item.StartTime) if pcmResult { // RUN STT sttCallRes, err := icsstt.STTController(reqDataForVoiceFile, folderName) if err != nil { l.Println("runSTT() failed with the error: ", err) } // Delete the pcm files if stt was successful if sttCallRes { pcmDir := filepath.Join(cfg.Directories.PCMDirectory, st) icsutil.DeletePcmFolder(pcmDir) } } } } }) if err != nil { l.Printf("Error at batch_everyday.go>BatchForFailedSTT> Adding cron instance failed. error: %v", err) return } cronInstance.Start() select {} } func BatchForFailedSTTTest() { loc, err := time.LoadLocation("Asia/Seoul") if err != nil { l.Fatalln("타임존 로드 실패: ", err) } cronInstance := cron.New(cron.WithLocation(loc)) _, err = cronInstance.AddFunc("51 12 * * *", func() { l.Println("current server time: ", time.Now().Format("2006-01-02 15:04:05")) fmt.Println("test succeeded") }) if err != nil { fmt.Println("스케줄 추가 오류:", err) return } cronInstance.Start() select {} }