Skip to content
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
20 changes: 12 additions & 8 deletions cdb/db_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cdb
import (
"context"
"database/sql"
"errors"
"fmt"
"time"

Expand Down Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions worker/job_feed_instance_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
Loading