247 lines
8.1 KiB
Go
247 lines
8.1 KiB
Go
package staterpc
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"time"
|
|
|
|
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/storecontract"
|
|
)
|
|
|
|
type statusServer struct {
|
|
pb.UnimplementedStatusServiceServer
|
|
*server
|
|
}
|
|
|
|
func (s *server) applyStatus() (storecontract.ApplyStatusStore, error) {
|
|
if s.deps.ApplyStatus == nil {
|
|
return nil, errApplyStatusUnavailable
|
|
}
|
|
return s.deps.ApplyStatus, nil
|
|
}
|
|
|
|
func (s *statusServer) PutApplyStatus(ctx context.Context, r *pb.PutApplyStatusRequest) (*pb.PutApplyStatusResponse, error) {
|
|
as, err := s.applyStatus()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := as.PutApplyStatus(ctx, applyStatusValue(r.Status)); err != nil {
|
|
return nil, s.logErr("PutApplyStatus", err)
|
|
}
|
|
return &pb.PutApplyStatusResponse{}, nil
|
|
}
|
|
|
|
func (s *statusServer) ListApplyStatus(ctx context.Context, _ *pb.ListApplyStatusRequest) (*pb.ListApplyStatusResponse, error) {
|
|
as, err := s.applyStatus()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
statuses, err := as.ListApplyStatus(ctx)
|
|
if err != nil {
|
|
return nil, s.logErr("ListApplyStatus", err)
|
|
}
|
|
out := make([]*pb.ApplyStatus, 0, len(statuses))
|
|
for _, status := range statuses {
|
|
out = append(out, applyStatusProto(status))
|
|
}
|
|
return &pb.ListApplyStatusResponse{Statuses: out}, nil
|
|
}
|
|
|
|
func (s *server) configSnapshots() (storecontract.ConfigSnapshotStore, error) {
|
|
if s.deps.ConfigSnapshots == nil {
|
|
return nil, errConfigSnapshotsUnavailable
|
|
}
|
|
return s.deps.ConfigSnapshots, nil
|
|
}
|
|
|
|
func (s *statusServer) PutConfigSnapshot(ctx context.Context, r *pb.PutConfigSnapshotRequest) (*pb.PutConfigSnapshotResponse, error) {
|
|
cs, err := s.configSnapshots()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := cs.PutConfigSnapshot(ctx, configSnapshotValue(r.Snapshot)); err != nil {
|
|
return nil, s.logErr("PutConfigSnapshot", err)
|
|
}
|
|
return &pb.PutConfigSnapshotResponse{}, nil
|
|
}
|
|
|
|
func (s *statusServer) GetConfigSnapshot(ctx context.Context, _ *pb.GetConfigSnapshotRequest) (*pb.GetConfigSnapshotResponse, error) {
|
|
cs, err := s.configSnapshots()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
snapshot, found, err := cs.ConfigSnapshot(ctx)
|
|
if err != nil {
|
|
return nil, s.logErr("GetConfigSnapshot", err)
|
|
}
|
|
if !found {
|
|
return &pb.GetConfigSnapshotResponse{}, nil
|
|
}
|
|
return &pb.GetConfigSnapshotResponse{Snapshot: configSnapshotProto(snapshot), Found: true}, nil
|
|
}
|
|
|
|
func (s *server) presence() (storecontract.PresenceStore, error) {
|
|
if s.deps.Presence == nil {
|
|
return nil, errPresenceUnavailable
|
|
}
|
|
return s.deps.Presence, nil
|
|
}
|
|
|
|
func (s *statusServer) PutPresence(ctx context.Context, r *pb.PutPresenceRequest) (*pb.PutPresenceResponse, error) {
|
|
ps, err := s.presence()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
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
|
|
}
|
|
|
|
func (s *statusServer) ListPresence(ctx context.Context, _ *pb.ListPresenceRequest) (*pb.ListPresenceResponse, error) {
|
|
ps, err := s.presence()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
presences, err := ps.ListPresence(ctx)
|
|
if err != nil {
|
|
return nil, s.logErr("ListPresence", err)
|
|
}
|
|
out := make([]*pb.Presence, 0, len(presences))
|
|
for _, presence := range presences {
|
|
out = append(out, presenceProto(presence))
|
|
}
|
|
return &pb.ListPresenceResponse{Presences: out}, nil
|
|
}
|
|
|
|
func (s *server) channelStatuses() (storecontract.ChannelStatusStore, error) {
|
|
if s.deps.ChannelStatus == nil {
|
|
return nil, errChannelStatusUnavailable
|
|
}
|
|
return s.deps.ChannelStatus, nil
|
|
}
|
|
|
|
func (s *statusServer) PutChannelStatus(ctx context.Context, r *pb.PutChannelStatusRequest) (*pb.PutChannelStatusResponse, error) {
|
|
cs, err := s.channelStatuses()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := cs.PutChannelStatus(ctx, channelStatusesValue(r.Channels)); err != nil {
|
|
return nil, s.logErr("PutChannelStatus", err)
|
|
}
|
|
return &pb.PutChannelStatusResponse{}, nil
|
|
}
|
|
|
|
func (s *statusServer) ListChannelStatus(ctx context.Context, _ *pb.ListChannelStatusRequest) (*pb.ListChannelStatusResponse, error) {
|
|
cs, err := s.channelStatuses()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
channels, err := cs.ChannelStatus(ctx)
|
|
if err != nil {
|
|
return nil, s.logErr("ListChannelStatus", err)
|
|
}
|
|
return &pb.ListChannelStatusResponse{Channels: channelStatusesProto(channels)}, nil
|
|
}
|
|
|
|
// PutConfigSnapshot publishes the dashboard's configuration projection. The
|
|
// server admits it on the administrative token only; a task-scoped grant
|
|
// cannot reach it (see TaskGrants).
|
|
func (c *Client) PutConfigSnapshot(ctx context.Context, snapshot storecontract.ConfigSnapshot) error {
|
|
_, err := c.status.PutConfigSnapshot(ctx, &pb.PutConfigSnapshotRequest{Snapshot: configSnapshotProto(snapshot)})
|
|
if err != nil {
|
|
return unmapError(err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ConfigSnapshot reads the published projection. found is false, with a nil
|
|
// error, before a daemon has published one.
|
|
func (c *Client) ConfigSnapshot(ctx context.Context) (storecontract.ConfigSnapshot, bool, error) {
|
|
reply, err := c.status.GetConfigSnapshot(ctx, &pb.GetConfigSnapshotRequest{})
|
|
if err != nil {
|
|
return storecontract.ConfigSnapshot{}, false, unmapError(err)
|
|
}
|
|
if !reply.Found {
|
|
return storecontract.ConfigSnapshot{}, false, nil
|
|
}
|
|
return configSnapshotValue(reply.Snapshot), true, nil
|
|
}
|
|
|
|
// PutChannelStatus publishes the channel state the hosting process observes.
|
|
// Administrative, like the config snapshot pair: the server admits it on the
|
|
// administrative token only, so a task-scoped grant cannot reach it.
|
|
func (c *Client) PutChannelStatus(ctx context.Context, channels []storecontract.ChannelStatus) error {
|
|
_, err := c.status.PutChannelStatus(ctx, &pb.PutChannelStatusRequest{Channels: channelStatusesProto(channels)})
|
|
if err != nil {
|
|
return unmapError(err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ChannelStatus reads every reported channel.
|
|
func (c *Client) ChannelStatus(ctx context.Context) ([]storecontract.ChannelStatus, error) {
|
|
reply, err := c.status.ListChannelStatus(ctx, &pb.ListChannelStatusRequest{})
|
|
if err != nil {
|
|
return nil, unmapError(err)
|
|
}
|
|
return channelStatusesValue(reply.Channels), nil
|
|
}
|
|
|
|
// PutApplyStatus reports what this process applied for one resource kind.
|
|
// Administrative, like the config snapshot pair: a task-scoped grant cannot
|
|
// reach it.
|
|
func (c *Client) PutApplyStatus(ctx context.Context, status storecontract.ApplyStatus) error {
|
|
_, err := c.status.PutApplyStatus(ctx, &pb.PutApplyStatusRequest{Status: applyStatusProto(status)})
|
|
if err != nil {
|
|
return unmapError(err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ListApplyStatus reads every process's report. An empty slice means nothing
|
|
// has reported yet, which is not an error.
|
|
func (c *Client) ListApplyStatus(ctx context.Context) ([]storecontract.ApplyStatus, error) {
|
|
reply, err := c.status.ListApplyStatus(ctx, &pb.ListApplyStatusRequest{})
|
|
if err != nil {
|
|
return nil, unmapError(err)
|
|
}
|
|
statuses := make([]storecontract.ApplyStatus, 0, len(reply.Statuses))
|
|
for _, status := range reply.Statuses {
|
|
statuses = append(statuses, applyStatusValue(status))
|
|
}
|
|
return statuses, nil
|
|
}
|
|
|
|
// PutPresence re-stamps this service instance's record. Administrative.
|
|
func (c *Client) PutPresence(ctx context.Context, presence storecontract.Presence) error {
|
|
_, err := c.status.PutPresence(ctx, &pb.PutPresenceRequest{Presence: presenceProto(presence)})
|
|
if err != nil {
|
|
return unmapError(err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ListPresence reads every service instance's record.
|
|
func (c *Client) ListPresence(ctx context.Context) ([]storecontract.Presence, error) {
|
|
reply, err := c.status.ListPresence(ctx, &pb.ListPresenceRequest{})
|
|
if err != nil {
|
|
return nil, unmapError(err)
|
|
}
|
|
out := make([]storecontract.Presence, 0, len(reply.Presences))
|
|
for _, presence := range reply.Presences {
|
|
out = append(out, presenceValue(presence))
|
|
}
|
|
return out, nil
|
|
}
|