jobs waiting
parent
e13598dfda
commit
e2c68c8c69
|
@ -48,10 +48,9 @@ func GetAllRobots(query rspagination.PageQuery) structs.RobotsResponse {
|
||||||
Name: v.Name,
|
Name: v.Name,
|
||||||
Status: v.Status,
|
Status: v.Status,
|
||||||
Address: v.Address,
|
Address: v.Address,
|
||||||
CurrentJobId: v.CurrentJobId,
|
|
||||||
CurrentJobName: v.CurrentJobName,
|
CurrentJobName: v.CurrentJobName,
|
||||||
JobsWaitingCount: v.JobsWaitingCount,
|
JobsWaitingCount: v.JobsWaitingCount,
|
||||||
JobsWaitingNameList: v.JobsWaitingNameList,
|
JobsWaitingNameList: utils.GetJobsWaitingNameList(v.JobsWaitingNameList),
|
||||||
FirmwareVersion: v.FirmwareVersion,
|
FirmwareVersion: v.FirmwareVersion,
|
||||||
ConnectedAt: v.ConnectedAt,
|
ConnectedAt: v.ConnectedAt,
|
||||||
CreatedAt: v.CreatedAt,
|
CreatedAt: v.CreatedAt,
|
||||||
|
|
|
@ -2,6 +2,7 @@ package robot
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"jannex/robot-control-manager/modules/cache"
|
"jannex/robot-control-manager/modules/cache"
|
||||||
"jannex/robot-control-manager/modules/database"
|
"jannex/robot-control-manager/modules/database"
|
||||||
"jannex/robot-control-manager/modules/logger"
|
"jannex/robot-control-manager/modules/logger"
|
||||||
|
@ -91,6 +92,114 @@ func UpdateRobotStatus(robot *structs.Robot, status uint8) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ProcessJobTask(r *structs.Robot, jobId string, jobName string) {
|
||||||
|
if r.CurrentJobId == "" {
|
||||||
|
UpdateRobotCurrentJob(r, jobId, jobName)
|
||||||
|
}
|
||||||
|
|
||||||
|
if r.CurrentJobId != "" && r.CurrentJobId != jobId {
|
||||||
|
CountUpJobsWaiting(r, jobId, jobName)
|
||||||
|
}
|
||||||
|
|
||||||
|
for r.CurrentJobId != "" && r.CurrentJobId != jobId {
|
||||||
|
// wait for current job to finish
|
||||||
|
fmt.Println("job is processing", r.CurrentJobId, jobId, r.JobsWaitingCount)
|
||||||
|
time.Sleep(2 * time.Second)
|
||||||
|
}
|
||||||
|
|
||||||
|
RemoveJobFromJobsWaitingList(r, jobId)
|
||||||
|
UpdateRobotCurrentJob(r, jobId, jobName)
|
||||||
|
|
||||||
|
fmt.Println("processing job", jobId)
|
||||||
|
}
|
||||||
|
|
||||||
|
func UpdateRobotCurrentJob(r *structs.Robot, jobId string, jobName string) {
|
||||||
|
r.JobMutex.Lock()
|
||||||
|
defer r.JobMutex.Unlock()
|
||||||
|
|
||||||
|
if r.CurrentJobId == jobId {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
r.CurrentJobId = jobId
|
||||||
|
r.CurrentJobName = jobName
|
||||||
|
|
||||||
|
database.DB.Model(&structs.Robot{}).
|
||||||
|
Where("id = ?", r.Id).
|
||||||
|
Updates(map[string]interface{}{"current_job_id": jobId, "current_job_name": jobName})
|
||||||
|
|
||||||
|
log.Info().Msgf("Robot %s current job id updated to %s", r.Name, jobId)
|
||||||
|
|
||||||
|
BroadcastSSEMessage(structs.SSEMessage{
|
||||||
|
Cmd: utils.SSESentCmdUpdateRobotCurrentJob,
|
||||||
|
Body: struct {
|
||||||
|
RobotId string
|
||||||
|
JobName string
|
||||||
|
JobsWaitingNameList []string
|
||||||
|
}{
|
||||||
|
RobotId: r.Id,
|
||||||
|
JobName: jobName,
|
||||||
|
JobsWaitingNameList: utils.GetJobsWaitingNameList(r.JobsWaitingNameList),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func CountUpJobsWaiting(r *structs.Robot, jobId string, jobName string) {
|
||||||
|
r.JobMutex.Lock()
|
||||||
|
defer r.JobMutex.Unlock()
|
||||||
|
|
||||||
|
r.JobsWaitingCount++
|
||||||
|
|
||||||
|
r.JobsWaitingNameList = append(r.JobsWaitingNameList, structs.JobWaitingName{
|
||||||
|
JobId: jobId,
|
||||||
|
Name: jobName,
|
||||||
|
})
|
||||||
|
|
||||||
|
BroadcastSSEMessage(structs.SSEMessage{
|
||||||
|
Cmd: utils.SSESentCmdUpdateRobotJobsWaitingCount,
|
||||||
|
Body: struct {
|
||||||
|
RobotId string
|
||||||
|
JobsWaitingCount int
|
||||||
|
JobsWaitingNameList []string
|
||||||
|
}{
|
||||||
|
RobotId: r.Id,
|
||||||
|
JobsWaitingCount: r.JobsWaitingCount,
|
||||||
|
JobsWaitingNameList: utils.GetJobsWaitingNameList(r.JobsWaitingNameList),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func RemoveJobFromJobsWaitingList(r *structs.Robot, jobId string) {
|
||||||
|
for i, j := range r.JobsWaitingNameList {
|
||||||
|
if j.JobId == jobId {
|
||||||
|
r.JobsWaitingNameList = append(r.JobsWaitingNameList[:i], r.JobsWaitingNameList[i+1:]...)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func CountDownJobsWaiting(r *structs.Robot, jobId string) {
|
||||||
|
r.JobMutex.Lock()
|
||||||
|
defer r.JobMutex.Unlock()
|
||||||
|
|
||||||
|
if r.JobsWaitingCount > 0 {
|
||||||
|
r.JobsWaitingCount--
|
||||||
|
}
|
||||||
|
|
||||||
|
BroadcastSSEMessage(structs.SSEMessage{
|
||||||
|
Cmd: utils.SSESentCmdUpdateRobotJobsWaitingCount,
|
||||||
|
Body: struct {
|
||||||
|
RobotId string
|
||||||
|
JobsWaitingCount int
|
||||||
|
JobsWaitingNameList []string
|
||||||
|
}{
|
||||||
|
RobotId: r.Id,
|
||||||
|
JobsWaitingCount: r.JobsWaitingCount,
|
||||||
|
JobsWaitingNameList: utils.GetJobsWaitingNameList(r.JobsWaitingNameList),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func BroadcastSSEMessage(message structs.SSEMessage) {
|
func BroadcastSSEMessage(message structs.SSEMessage) {
|
||||||
marshaledMessage, err := json.Marshal(message)
|
marshaledMessage, err := json.Marshal(message)
|
||||||
|
|
||||||
|
@ -106,3 +215,24 @@ func BroadcastSSEMessage(message structs.SSEMessage) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
func AddJobNameToJobsWaitingList(r *structs.Robot, jobName string) {
|
||||||
|
r.JobMutex.Lock()
|
||||||
|
defer r.JobMutex.Unlock()
|
||||||
|
|
||||||
|
r.JobsWaitingNameList = append(r.JobsWaitingNameList, jobName)
|
||||||
|
}
|
||||||
|
|
||||||
|
func RemoveJobNameFromJobsWaitingList(r *structs.Robot, jobName string) {
|
||||||
|
r.JobMutex.Lock()
|
||||||
|
defer r.JobMutex.Unlock()
|
||||||
|
|
||||||
|
for i, j := range r.JobsWaitingNameList {
|
||||||
|
if j == jobName {
|
||||||
|
r.JobsWaitingNameList = append(r.JobsWaitingNameList[:i], r.JobsWaitingNameList[i+1:]...)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package structs
|
package structs
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
@ -18,19 +17,23 @@ type Robot struct {
|
||||||
CurrentJobName string
|
CurrentJobName string
|
||||||
JobMutex sync.Mutex `gorm:"-"`
|
JobMutex sync.Mutex `gorm:"-"`
|
||||||
JobsWaitingCount int `gorm:"-"`
|
JobsWaitingCount int `gorm:"-"`
|
||||||
JobsWaitingNameList []string `gorm:"-"`
|
JobsWaitingNameList []JobWaitingName `gorm:"-"`
|
||||||
LastTaskAt time.Time `gorm:"-"`
|
LastTaskAt time.Time `gorm:"-"`
|
||||||
ConnectedAt time.Time `gorm:"-"`
|
ConnectedAt time.Time `gorm:"-"`
|
||||||
CreatedAt time.Time
|
CreatedAt time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type JobWaitingName struct {
|
||||||
|
JobId string
|
||||||
|
Name string
|
||||||
|
}
|
||||||
|
|
||||||
type APIRobot struct {
|
type APIRobot struct {
|
||||||
Id string
|
Id string
|
||||||
Type uint8
|
Type uint8
|
||||||
Name string
|
Name string
|
||||||
Status uint8
|
Status uint8
|
||||||
Address string
|
Address string
|
||||||
CurrentJobId string
|
|
||||||
CurrentJobName string
|
CurrentJobName string
|
||||||
JobsWaitingCount int
|
JobsWaitingCount int
|
||||||
JobsWaitingNameList []string
|
JobsWaitingNameList []string
|
||||||
|
@ -39,13 +42,14 @@ type APIRobot struct {
|
||||||
CreatedAt time.Time
|
CreatedAt time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
func (r *Robot) CountUpJobsWaiting() {
|
func (r *Robot) CountUpJobsWaiting() {
|
||||||
r.JobMutex.Lock()
|
r.JobMutex.Lock()
|
||||||
defer r.JobMutex.Unlock()
|
defer r.JobMutex.Unlock()
|
||||||
|
|
||||||
r.JobsWaitingCount++
|
r.JobsWaitingCount++
|
||||||
}
|
} */
|
||||||
|
/*
|
||||||
func (r *Robot) CountDownJobsWaiting() {
|
func (r *Robot) CountDownJobsWaiting() {
|
||||||
r.JobMutex.Lock()
|
r.JobMutex.Lock()
|
||||||
defer r.JobMutex.Unlock()
|
defer r.JobMutex.Unlock()
|
||||||
|
@ -54,12 +58,13 @@ func (r *Robot) CountDownJobsWaiting() {
|
||||||
r.JobsWaitingCount--
|
r.JobsWaitingCount--
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
/*
|
||||||
func (r *Robot) ProcessJobTask(jobId string) {
|
func (r *Robot) ProcessJobTask(jobId string) {
|
||||||
fmt.Println("wait for job", jobId)
|
fmt.Println("wait for job", jobId)
|
||||||
|
|
||||||
if r.CurrentJobId == "" {
|
if r.CurrentJobId == "" {
|
||||||
r.SetCurrentJobId(jobId)
|
robot.UpdateRobotCurrentJobId(r, jobId)
|
||||||
}
|
}
|
||||||
|
|
||||||
if r.CurrentJobId != "" && r.CurrentJobId != jobId {
|
if r.CurrentJobId != "" && r.CurrentJobId != jobId {
|
||||||
|
@ -72,17 +77,18 @@ func (r *Robot) ProcessJobTask(jobId string) {
|
||||||
time.Sleep(2 * time.Second)
|
time.Sleep(2 * time.Second)
|
||||||
}
|
}
|
||||||
|
|
||||||
r.SetCurrentJobId(jobId)
|
robot.UpdateRobotCurrentJobId(r, jobId)
|
||||||
|
|
||||||
fmt.Println("processing job", jobId)
|
fmt.Println("processing job", jobId)
|
||||||
}
|
} */
|
||||||
|
|
||||||
|
/*
|
||||||
func (r *Robot) SetCurrentJobId(jobId string) {
|
func (r *Robot) SetCurrentJobId(jobId string) {
|
||||||
r.JobMutex.Lock()
|
r.JobMutex.Lock()
|
||||||
defer r.JobMutex.Unlock()
|
defer r.JobMutex.Unlock()
|
||||||
|
|
||||||
r.CurrentJobId = jobId
|
r.CurrentJobId = jobId
|
||||||
}
|
} */
|
||||||
|
|
||||||
type UnauthorizedRobot struct {
|
type UnauthorizedRobot struct {
|
||||||
Id string
|
Id string
|
||||||
|
|
|
@ -30,6 +30,8 @@ const (
|
||||||
SSESentCmdRemoveUnauthorizedRobot = 4
|
SSESentCmdRemoveUnauthorizedRobot = 4
|
||||||
SSESentCmdRemoveRobot = 5
|
SSESentCmdRemoveRobot = 5
|
||||||
SSESentCmdRobotUpdated = 6
|
SSESentCmdRobotUpdated = 6
|
||||||
|
SSESentCmdUpdateRobotCurrentJob = 7
|
||||||
|
SSESentCmdUpdateRobotJobsWaitingCount = 8
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package utils
|
package utils
|
||||||
|
|
||||||
|
import "jannex/robot-control-manager/modules/structs"
|
||||||
|
|
||||||
func GetRobotTypeString(t uint8) string {
|
func GetRobotTypeString(t uint8) string {
|
||||||
switch t {
|
switch t {
|
||||||
case RobotTypeRex:
|
case RobotTypeRex:
|
||||||
|
@ -27,3 +29,13 @@ func GetRobotStatusString(s uint8) string {
|
||||||
return "unknown"
|
return "unknown"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetJobsWaitingNameList(list []structs.JobWaitingName) []string {
|
||||||
|
var jobsWaitingNameList []string
|
||||||
|
|
||||||
|
for _, j := range list {
|
||||||
|
jobsWaitingNameList = append(jobsWaitingNameList, j.Name)
|
||||||
|
}
|
||||||
|
|
||||||
|
return jobsWaitingNameList
|
||||||
|
}
|
||||||
|
|
|
@ -3,7 +3,9 @@ package control
|
||||||
import (
|
import (
|
||||||
"jannex/robot-control-manager/modules/cache"
|
"jannex/robot-control-manager/modules/cache"
|
||||||
"jannex/robot-control-manager/modules/request"
|
"jannex/robot-control-manager/modules/request"
|
||||||
|
"jannex/robot-control-manager/modules/robot"
|
||||||
"jannex/robot-control-manager/modules/structs"
|
"jannex/robot-control-manager/modules/structs"
|
||||||
|
"jannex/robot-control-manager/modules/utils"
|
||||||
|
|
||||||
"git.ex.umbach.dev/Alex/roese-utils/rsutils"
|
"git.ex.umbach.dev/Alex/roese-utils/rsutils"
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
|
@ -47,18 +49,21 @@ func ControlRex(c *fiber.Ctx) error {
|
||||||
fmt.Printf("y-value: %d\n", *body.Task.Y)
|
fmt.Printf("y-value: %d\n", *body.Task.Y)
|
||||||
} */
|
} */
|
||||||
|
|
||||||
robot := cache.GetRobotByName(body.RobotName)
|
r := cache.GetRobotByName(body.RobotName)
|
||||||
|
|
||||||
if robot == nil {
|
if r == nil {
|
||||||
return c.SendStatus(fiber.StatusUnprocessableEntity)
|
return c.SendStatus(fiber.StatusUnprocessableEntity)
|
||||||
}
|
}
|
||||||
|
|
||||||
robot.ProcessJobTask(body.JobId)
|
robot.ProcessJobTask(r, body.JobId, body.JobName)
|
||||||
|
robot.UpdateRobotStatus(r, utils.RobotStatusRunning)
|
||||||
|
|
||||||
if err := request.Request(fiber.MethodPost, robot.Address+":5000/api/v1/control", body.Task); err != nil {
|
if err := request.Request(fiber.MethodPost, r.Address+":5000/api/v1/control", body.Task); err != nil {
|
||||||
return c.JSON(structs.StatusResponse{Status: "err"})
|
return c.JSON(structs.StatusResponse{Status: "err"})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
robot.UpdateRobotStatus(r, utils.RobotStatusIdle)
|
||||||
|
|
||||||
return c.JSON(structs.StatusResponse{Status: "ok"})
|
return c.JSON(structs.StatusResponse{Status: "ok"})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -86,13 +91,14 @@ func FinishControlRex(c *fiber.Ctx) error {
|
||||||
return c.SendStatus(fiber.StatusBadRequest)
|
return c.SendStatus(fiber.StatusBadRequest)
|
||||||
}
|
}
|
||||||
|
|
||||||
robot := cache.GetRobotByName(body.RobotName)
|
r := cache.GetRobotByName(body.RobotName)
|
||||||
|
|
||||||
if robot == nil || robot.CurrentJobId != body.JobId {
|
if r == nil || r.CurrentJobId != body.JobId {
|
||||||
return c.SendStatus(fiber.StatusUnprocessableEntity)
|
return c.SendStatus(fiber.StatusUnprocessableEntity)
|
||||||
}
|
}
|
||||||
|
|
||||||
robot.SetCurrentJobId("")
|
robot.CountDownJobsWaiting(r, body.JobId)
|
||||||
|
robot.UpdateRobotCurrentJob(r, "", "")
|
||||||
|
|
||||||
return c.SendStatus(fiber.StatusOK)
|
return c.SendStatus(fiber.StatusOK)
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,7 +35,7 @@ def hello():
|
||||||
|
|
||||||
print("controlling robot", body)
|
print("controlling robot", body)
|
||||||
|
|
||||||
time.sleep(15)
|
time.sleep(3)
|
||||||
|
|
||||||
|
|
||||||
print("robot controlled")
|
print("robot controlled")
|
||||||
|
|
Loading…
Reference in New Issue