代码拉取完成,页面将自动刷新
package main
import (
"context"
"database/sql"
"encoding/json"
"gitee.com/aywc_1/aywc_judge/src/appinit"
"gitee.com/aywc_1/aywc_judge/src/configuration"
"gitee.com/aywc_1/aywc_judge/src/lib"
"gitee.com/aywc_1/aywc_judge/src/models"
"gitee.com/aywc_1/aywc_judge/src/mqttmessage"
"gitee.com/aywc_1/aywc_judge/src/utils"
"github.com/astaxie/beego/logs"
"github.com/jmoiron/sqlx"
"github.com/robfig/cron/v3"
log "github.com/sirupsen/logrus"
"os"
)
var MyCron *cron.Cron
var configFilePath = ""
var isLockDeleteExpirePicture = false
var isLockWorkerSign = false
const DeviceWarmProcessSql = "select id,upload_file_name,create_time from device_warm where status=0 and TIMESTAMPDIFF(SECOND,create_time,now())>? limit 100 "
const workSignSql = "select id,upload_file_name,create_time from device_warm where status=0 and TIMESTAMPDIFF(SECOND,create_time,now())>? limit 100 "
func initCron() error {
deviceWarmProcessCron := configuration.GetYamlValue([]string{"config", "business", "expired_picture_process", "cron"}, 0, "0/5 * * * * *").(string)
nurseSignCron := configuration.GetYamlValue([]string{"config", "business", "nurse_sign", "cron"}, 0, "0/5 * * * * *").(string)
MyCron = cron.New(cron.WithSeconds()) //支持秒级定时器
_, err := MyCron.AddFunc(deviceWarmProcessCron, DeleteExpirePicture)
if err != nil {
log.Error(err)
return err
}
_, err = MyCron.AddFunc(nurseSignCron, nurseSign)
if err != nil {
log.Error(err)
return err
}
return nil
}
func DeleteExpirePicture() {
if isLockDeleteExpirePicture {
log.Info("DeleteExpirePicture locked.return.......")
return
}
tx, err := appinit.GetDB().BeginTxx(context.Background(), nil)
if err != nil {
log.Error("事务失败:", err)
return
}
isLockDeleteExpirePicture = true //加锁
defer clearTx(tx) //清理事务
expired_picture := configuration.GetYamlValue([]string{"config", "business", "expired_picture_process", "expired"}, 0, 86400).(int)
uploadPath := configuration.GetYamlValue([]string{"upload_path"}, 0, "/upload").(string)
rows, err := tx.Queryx(DeviceWarmProcessSql, expired_picture)
if err != nil {
tx.Rollback()
return
}
defer rows.Close()
tms := []models.DeviceWarmModel{}
_ = sqlx.StructScan(rows, &tms)
for _, tm := range tms {
sourcePath := uploadPath + "/" + tm.Id + "_" + tm.UploadFileName
_, err = tx.Exec("delete from device_warm where id=? ", tm.Id)
if err != nil {
tx.Rollback()
}
err := os.Remove(sourcePath)
if err != nil {
log.Errorf("Failed to remove file,err:%s\n", err.Error())
continue
}
}
}
/**
护工上门
*/
func nurseSign() {
if isLockWorkerSign {
log.Info("nurseSign locked.return.......")
return
}
isLockWorkerSign = true //加锁
rows, err := appinit.GetDB().Queryx(workSignSql)
if err != nil {
log.Error(err)
}
transList := []*models.DeviceWarmModel{}
err = sqlx.StructScan(rows, &transList)
const (
clientId = "nurse-001"
// topic规则:设备编号/接口名
topicName = "device001/nursepub"
actionNameUser = "nurse/sign"
)
client := lib.NewClient(clientId)
err = client.Connect()
if err != nil {
logs.Error(err.Error())
}
body := mqttmessage.NurseSignMessage{ID: 1, NurseName: "小宝", SignTime: utils.GetNowTime().Unix()}
msg := &lib.Message{
ClientID: clientId,
Action: actionNameUser,
Type: "json",
Time: utils.GetNowTime().Unix(),
Body: body,
}
data, _ := json.Marshal(msg)
err = client.Publish(topicName, 0, false, data)
if err != nil {
log.Error(err)
}
}
func clearTx(tx *sqlx.Tx) {
err := tx.Commit()
if err != nil && err != sql.ErrTxDone {
log.Println("tx error:", err)
}
isLockDeleteExpirePicture = false
}
func main() {
configuration.InitLog("check")
c := make(chan error)
go func() {
err := appinit.DBInit()
if err != nil {
c <- err
}
}()
go func() {
err := initCron()
if err != nil {
c <- err
}
MyCron.Start() //开启定时任务
}()
err := <-c
log.Fatal(err)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。