feat(presence): report workers and broker on status
This commit is contained in:
1 parent
7680dc5079
commit
ff9b6c7196
9 files changed
+194
-79
No files matched your search
@@ -14,6 +14,7 @@ import (
|
||||
natsio "github.com/nats-io/nats.go"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/agentexec"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
@@ -345,3 +346,12 @@ func (*Transport) respondTask(msg *natsio.Msg, response *taskrun.Response, runEr
|
||||
log.Error("taskrun respond failed", "err", err)
|
||||
}
|
||||
}
|
||||
|
||||
// PutPresence publishes the worker's task-scoped record.
|
||||
func (t *Transport) PutPresence(ctx context.Context, record storecontract.Presence) error {
|
||||
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
return t.state.PutPresence(ctx, record)
|
||||
}
|
||||
|
||||
func (t *Transport) BrokerReady() bool { return t.conn.IsConnected() }
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
@@ -596,7 +597,15 @@ func (s *server) PutPresence(ctx context.Context, r *pb.PutPresenceRequest) (*pb
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := ps.PutPresence(ctx, presenceValue(r.Presence)); err != nil {
|
||||
record := presenceValue(r.Presence)
|
||||
if strings.HasPrefix(access.ActorFromContext(ctx), "task/") {
|
||||
// A worker's clock cannot expire other services' retained records.
|
||||
record.ReportedAt = time.Now().UTC()
|
||||
if record.StartedAt.IsZero() || record.StartedAt.After(record.ReportedAt) {
|
||||
record.StartedAt = record.ReportedAt
|
||||
}
|
||||
}
|
||||
if err := ps.PutPresence(ctx, record); err != nil {
|
||||
return nil, s.logErr("PutPresence", err)
|
||||
}
|
||||
return &pb.PutPresenceResponse{}, nil
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
@@ -18,6 +19,7 @@ import (
|
||||
pb "github.com/samcharles93/archie-core/internal/contracts/state/v1"
|
||||
"github.com/samcharles93/archie-core/internal/domain/access"
|
||||
"github.com/samcharles93/archie-core/internal/domain/identity"
|
||||
"github.com/samcharles93/archie-core/internal/domain/presence"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
@@ -87,6 +89,17 @@ func authorizesTaskScopedCall(fullMethod string, req any, taskID int64) bool {
|
||||
// taskScopedTargets maps each RPC a task grant may call to the task ID its
|
||||
// request targets.
|
||||
var taskScopedTargets = map[string]func(any) int64{
|
||||
pb.StateStoreService_PutPresence_FullMethodName: func(req any) int64 {
|
||||
r, ok := req.(*pb.PutPresenceRequest)
|
||||
if !ok || r.Presence == nil || r.Presence.Service != presence.Agent {
|
||||
return 0
|
||||
}
|
||||
id, err := strconv.ParseInt(r.Presence.InstanceId, 10, 64)
|
||||
if err != nil || id <= 0 || strconv.FormatInt(id, 10) != r.Presence.InstanceId {
|
||||
return 0
|
||||
}
|
||||
return id
|
||||
},
|
||||
pb.StateStoreService_Update_FullMethodName: func(req any) int64 {
|
||||
r, ok := req.(*pb.UpdateRequest)
|
||||
if !ok || r.Task == nil {
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package staterpc
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
pb "github.com/samcharles93/archie-core/internal/contracts/state/v1"
|
||||
)
|
||||
|
||||
func TestWorkerPresenceAuthority(t *testing.T) {
|
||||
for _, tt := range []struct {
|
||||
name, service, instance string
|
||||
allowed bool
|
||||
}{
|
||||
{"own task", "archie-agent", "7", true},
|
||||
{"another task", "archie-agent", "8", false},
|
||||
{"daemon", "archied", "7", false},
|
||||
{"broker", "nats", "7", false},
|
||||
{"noncanonical task", "archie-agent", "007", false},
|
||||
{"no task", "archie-agent", "", false},
|
||||
} {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
req := &pb.PutPresenceRequest{Presence: &pb.Presence{Service: tt.service, InstanceId: tt.instance}}
|
||||
if got := authorizesTaskScopedCall(pb.StateStoreService_PutPresence_FullMethodName, req, 7); got != tt.allowed {
|
||||
t.Fatalf("authorized = %v, want %v", got, tt.allowed)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user