54 lines
1.9 KiB
Go
54 lines
1.9 KiB
Go
package store
|
|
|
|
import (
|
|
"database/sql"
|
|
"errors"
|
|
|
|
agentv1 "git.ipao.vip/rogee/go-sip/gen/agent/v1"
|
|
"google.golang.org/protobuf/proto"
|
|
)
|
|
|
|
type PendingTaskControl struct {
|
|
TenantID, CommandID, ExecutionID, AgentID, Action, Policy string
|
|
ExpectedRevision int64
|
|
Binding *agentv1.ExecutionBinding
|
|
}
|
|
|
|
// PendingTaskControls includes unroutable targets rather than silently dropping
|
|
// them through an inner join. Missing assignment is an explicit recovery error.
|
|
func (s *Store) PendingTaskControls(limit int) ([]PendingTaskControl, error) {
|
|
if limit <= 0 {
|
|
return nil, errors.New("control batch limit must be positive")
|
|
}
|
|
rows, err := s.db.Query(`SELECT c.tenant_id,c.command_id,t.execution_id,c.action,c.active_call_policy,c.expected_revision,a.agent_id,a.binding
|
|
FROM mq_task_controls c JOIN mq_task_control_targets t ON t.tenant_id=c.tenant_id AND t.command_id=c.command_id
|
|
LEFT JOIN execution_agents a ON a.execution_id=t.execution_id
|
|
WHERE c.state='pending' AND t.applied=0 ORDER BY c.rowid,t.execution_id LIMIT ?`, limit)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var result []PendingTaskControl
|
|
for rows.Next() {
|
|
var item PendingTaskControl
|
|
var agentID sql.NullString
|
|
var raw []byte
|
|
if err := rows.Scan(&item.TenantID, &item.CommandID, &item.ExecutionID, &item.Action, &item.Policy, &item.ExpectedRevision, &agentID, &raw); err != nil {
|
|
return nil, err
|
|
}
|
|
if agentID.Valid {
|
|
item.AgentID = agentID.String
|
|
item.Binding = &agentv1.ExecutionBinding{}
|
|
if err := proto.Unmarshal(raw, item.Binding); err != nil {
|
|
return nil, err
|
|
}
|
|
if item.Binding.ExecutionId != item.ExecutionID || item.Binding.TenantId != item.TenantID {
|
|
return nil, ErrMessageScope
|
|
}
|
|
item.Binding.TaskRevision = item.ExpectedRevision
|
|
}
|
|
result = append(result, item)
|
|
}
|
|
return result, rows.Err()
|
|
}
|