You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
4.0 KiB
Go

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package crontask
import (
"autogo/common"
"autogo/dbsql"
"autogo/models"
"autogo/monkey"
"fmt"
"os/exec"
"strings"
"time"
"github.com/robfig/cron/v3"
"github.com/spf13/cast"
)
func Run() {
crontab := cron.New(cron.WithSeconds())
crontab.AddFunc("0 */1 * * * ?", CheckAndroidDevices)
crontab.AddFunc("0 */1 * * * ?", CheckIOSDevices)
crontab.AddFunc("*/15 * * * * ?", CheckMonkeyTasks)
crontab.Start()
fmt.Println("[Crontab] 定时任务启动")
}
func CheckAndroidDevices() {
// fmt.Println("[Crontab] 检查安卓设备")
command := `devices` // | grep product.marketname'
cmd := exec.Command("adb", command)
out, err := cmd.CombinedOutput()
if err != nil {
fmt.Println(err)
}
strs := strings.Split(string(out), "\n")
device := make(map[string]string)
for i, v := range strs {
if i == 0 || len(v) < 4 {
continue
}
_strs := strings.Split(v, "\t")
device[_strs[0]] = _strs[1]
}
db, err := dbsql.GetConn(dbsql.DSN)
if err != nil {
fmt.Println("[Crontab] 数据库连接失败")
return
}
defer dbsql.Close(db)
var list []models.Device
db.Model(models.Device{}).Where("platform = ? AND is_del = 0", "adr").Find(&list)
for _, v := range list {
if vv, ok := device[v.Udid]; ok {
// 数据表中的设备adb有获取到
switch v.Status {
case "busy":
continue
case "unauthorized":
if strings.Contains(vv, "device") {
db.Model(models.Device{}).Where("udid = ?", v.Udid).Update("status", "online")
fmt.Println("设备", v.Udid, "已连接")
}
case "offline":
if strings.Contains(vv, "device") {
db.Model(models.Device{}).Where("udid = ?", v.Udid).Update("status", "online")
fmt.Println("设备", v.Udid, "已连接")
} else {
db.Model(models.Device{}).Where("udid = ?", v.Udid).Update("status", "unauthorized")
fmt.Println("设备", v.Udid, "已连接但未认证")
}
case "online":
monkey.KeepDevice(v.Udid)
}
} else {
// 数据表中的设备adb没有获取到视为离线
if v.Status != "offline" {
db.Model(models.Device{}).Where("udid = ?", v.Udid).Update("status", "offline")
fmt.Println("设备", v.Udid, "已离线")
}
}
}
}
func CheckTaskStatus() {
db, err := dbsql.GetConn(dbsql.DSN)
if err != nil {
fmt.Println("[Crontab] 数据库连接失败")
return
}
defer dbsql.Close(db)
var list []models.MonkeyTask
db.Model(models.MonkeyTask{}).Where("status = ?", "RUNNING").Find(&list)
for i, v := range list {
// 如果无对应正在运行的容器,判断为【结束】或【异常停止】
if len(monkey.GetTaskFromDocker(v.Id)) == 0 {
// 如果预期结束时间(任务开始时间+运行时间) < 当前时间,判断任务为运行正常结束
if v.CreateTime+v.RunTime < int(time.Now().Unix()) {
list[i].Status = "FINISH"
db.Table(v.TableName()).Model(models.MonkeyTask{}).Where("id = ?", v.Id).Update("status", list[i].Status)
} else {
common.PushCorntaskLog("任务状态似乎异常task_id=" + cast.ToString(v.Id))
}
}
continue
// var count int64
// db.Model(models.MonkeyActivity{}).Where("task_id = ?", list[i].Id).Count(&count)
// list[i].CoveredAcitvities = int(count)
// var pids []models.MonkeyPid
// db.Model(models.MonkeyPid{}).Where("task_id = ?", v.Id).Find(&pids)
// if code := checkTaskPids(pids); code != 0 {
// list[i].Status = "FINISH"
// db.Model(models.MonkeyTask{}).Where("id = ?", v.Id).Update("status", list[i].Status)
// common.PushMonkeyResult(v)
// }
}
}
func checkTaskPids(pids []models.MonkeyPid) int {
if len(pids) == 0 {
return -1
}
var strs []string
for _, v := range pids {
strs = append(strs, cast.ToString(v.PId))
}
pids_str := strings.Join(strs, "|")
cmd_str := "ps -ef | grep -v -E 'bash|grep' | grep -E '" + pids_str + "'"
fmt.Println(cmd_str)
cmd := exec.Command("bash", "-c", cmd_str)
cmd.Run()
return cmd.ProcessState.ExitCode()
}