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.

76 lines
2.7 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"
"strings"
"time"
"github.com/spf13/cast"
)
func CheckMonkeyTasks() {
db, err := dbsql.GetConn(dbsql.DSN)
if err != nil {
return
}
defer dbsql.Close(db)
var list []models.MonkeyTask
db.Model(models.MonkeyTask{}).Where("status = ? AND is_del = 0", "WAITTING").Find(&list)
// 运行等待中的任务
for _, task := range list {
if task.Platform == "adr" {
product_name := task.Product
if strings.Contains(task.Product, "-") {
product_name = strings.Split(task.Product, "-")[0]
}
var device models.Device
db.Table("device").Model(models.Device{}).Where("project = ? AND product_name = ? AND platform = ? AND status = ?", task.Project, product_name, task.Platform, "online").First(&device)
if device.ID < 1 {
// 没有空闲设备
continue
}
db.Table("device").Model(models.Device{}).Where("udid = ?", device.Udid).Update("status", "busy")
go monkey.RunAndroidMonkeyCmd(task, device.Udid)
common.PushCorntaskLog("执行Monkey任务" + task.Project + "-" + device.Udid)
} else if task.Platform == "ios" {
product_name := task.Product
if strings.Contains(task.Product, "-") {
product_name = strings.Split(task.Product, "-")[0]
}
var device models.Device
db.Table("device").Model(models.Device{}).Where("project = ? AND product_name = ? AND platform = ? AND status = ?", task.Project, product_name, task.Platform, "online").First(&device)
if device.ID < 1 {
// 没有空闲设备
continue
}
db.Table("device").Model(models.Device{}).Where("udid = ?", device.Udid).Update("status", "busy")
// go monkey.RunAndroidMonkeyCmd(task, device.Udid)
go monkey.RuniOSMonkeyByDocker(task, device.Udid)
common.PushCorntaskLog("执行Monkey任务" + task.Project + "-" + device.Udid)
}
}
// 判断运行中的任务的实际状态
db.Model(models.MonkeyTask{}).Where("status = ? AND is_del = 0", "RUNNING").Find(&list)
for i, v := range list {
if v.Platform == "adr" {
// 如果无对应正在运行的容器,判断为【结束】或【异常停止】
if len(monkey.GetTaskFromDocker(v.Id)) == 0 {
// 如果预期结束时间(任务开始时间+运行时间) < 当前时间,判断任务为运行正常结束
if v.UpdateTime+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)
common.PushMonkeyResult(v)
} else {
common.PushCorntaskLog("任务状态似乎异常task_id=" + cast.ToString(v.Id))
}
}
}
}
}