free up job

main
alex 2023-10-18 18:42:13 +02:00
parent aabca4974a
commit ad1103ebac
7 changed files with 110 additions and 27 deletions

View File

@ -124,10 +124,11 @@ func ProcessJobTask(r *structs.Robot, jobId string, jobName string) {
func UpdateRobotCurrentJob(r *structs.Robot, jobId string, jobName string) {
r.JobMutex.Lock()
defer r.JobMutex.Unlock()
/*
if r.CurrentJobId == jobId {
log.Info().Msgf("Robot %s current job id already set to %s", r.Name, jobId)
return
}
} */
r.CurrentJobId = jobId
r.CurrentJobName = jobName
@ -186,7 +187,7 @@ func RemoveJobFromJobsWaitingList(r *structs.Robot, jobId string) {
}
}
func CountDownJobsWaiting(r *structs.Robot, jobId string) {
func CountDownJobsWaiting(r *structs.Robot) {
r.JobMutex.Lock()
defer r.JobMutex.Unlock()

View File

@ -18,6 +18,7 @@ const (
RobotStatusConnecting = 3
RobotStatusError = 4
RobotStatusOffline = 5
RobotStatusWaiting = 6
)
const (

View File

@ -325,7 +325,7 @@
{
"description": "Robot id.",
"name": "robotId",
"in": "path",
"in": "params",
"required": true,
"schema": {
"type": "string"
@ -342,6 +342,40 @@
}
}
},
"/robot/fuj/{robotId}": {
"patch": {
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"robot"
],
"summary": "Free up job from robot.",
"operationId": "robotFreeUpJob",
"parameters": [
{
"description": "Robot id.",
"name": "robotId",
"in": "params",
"required": true,
"schema": {
"type": "string"
}
}
],
"responses": {
"200": {
"description": "Job freed up"
},
"400": {
"description": "Invalid robot id"
}
}
}
},
"/robot/{robotId}": {
"delete": {
"consumes": [
@ -359,7 +393,7 @@
{
"description": "Robot id.",
"name": "robotId",
"in": "path",
"in": "params",
"required": true,
"schema": {
"type": "string"
@ -513,7 +547,8 @@
"type": "object",
"properties": {
"Status": {
"type": "string"
"type": "integer",
"format": "int64"
}
},
"x-go-package": "jannex/robot-control-manager/modules/structs"

View File

@ -61,7 +61,9 @@ func ControlRex(c *fiber.Ctx) error {
return c.JSON(structs.StatusResponse{Status: utils.ResponseStatusOffline})
}
if r.Status != utils.RobotStatusIdle && r.Status != utils.RobotStatusRunning {
if r.Status != utils.RobotStatusIdle &&
r.Status != utils.RobotStatusRunning &&
r.Status != utils.RobotStatusWaiting {
return c.JSON(structs.StatusResponse{Status: utils.ResponseStatusError})
}
@ -74,7 +76,7 @@ func ControlRex(c *fiber.Ctx) error {
return c.JSON(structs.StatusResponse{Status: utils.ResponseStatusError})
}
robot.UpdateRobotStatus(r, utils.RobotStatusIdle)
robot.UpdateRobotStatus(r, utils.RobotStatusWaiting)
return c.JSON(structs.StatusResponse{Status: utils.ResponseStatusOk})
}
@ -109,8 +111,10 @@ func FinishControlRex(c *fiber.Ctx) error {
return c.SendStatus(fiber.StatusUnprocessableEntity)
}
robot.CountDownJobsWaiting(r, body.JobId)
robot.UpdateRobotCurrentJob(r, "", "")
robot.CountDownJobsWaiting(r)
logger.AddRobotLog(rslogger.LogTypeInfo, utils.RobotTypeRex, r.Id, "Finish control robot")
return c.SendStatus(fiber.StatusOK)
}

View File

@ -19,7 +19,7 @@ func SetPermitJoin(c *fiber.Ctx) error {
// description: |
// This is used to enable or disable permit join.
// parameters:
// - in: path
// - in: params
// name: enabled
// description: Enable or disable permit join. 0 = disable, 1 = enable.
// required: true

View File

@ -54,7 +54,7 @@ func FirstRequest(c *fiber.Ctx) error {
database.DB.First(&foundRobot, "id = ?", body.Id)
if !cache.IsPermitJoinEnabled() && foundRobot.Id == "" {
logger.AddSystemLog("Unauthorized robot tried to connect with id %v and type %v", body.Id, utils.GetRobotTypeString(body.Type))
logger.AddSystemLog(rslogger.LogTypeWarning, "Unauthorized robot tried to connect with id %v and type %v", body.Id, utils.GetRobotTypeString(body.Type))
return c.SendStatus(fiber.StatusForbidden)
}
@ -85,7 +85,7 @@ func FirstRequest(c *fiber.Ctx) error {
},
})
logger.AddSystemLog("Unauthorized robot connected with id %v and type %v", body.Id, utils.GetRobotTypeString(body.Type))
logger.AddSystemLog(rslogger.LogTypeInfo, "Unauthorized robot connected with id %v and type %v", body.Id, utils.GetRobotTypeString(body.Type))
} else {
newRobot := structs.Robot{
Id: body.Id,
@ -179,7 +179,7 @@ func AuthorizeRobot(c *fiber.Ctx) error {
}
if !cache.IsUnauthorizedRobotInList(params.RobotId) {
logger.AddSystemLog("Unauthorized robot with id %v not found", params.RobotId)
logger.AddSystemLog(rslogger.LogTypeWarning, "Unauthorized robot with id %v not found", params.RobotId)
return c.SendStatus(fiber.StatusUnprocessableEntity)
}
@ -208,7 +208,7 @@ func AuthorizeRobot(c *fiber.Ctx) error {
addRobotSSEMessage(&newRobot)
logger.AddSystemLog("Robot authorized with id %v and type %v", params.RobotId, utils.GetRobotTypeString(unauthorizedRobot.Type))
logger.AddSystemLog(rslogger.LogTypeInfo, "Robot authorized with id %v and type %v", params.RobotId, utils.GetRobotTypeString(unauthorizedRobot.Type))
return c.SendStatus(fiber.StatusOK)
}
@ -222,7 +222,7 @@ func DeleteRobot(c *fiber.Ctx) error {
// produces:
// - application/json
// parameters:
// - in: path
// - in: params
// name: robotId
// description: Robot id.
// required: true
@ -254,7 +254,7 @@ func DeleteRobot(c *fiber.Ctx) error {
},
})
logger.AddSystemLog("Robot deleted with id %v", params.RobotId)
logger.AddSystemLog(rslogger.LogTypeInfo, "Robot deleted with id %v", params.RobotId)
return c.SendStatus(fiber.StatusOK)
}
@ -268,7 +268,7 @@ func DenyUnauthorizedRobot(c *fiber.Ctx) error {
// produces:
// - application/json
// parameters:
// - in: path
// - in: params
// name: robotId
// description: Robot id.
// required: true
@ -299,7 +299,7 @@ func DenyUnauthorizedRobot(c *fiber.Ctx) error {
},
})
logger.AddSystemLog("Unauthorized robot denied with id %v", params.RobotId)
logger.AddSystemLog(rslogger.LogTypeInfo, "Unauthorized robot denied with id %v", params.RobotId)
return c.SendStatus(fiber.StatusOK)
}
@ -333,15 +333,10 @@ func UpdateRobot(c *fiber.Ctx) error {
return c.SendStatus(fiber.StatusBadRequest)
}
if !cache.IsRobotInList(body.RobotId) {
logger.AddSystemLog("Robot with id %v not found", body.RobotId)
return c.SendStatus(fiber.StatusUnprocessableEntity)
}
foundRobot := cache.GetRobotById(body.RobotId)
if foundRobot == nil || cache.IsRobotNameInList(body.RobotName) {
logger.AddSystemLog(rslogger.LogTypeInfo, "Robot with id %v not found", body.RobotId)
return c.SendStatus(fiber.StatusUnprocessableEntity)
}
@ -356,7 +351,53 @@ func UpdateRobot(c *fiber.Ctx) error {
Body: body,
})
logger.AddSystemLog("Robot with id %v name changed to %v", body.RobotId, body.RobotName)
logger.AddSystemLog(rslogger.LogTypeInfo, "Robot with id %v name changed to %v", body.RobotId, body.RobotName)
return c.SendStatus(fiber.StatusOK)
}
// This will be used to free up a job from a robot if it is stuck or job was not correctly finished
func FreeUpJob(c *fiber.Ctx) error {
// swagger:operation PATCH /robot/fuj/{robotId} robot robotFreeUpJob
// ---
// summary: Free up job from robot.
// consumes:
// - application/json
// produces:
// - application/json
// parameters:
// - in: params
// name: robotId
// description: Robot id.
// required: true
// schema:
// type: string
// responses:
// "200":
// description: Job freed up
// "400":
// description: Invalid request body
// "422":
// description: Invalid robot id
var params structs.RobotIdParams
if err := rsutils.ParamsParserHelper(c, &params); err != nil {
return c.SendStatus(fiber.StatusBadRequest)
}
foundRobot := cache.GetRobotById(params.RobotId)
if foundRobot == nil {
logger.AddSystemLog(rslogger.LogTypeWarning, "Robot %v not found. Cannot free ub job.", params.RobotId)
return c.SendStatus(fiber.StatusUnprocessableEntity)
}
robot.UpdateRobotCurrentJob(foundRobot, "", "")
robot.UpdateRobotStatus(foundRobot, utils.RobotStatusIdle)
robot.CountDownJobsWaiting(foundRobot)
logger.AddSystemLog(rslogger.LogTypeInfo, "Job freed up from robot %s", params.RobotId)
return c.SendStatus(fiber.StatusOK)
}

View File

@ -19,6 +19,7 @@ func SetupRoutes(app *fiber.App) {
r.Delete("/:robotId", robot.DeleteRobot)
r.Delete("/deny/:robotId", robot.DenyUnauthorizedRobot)
r.Patch("/", robot.UpdateRobot)
r.Patch("/fuj/:robotId", robot.FreeUpJob)
rs := v1.Group("/robots")
rs.Get("/", robots.GetRobots)