86 lines
1.9 KiB
Go
86 lines
1.9 KiB
Go
package cron
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"mini-chat/internal/pkg/httpclient"
|
|
"mini-chat/internal/repository/mysql/dao"
|
|
|
|
"go.uber.org/zap"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func (s *server) Start() {
|
|
s.cron.Start()
|
|
go s.taskCount.Wait()
|
|
|
|
// 每 10 分钟监控小程序状态
|
|
s.cron.AddFunc("0 */10 * * * ?", func() {
|
|
s.taskCount.Add()
|
|
defer s.taskCount.Done()
|
|
|
|
readDB := dao.Use(s.db.GetDbR())
|
|
writeDB := dao.Use(s.db.GetDbW())
|
|
|
|
appList, err := readDB.MiniProgram.Find()
|
|
if err != nil && err != gorm.ErrRecordNotFound {
|
|
s.logger.Error("获取小程序列表失败", zap.Error(err))
|
|
return
|
|
}
|
|
|
|
if len(appList) == 0 {
|
|
return
|
|
}
|
|
|
|
for _, app := range appList {
|
|
type checkAppResponse struct {
|
|
Code int `json:"code"`
|
|
Appid string `json:"appid"`
|
|
Status string `json:"status"`
|
|
}
|
|
|
|
checkAppRes := new(checkAppResponse)
|
|
response, err := httpclient.GetHttpClient().R().
|
|
SetQueryParams(map[string]string{
|
|
"appid": app.AppID,
|
|
}).
|
|
SetResult(checkAppRes).
|
|
Get("https://api.wxapi.work/xcx/checkxcx.php")
|
|
if err != nil {
|
|
s.logger.Error(fmt.Sprintf("[更新小程序状态失败]%s 请求APP验证服务失败", app.AppID), zap.Error(err))
|
|
continue
|
|
}
|
|
if response.IsError() {
|
|
s.logger.Error(fmt.Sprintf("[更新小程序状态失败]%s 请求APP验证服务异常(%d)", app.AppID, response.StatusCode()))
|
|
}
|
|
|
|
status := 0
|
|
if checkAppRes.Code == 1 {
|
|
status = 1
|
|
} else if checkAppRes.Code == 0 {
|
|
status = -1
|
|
}
|
|
|
|
if _, err := writeDB.MiniProgram.
|
|
Where(writeDB.MiniProgram.AppID.Eq(app.AppID)).
|
|
Updates(map[string]interface{}{
|
|
"status": status,
|
|
"updated_user": "cron",
|
|
"updated_at": time.Now(),
|
|
}); err != nil {
|
|
s.logger.Error("更新小程序状态失败", zap.Error(err))
|
|
return
|
|
}
|
|
}
|
|
}, "监控小程序状态")
|
|
|
|
// 初始化任务
|
|
s.initTask()
|
|
}
|
|
|
|
// initTask 初始化任务
|
|
func (s *server) initTask() {
|
|
|
|
}
|