From af58edff0d406e3af23f6a5bc6f7c1548e58eb65 Mon Sep 17 00:00:00 2001 From: alex Date: Tue, 17 Oct 2023 19:56:27 +0200 Subject: [PATCH] logging --- modules/logger/logger.go | 13 +++++++++++-- modules/robot/robot.go | 7 +++++++ routers/api/v1/control/control.go | 7 +++++-- routers/api/v1/robot/robot.go | 2 +- testclient/testrobot.py | 4 ++-- 5 files changed, 26 insertions(+), 7 deletions(-) diff --git a/modules/logger/logger.go b/modules/logger/logger.go index 417e38a..27bed0d 100644 --- a/modules/logger/logger.go +++ b/modules/logger/logger.go @@ -2,6 +2,7 @@ package logger import ( "fmt" + "jannex/robot-control-manager/modules/utils" "git.ex.umbach.dev/Alex/roese-utils/rslogger" "github.com/gofiber/fiber/v2" @@ -13,8 +14,16 @@ func AddSystemLog(format string, v ...any) { Logs: []string{"I " + rslogger.GetTime() + fmt.Sprintf(format, v...)}}) } -func AddRexLog(rexId, format string, v ...any) { +func AddRobotLog(robotType uint8, robotId string, format string, v ...any) { + var logType string + + if robotType == utils.RobotTypeRex { + logType = "rex" + } else { + logType = "yeet" + } + go rslogger.LogManagerRequestClient(fiber.MethodPost, rslogger.LogManagerRequestBody{ - Type: "rex-" + rexId, + Type: logType + "-" + robotId, Logs: []string{"I " + rslogger.GetTime() + fmt.Sprintf(format, v...)}}) } diff --git a/modules/robot/robot.go b/modules/robot/robot.go index 81e6df3..a8313e7 100644 --- a/modules/robot/robot.go +++ b/modules/robot/robot.go @@ -103,6 +103,13 @@ func ProcessJobTask(r *structs.Robot, jobId string, jobName string) { for r.CurrentJobId != "" && r.CurrentJobId != jobId { // wait for current job to finish + + if r.Status == utils.RobotStatusOffline { + log.Info().Msgf("Robot %s offline. Job with id %s and name %s canceled", r.Name, jobId, jobName) + logger.AddRobotLog(r.Type, r.Id, "Job with id %s and name %s canceled", jobId, jobName) + return + } + fmt.Println("job is processing", r.CurrentJobId, jobId, r.JobsWaitingCount) time.Sleep(2 * time.Second) } diff --git a/routers/api/v1/control/control.go b/routers/api/v1/control/control.go index 167be20..b856cfe 100644 --- a/routers/api/v1/control/control.go +++ b/routers/api/v1/control/control.go @@ -2,6 +2,7 @@ package control import ( "jannex/robot-control-manager/modules/cache" + "jannex/robot-control-manager/modules/logger" "jannex/robot-control-manager/modules/request" "jannex/robot-control-manager/modules/robot" "jannex/robot-control-manager/modules/structs" @@ -60,12 +61,14 @@ func ControlRex(c *fiber.Ctx) error { } if r.Status != utils.RobotStatusIdle && r.Status != utils.RobotStatusRunning { - - } + return c.JSON(structs.StatusResponse{Status: utils.ResponseStatusError}) + } robot.ProcessJobTask(r, body.JobId, body.JobName) robot.UpdateRobotStatus(r, utils.RobotStatusRunning) + logger.AddRobotLog(utils.RobotTypeRex, r.Id, "Control robot with task: %v", rsutils.MarshalJson(body.Task)) + if err := request.Request(fiber.MethodPost, r.Address+":5000/api/v1/control", body.Task); err != nil { return c.JSON(structs.StatusResponse{Status: utils.ResponseStatusError}) } diff --git a/routers/api/v1/robot/robot.go b/routers/api/v1/robot/robot.go index f26eb9f..f4a50c9 100644 --- a/routers/api/v1/robot/robot.go +++ b/routers/api/v1/robot/robot.go @@ -111,7 +111,7 @@ func FirstRequest(c *fiber.Ctx) error { addRobotSSEMessage(&newRobot) - logger.AddSystemLog("Robot connected with id %v and type %v", body.Id, utils.GetRobotTypeString(body.Type)) + logger.AddRobotLog(newRobot.Type, newRobot.Id, "Robot connected") } return c.JSON(structs.StatusResponse{Status: utils.ResponseStatusOk}) diff --git a/testclient/testrobot.py b/testclient/testrobot.py index 1d646d2..ddae43e 100644 --- a/testclient/testrobot.py +++ b/testclient/testrobot.py @@ -42,7 +42,7 @@ for i in range(1): RexRobot("762") RexRobot("763") RexRobot("764") - RexRobot("765") + RexRobot("770") @@ -69,4 +69,4 @@ def ping(): return jsonify({'status': 'ok'}) if __name__ == '__main__': - app.run(debug=False, port=5003) \ No newline at end of file + app.run(debug=False, port=5000) \ No newline at end of file