From d5756a4ec8795378bc2e629c565df6a76f0379ae Mon Sep 17 00:00:00 2001 From: Cyril Galibern Date: Thu, 22 Jan 2026 18:13:21 +0100 Subject: [PATCH] [worker, cdb] Fix action ID lookup to use session UUID - Replace `FindActionID` with `FindInstanceActionIDFromSID` for action lookup based on session UUID (`sid`). - Adjust related logic in `jobFeedInstanceAction` to handle new function output, including `found` flag for record existence checks. --- cdb/db_actions.go | 20 ++++++++++++-------- worker/job_feed_instance_action.go | 6 +++--- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/cdb/db_actions.go b/cdb/db_actions.go index a19f539..2992f4f 100644 --- a/cdb/db_actions.go +++ b/cdb/db_actions.go @@ -3,6 +3,7 @@ package cdb import ( "context" "database/sql" + "errors" "fmt" "time" @@ -261,15 +262,18 @@ func (oDb *DB) UpdateSvcAction(ctx context.Context, svcActionID int64, end time. return nil } -// FindActionID finds the action ID for the given parameters. -func (oDb *DB) FindActionID(ctx context.Context, nodeID string, svcID string, begin time.Time, action string) (int64, error) { - // todo : check if there is only one result - const query = "SELECT id FROM svcactions WHERE node_id = ? AND svc_id = ? AND begin = ? AND action = ? AND pid IS NULL" - var id int64 - if err := oDb.DB.QueryRowContext(ctx, query, nodeID, svcID, begin, action).Scan(&id); err != nil { - return 0, err +// FindInstanceActionIDFromSID finds the instance action ID from the sid. +func (oDb *DB) FindInstanceActionIDFromSID(ctx context.Context, nodeID string, svcID string, sid string) (id int64, found bool, err error) { + const query = "SELECT id FROM svcactions WHERE node_id = ? AND svc_id = ? AND sid = ?" + err = oDb.DB.QueryRowContext(ctx, query, nodeID, svcID, sid).Scan(&id) + if err != nil { + if errors.Is(err, sql.ErrNoRows) { + err = nil + } + } else { + found = true } - return id, nil + return } // UpdateActionErrors updates the action errors in the database. diff --git a/worker/job_feed_instance_action.go b/worker/job_feed_instance_action.go index 2228fa9..daeb4a1 100644 --- a/worker/job_feed_instance_action.go +++ b/worker/job_feed_instance_action.go @@ -138,19 +138,19 @@ func (d *jobFeedInstanceAction) updateDB(ctx context.Context) error { return fmt.Errorf("invalid end time format: %w", err) } - actionId, err := d.oDb.FindActionID(ctx, d.nodeID, d.objectID, beginTime, d.data.Action) + actionID, found, err := d.oDb.FindInstanceActionIDFromSID(ctx, d.nodeID, d.objectID, d.data.SessionUuid) if err != nil { return fmt.Errorf("find action ID failed: %w", err) } - if actionId == 0 { + if !found { // begin not processed yet, insert full record if _, err := d.oDb.InsertSvcAction(ctx, objectUUID, nodeUUID, d.data.Action, beginTime, statusLog, d.data.SessionUuid, d.data.Cron, endTime, d.data.Status); err != nil { return fmt.Errorf("insert svc action failed: %w", err) } } else { // begin already processed, update record with end info - if err := d.oDb.UpdateSvcAction(ctx, actionId, endTime, d.data.Status, statusLog); err != nil { + if err := d.oDb.UpdateSvcAction(ctx, actionID, endTime, d.data.Status, statusLog); err != nil { return fmt.Errorf("end svc action failed: %w", err) } }