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.

126 lines
3.4 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"
"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 * * * ?", CheckTaskStatus)
crontab.AddFunc("*/15 * * * * ?", CheckMonkeyTasks)
// crontab.AddFunc("*/10 * * * * ?", GetBugStatusDaily) */1 * * * *
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("is_del = 0").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 {
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()
}