Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix POST /oc3/daemon/system #26

Merged
merged 3 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions cache/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ package cache
var (
clientKey = "redisClient"

KeySystem = "oc3:q:system"
KeySystemHash = "oc3:h:system"
KeyGeneric = "oc3:q:generic"
KeyGeneric = "oc3:q:generic"

KeyDaemonPing = "oc3:q:daemon_ping"
KeyDaemonPingPending = "oc3:h:daemon_ping_pending"
KeyDaemonStatusHash = "oc3:h:daemon_status"
KeyDaemonStatusChangesHash = "oc3:h:daemon_status_changes"
KeyDaemonStatus = "oc3:q:daemon_status"
KeyDaemonStatusPending = "oc3:h:daemon_status_pending"
KeyDaemonSystem = "oc3:q:daemon_system"
KeyDaemonSystemHash = "oc3:h:daemon_system"
KeyDaemonSystemPending = "oc3:h:daemon_system_pending"
KeyPackagesHash = "oc3:h:packages"
KeyPackages = "oc3:q:packages"
KeyPackagesPending = "oc3:h:packages_pending"
Expand Down
37 changes: 10 additions & 27 deletions handlers/post_daemon_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@ package handlers
import (
"fmt"
"io"
"log/slog"
"net/http"

"github.com/go-redis/redis/v8"
"github.com/labstack/echo/v4"

"github.com/opensvc/oc3/cache"
)

func (a *Api) PostDaemonSystem(c echo.Context) error {
key := nodeIDFromContext(c)
if key == "" {
log := getLog(c)
nodeID := nodeIDFromContext(c)
if nodeID == "" {
return JSONNodeAuthProblem(c)
}

Expand All @@ -25,33 +24,17 @@ func (a *Api) PostDaemonSystem(c echo.Context) error {

reqCtx := c.Request().Context()

s := fmt.Sprintf("HSET %s %s", cache.KeySystemHash, key)
slog.Info(s)
if _, err := a.Redis.HSet(reqCtx, cache.KeySystemHash, key, string(b)).Result(); err != nil {
s := fmt.Sprintf("HSET %s %s", cache.KeyDaemonSystemHash, nodeID)
log.Info(s)
if _, err := a.Redis.HSet(reqCtx, cache.KeyDaemonSystemHash, nodeID, string(b)).Result(); err != nil {
s = fmt.Sprintf("%s: %s", s, err)
slog.Error(s)
log.Error(s)
return JSONProblem(c, http.StatusInternalServerError, "", s)
}

s = fmt.Sprintf("LPOS %s %s", cache.KeySystem, key)
slog.Info(s)
if _, err := a.Redis.LPos(reqCtx, cache.KeySystem, key, redis.LPosArgs{}).Result(); err != nil {
switch err {
case nil:
case redis.Nil:
s = fmt.Sprintf("LPUSH %s %s", cache.KeySystem, key)
slog.Info(s)
if _, err := a.Redis.LPush(reqCtx, cache.KeySystem, key).Result(); err != nil {
s := fmt.Sprintf("%s: %s", s, err)
slog.Error(s)
return JSONProblemf(c, http.StatusInternalServerError, "", "%s", s)
}
default:
s := fmt.Sprintf("%s: %s", s, err)
slog.Error(s)
return JSONProblemf(c, http.StatusInternalServerError, "", "%s", s)
}

if err := a.pushNotPending(reqCtx, cache.KeyDaemonSystemPending, cache.KeyDaemonSystem, nodeID); err != nil {
log.Error(fmt.Sprintf("can't push %s %s: %s", cache.KeyDaemonSystem, nodeID, err))
return JSONProblemf(c, http.StatusInternalServerError, "redis operation", "can't push %s %s: %s", cache.KeyDaemonSystem, nodeID, err)
}

return c.NoContent(http.StatusNoContent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (t *Worker) handleDaemonPing(nodeID string) error {

func (d *daemonPing) dropPending() error {
if err := d.redis.HDel(d.ctx, cache.KeyDaemonPingPending, d.nodeID).Err(); err != nil {
return fmt.Errorf("dropPending: HDEL %s %s: %w", cache.KeyDaemonStatusPending, d.nodeID, err)
return fmt.Errorf("dropPending: HDEL %s %s: %w", cache.KeyDaemonPingPending, d.nodeID, err)
}
return nil
}
Expand Down
File renamed without changes.
7 changes: 6 additions & 1 deletion worker/worker_system.go → worker/job_daemon_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/go-redis/redis/v8"

"github.com/opensvc/oc3/cache"
"github.com/opensvc/oc3/mariadb"
)
Expand Down Expand Up @@ -363,7 +364,11 @@ func (t *Worker) handleSystem(nodeID string) error {
ctx := context.Background()
ctx, _ = context.WithTimeout(ctx, time.Second*5)

cmd := t.Redis.HGet(ctx, cache.KeySystemHash, nodeID)
if err := t.Redis.HDel(ctx, cache.KeyDaemonSystemPending, nodeID).Err(); err != nil {
return fmt.Errorf("dropPending: HDEL %s %s: %w", cache.KeyDaemonSystemPending, nodeID, err)
}

cmd := t.Redis.HGet(ctx, cache.KeyDaemonSystemHash, nodeID)
result, err := cmd.Result()
switch err {
case nil:
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (t *Worker) Run() error {
case cache.KeyDaemonPing:
workType = "daemonPing"
err = t.handleDaemonPing(result[1])
case cache.KeySystem:
case cache.KeyDaemonSystem:
workType = "daemonSystem"
err = t.handleSystem(result[1])
case cache.KeyDaemonStatus:
Expand Down
Loading