refactor(staterpc): one file per State Store service
This commit is contained in:
1 parent
6120c38e7f
commit
0cccef6bb7
23 files changed
+2273
-2154
No files matched your search
@@ -197,3 +197,8 @@ func accessStatus(err error) error {
|
||||
}
|
||||
return mapError(err)
|
||||
}
|
||||
|
||||
type accessServer struct {
|
||||
pb.UnimplementedAccessServiceServer
|
||||
*server
|
||||
}
|
||||
@@ -168,3 +168,8 @@ func (s *server) authorizeAccountManagement(ctx context.Context) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type accountServer struct {
|
||||
pb.UnimplementedAccountServiceServer
|
||||
*server
|
||||
}
|
||||
@@ -188,3 +188,8 @@ func (s *alertServer) RecordAlertAttempt(ctx context.Context, r *pb.RecordAlertA
|
||||
}
|
||||
return &pb.RecordAlertAttemptResponse{}, nil
|
||||
}
|
||||
|
||||
type alertServer struct {
|
||||
pb.UnimplementedAlertServiceServer
|
||||
*server
|
||||
}
|
||||
@@ -0,0 +1,203 @@
|
||||
package staterpc
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
pb "github.com/samcharles93/archie-core/internal/contracts/state/v1"
|
||||
"github.com/samcharles93/archie-core/internal/domain/binding"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
)
|
||||
|
||||
type bindingServer struct {
|
||||
pb.UnimplementedBindingServiceServer
|
||||
*server
|
||||
}
|
||||
|
||||
func (s *server) binding() (storecontract.BindingStore, error) {
|
||||
if s.deps.Bindings == nil {
|
||||
return nil, errBindingUnavailable
|
||||
}
|
||||
return s.deps.Bindings, nil
|
||||
}
|
||||
|
||||
func (s *bindingServer) InsertBinding(ctx context.Context, r *pb.InsertBindingRequest) (*pb.InsertBindingResponse, error) {
|
||||
bs, err := s.binding()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
value := bindingValue(r.Binding)
|
||||
if err := value.Validate(); err != nil {
|
||||
return nil, validationFailure(err)
|
||||
}
|
||||
id, err := bs.InsertBinding(ctx, value)
|
||||
if err != nil {
|
||||
return nil, s.logErr("InsertBinding", err)
|
||||
}
|
||||
return &pb.InsertBindingResponse{Id: id}, nil
|
||||
}
|
||||
|
||||
func (s *bindingServer) GetBinding(ctx context.Context, r *pb.GetBindingRequest) (*pb.GetBindingResponse, error) {
|
||||
bs, err := s.binding()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
b, err := bs.GetBinding(ctx, r.Id)
|
||||
if err != nil {
|
||||
return nil, s.logErr("GetBinding", err)
|
||||
}
|
||||
return &pb.GetBindingResponse{Binding: bindingProtoPtr(b), Found: b != nil}, nil
|
||||
}
|
||||
|
||||
func (s *bindingServer) ListBindings(ctx context.Context, _ *pb.ListBindingsRequest) (*pb.ListBindingsResponse, error) {
|
||||
bs, err := s.binding()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
bindings, err := bs.ListBindings(ctx)
|
||||
if err != nil {
|
||||
return nil, s.logErr("ListBindings", err)
|
||||
}
|
||||
return &pb.ListBindingsResponse{Bindings: mapValues(bindings, bindingProto)}, nil
|
||||
}
|
||||
|
||||
func (s *bindingServer) UpdateBinding(ctx context.Context, r *pb.UpdateBindingRequest) (*pb.UpdateBindingResponse, error) {
|
||||
bs, err := s.binding()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
value := bindingValue(r.Binding)
|
||||
if err := value.Validate(); err != nil {
|
||||
return nil, validationFailure(err)
|
||||
}
|
||||
if err := bs.UpdateBinding(ctx, value); err != nil {
|
||||
return nil, s.logErr("UpdateBinding", err)
|
||||
}
|
||||
return &pb.UpdateBindingResponse{}, nil
|
||||
}
|
||||
|
||||
func (s *bindingServer) DeleteBinding(ctx context.Context, r *pb.DeleteBindingRequest) (*pb.DeleteBindingResponse, error) {
|
||||
bs, err := s.binding()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := bs.DeleteBinding(ctx, r.Id); err != nil {
|
||||
return nil, s.logErr("DeleteBinding", err)
|
||||
}
|
||||
return &pb.DeleteBindingResponse{}, nil
|
||||
}
|
||||
|
||||
func (s *bindingServer) PauseBinding(ctx context.Context, r *pb.PauseBindingRequest) (*pb.PauseBindingResponse, error) {
|
||||
bs, err := s.binding()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := bs.PauseBinding(ctx, r.Id); err != nil {
|
||||
return nil, s.logErr("PauseBinding", err)
|
||||
}
|
||||
return &pb.PauseBindingResponse{}, nil
|
||||
}
|
||||
|
||||
func (s *bindingServer) ResumeBinding(ctx context.Context, r *pb.ResumeBindingRequest) (*pb.ResumeBindingResponse, error) {
|
||||
bs, err := s.binding()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := bs.ResumeBinding(ctx, r.Id); err != nil {
|
||||
return nil, s.logErr("ResumeBinding", err)
|
||||
}
|
||||
return &pb.ResumeBindingResponse{}, nil
|
||||
}
|
||||
|
||||
func (s *bindingServer) ApproveBinding(ctx context.Context, r *pb.ApproveBindingRequest) (*pb.ApproveBindingResponse, error) {
|
||||
bs, err := s.binding()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := bs.ApproveBinding(ctx, r.Id); err != nil {
|
||||
return nil, s.logErr("ApproveBinding", err)
|
||||
}
|
||||
return &pb.ApproveBindingResponse{}, nil
|
||||
}
|
||||
|
||||
// Source
|
||||
|
||||
func (s *bindingServer) ArmedBindingsForSource(ctx context.Context, r *pb.ArmedBindingsForSourceRequest) (*pb.ArmedBindingsForSourceResponse, error) {
|
||||
if s.deps.BindingDispatcher == nil {
|
||||
return nil, errBindingDispatchUnavailable
|
||||
}
|
||||
bindings, err := s.deps.BindingDispatcher.ArmedBindingsForSource(ctx, r.Source)
|
||||
if err != nil {
|
||||
return nil, s.logErr("ArmedBindingsForSource", err)
|
||||
}
|
||||
return &pb.ArmedBindingsForSourceResponse{Bindings: mapValues(bindings, bindingProto)}, nil
|
||||
}
|
||||
|
||||
func bindingProtoPtr(b *binding.Binding) *pb.Binding {
|
||||
if b == nil {
|
||||
return nil
|
||||
}
|
||||
return bindingProto(*b)
|
||||
}
|
||||
|
||||
func (c *Client) InsertBinding(ctx context.Context, b binding.Binding) (string, error) {
|
||||
r, err := c.binding.InsertBinding(ctx, &pb.InsertBindingRequest{Binding: bindingProto(b)})
|
||||
if err != nil {
|
||||
return "", unmapError(err)
|
||||
}
|
||||
return r.Id, nil
|
||||
}
|
||||
|
||||
func (c *Client) GetBinding(ctx context.Context, id string) (*binding.Binding, error) {
|
||||
r, err := c.binding.GetBinding(ctx, &pb.GetBindingRequest{Id: id})
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
if !r.Found {
|
||||
return nil, nil
|
||||
}
|
||||
v := bindingValue(r.Binding)
|
||||
return &v, nil
|
||||
}
|
||||
|
||||
func (c *Client) ListBindings(ctx context.Context) ([]binding.Binding, error) {
|
||||
r, err := c.binding.ListBindings(ctx, &pb.ListBindingsRequest{})
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
return mapValues(r.Bindings, bindingValue), nil
|
||||
}
|
||||
|
||||
func (c *Client) UpdateBinding(ctx context.Context, b binding.Binding) error {
|
||||
_, err := c.binding.UpdateBinding(ctx, &pb.UpdateBindingRequest{Binding: bindingProto(b)})
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
func (c *Client) DeleteBinding(ctx context.Context, id string) error {
|
||||
_, err := c.binding.DeleteBinding(ctx, &pb.DeleteBindingRequest{Id: id})
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
func (c *Client) PauseBinding(ctx context.Context, id string) error {
|
||||
_, err := c.binding.PauseBinding(ctx, &pb.PauseBindingRequest{Id: id})
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
func (c *Client) ResumeBinding(ctx context.Context, id string) error {
|
||||
_, err := c.binding.ResumeBinding(ctx, &pb.ResumeBindingRequest{Id: id})
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
func (c *Client) ApproveBinding(ctx context.Context, id string) error {
|
||||
_, err := c.binding.ApproveBinding(ctx, &pb.ApproveBindingRequest{Id: id})
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
// Source
|
||||
|
||||
func (c *Client) ArmedBindingsForSource(ctx context.Context, source string) ([]binding.Binding, error) {
|
||||
r, err := c.binding.ArmedBindingsForSource(ctx, &pb.ArmedBindingsForSourceRequest{Source: source})
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
return mapValues(r.Bindings, bindingValue), nil
|
||||
}
|
||||
@@ -0,0 +1,170 @@
|
||||
package staterpc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"time"
|
||||
|
||||
pb "github.com/samcharles93/archie-core/internal/contracts/state/v1"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
)
|
||||
|
||||
type captureServer struct {
|
||||
pb.UnimplementedCaptureServiceServer
|
||||
*server
|
||||
}
|
||||
|
||||
func (s *server) capture() (storecontract.CaptureStore, error) {
|
||||
if s.deps.Captures == nil {
|
||||
return nil, errCaptureUnavailable
|
||||
}
|
||||
return s.deps.Captures, nil
|
||||
}
|
||||
|
||||
func (s *captureServer) RecordCaptureRefusal(ctx context.Context, r *pb.RecordCaptureRefusalRequest) (*pb.RecordCaptureRefusalResponse, error) {
|
||||
if s.deps.Refusals == nil {
|
||||
return nil, errRefusalsUnavailable
|
||||
}
|
||||
if err := s.deps.Refusals.RecordCaptureRefusal(ctx, r.Source, r.Addr, timeValue(r.RefusedAt)); err != nil {
|
||||
return nil, s.logErr("RecordCaptureRefusal", err)
|
||||
}
|
||||
return &pb.RecordCaptureRefusalResponse{}, nil
|
||||
}
|
||||
|
||||
func (s *captureServer) ListCaptureRefusals(ctx context.Context, r *pb.ListCaptureRefusalsRequest) (*pb.ListCaptureRefusalsResponse, error) {
|
||||
if s.deps.Refusals == nil {
|
||||
return nil, errRefusalsUnavailable
|
||||
}
|
||||
sources, err := s.deps.Refusals.CaptureRefusals(ctx, timeValue(r.Since))
|
||||
if err != nil {
|
||||
return nil, s.logErr("ListCaptureRefusals", err)
|
||||
}
|
||||
out := make([]*pb.CaptureRefusals, 0, len(sources))
|
||||
for _, c := range sources {
|
||||
out = append(out, &pb.CaptureRefusals{Source: c.Source, Refused: c.Refused, Accepted: c.Accepted, Addrs: c.Addrs})
|
||||
}
|
||||
return &pb.ListCaptureRefusalsResponse{Sources: out}, nil
|
||||
}
|
||||
|
||||
func (s *captureServer) InsertCapture(ctx context.Context, r *pb.InsertCaptureRequest) (*pb.InsertCaptureResponse, error) {
|
||||
cs, err := s.capture()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
id, err := cs.InsertCapture(ctx, capturedEventValue(r.Capture), timeSeconds(r.RetentionSeconds), int(r.MaxEvents))
|
||||
if err != nil {
|
||||
return nil, s.logErr("InsertCapture", err)
|
||||
}
|
||||
return &pb.InsertCaptureResponse{Id: id}, nil
|
||||
}
|
||||
|
||||
// StreamCaptures streams one capture per message: a batch of large capture
|
||||
// bodies in one unary response can exceed gRPC's 4MiB message cap
|
||||
func (s *captureServer) StreamCaptures(r *pb.StreamCapturesRequest, stream pb.CaptureService_StreamCapturesServer) error {
|
||||
cs, err := s.capture()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
captures, err := cs.ListCaptures(stream.Context(), int(r.Limit))
|
||||
if err != nil {
|
||||
return s.logErr("StreamCaptures", err)
|
||||
}
|
||||
for _, c := range captures {
|
||||
if err := stream.Send(&pb.StreamCapturesResponse{Capture: capturedEventProto(c)}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Mapping
|
||||
|
||||
// StreamUndispatchedCaptures streams for the same reason as StreamCaptures.
|
||||
func (s *captureServer) StreamUndispatchedCaptures(r *pb.StreamUndispatchedCapturesRequest, stream pb.CaptureService_StreamUndispatchedCapturesServer) error {
|
||||
if s.deps.BindingDispatcher == nil {
|
||||
return errBindingDispatchUnavailable
|
||||
}
|
||||
captures, err := s.deps.BindingDispatcher.ListUndispatchedCaptures(stream.Context(), r.Bindings, int(r.Limit))
|
||||
if err != nil {
|
||||
return s.logErr("StreamUndispatchedCaptures", err)
|
||||
}
|
||||
for _, c := range captures {
|
||||
if err := stream.Send(&pb.StreamUndispatchedCapturesResponse{Capture: capturedEventProto(c)}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// BindingTaskCreator
|
||||
|
||||
func (c *Client) InsertCapture(ctx context.Context, ce storecontract.CapturedEvent, retention time.Duration, maxEvents int) (string, error) {
|
||||
r, err := c.capture.InsertCapture(ctx, &pb.InsertCaptureRequest{Capture: capturedEventProto(ce), RetentionSeconds: int64(retention.Seconds()), MaxEvents: int64(maxEvents)})
|
||||
if err != nil {
|
||||
return "", unmapError(err)
|
||||
}
|
||||
return r.Id, nil
|
||||
}
|
||||
|
||||
// RecordCaptureRefusal counts one event the network rules refused.
|
||||
func (c *Client) RecordCaptureRefusal(ctx context.Context, source, addr string, at time.Time) error {
|
||||
_, err := c.capture.RecordCaptureRefusal(ctx, &pb.RecordCaptureRefusalRequest{Source: source, Addr: addr, RefusedAt: timestamp(at)})
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
// CaptureRefusals reads each source's refusals since a time.
|
||||
func (c *Client) CaptureRefusals(ctx context.Context, since time.Time) ([]storecontract.CaptureRefusals, error) {
|
||||
reply, err := c.capture.ListCaptureRefusals(ctx, &pb.ListCaptureRefusalsRequest{Since: timestamp(since)})
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
out := make([]storecontract.CaptureRefusals, 0, len(reply.Sources))
|
||||
for _, s := range reply.Sources {
|
||||
out = append(out, storecontract.CaptureRefusals{Source: s.Source, Refused: s.Refused, Accepted: s.Accepted, Addrs: s.Addrs})
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// ListCaptures streams one capture per message; see StreamCaptures in
|
||||
// server.go.
|
||||
func (c *Client) ListCaptures(ctx context.Context, limit int) ([]storecontract.CapturedEvent, error) {
|
||||
stream, err := c.capture.StreamCaptures(ctx, &pb.StreamCapturesRequest{Limit: int64(limit)})
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
var captures []storecontract.CapturedEvent
|
||||
for {
|
||||
r, err := stream.Recv()
|
||||
if errors.Is(err, io.EOF) {
|
||||
return captures, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
captures = append(captures, capturedEventValue(r.Capture))
|
||||
}
|
||||
}
|
||||
|
||||
// Mapping
|
||||
|
||||
// ListUndispatchedCaptures streams for the same reason as ListCaptures.
|
||||
func (c *Client) ListUndispatchedCaptures(ctx context.Context, bindings []string, limit int) ([]storecontract.CapturedEvent, error) {
|
||||
stream, err := c.capture.StreamUndispatchedCaptures(ctx, &pb.StreamUndispatchedCapturesRequest{Bindings: bindings, Limit: int64(limit)})
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
var captures []storecontract.CapturedEvent
|
||||
for {
|
||||
r, err := stream.Recv()
|
||||
if errors.Is(err, io.EOF) {
|
||||
return captures, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
captures = append(captures, capturedEventValue(r.Capture))
|
||||
}
|
||||
}
|
||||
|
||||
// BindingTaskCreator
|
||||
@@ -2,28 +2,15 @@ package staterpc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"time"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
controlpb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1"
|
||||
pb "github.com/samcharles93/archie-core/internal/contracts/state/v1"
|
||||
"github.com/samcharles93/archie-core/internal/domain/binding"
|
||||
"github.com/samcharles93/archie-core/internal/domain/harnesssecret"
|
||||
"github.com/samcharles93/archie-core/internal/domain/identity"
|
||||
"github.com/samcharles93/archie-core/internal/domain/mapping"
|
||||
"github.com/samcharles93/archie-core/internal/domain/source"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storepkg"
|
||||
"github.com/samcharles93/archie-core/internal/domain/usage"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
"github.com/samcharles93/archie-core/internal/logging"
|
||||
)
|
||||
|
||||
// Client is the single remote adapter over the State Store services,
|
||||
@@ -117,837 +104,7 @@ var (
|
||||
|
||||
// Lifecycle
|
||||
|
||||
func (c *Client) EnqueueIssue(ctx context.Context, owner, repo string, number int, title, body, labels, identity string) (bool, error) {
|
||||
r, err := c.task.EnqueueIssue(ctx, &pb.EnqueueIssueRequest{Owner: owner, Repo: repo, Number: int64(number), Title: title, Body: body, Labels: labels, Identity: identity})
|
||||
if err != nil {
|
||||
return false, unmapError(err)
|
||||
}
|
||||
return r.Inserted, nil
|
||||
}
|
||||
|
||||
func (c *Client) ActiveTasksByOrigin(ctx context.Context, origin string) ([]*task.Task, error) {
|
||||
r, err := c.task.ActiveTasksByOrigin(ctx, &pb.ActiveTasksByOriginRequest{Origin: origin})
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
return mapValues(r.Tasks, taskValue), nil
|
||||
}
|
||||
|
||||
func (c *Client) UnpostedOutcomes(ctx context.Context) ([]*task.Task, error) {
|
||||
r, err := c.task.UnpostedOutcomes(ctx, &pb.UnpostedOutcomesRequest{})
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
return mapValues(r.Tasks, taskValue), nil
|
||||
}
|
||||
|
||||
func (c *Client) MarkOutcomePosted(ctx context.Context, taskID int64, status string) error {
|
||||
_, err := c.task.MarkOutcomePosted(ctx, &pb.MarkOutcomePostedRequest{TaskId: taskID, Status: status})
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
func (c *Client) EnqueueChatTask(ctx context.Context, owner, repo, title, body, wf, identity, origin, draft string, inputs map[string]any) (*task.Task, error) {
|
||||
encoded, err := task.EncodeInputs(inputs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
r, err := c.task.EnqueueChatTask(ctx, &pb.EnqueueChatTaskRequest{Owner: owner, Repo: repo, Title: title, Body: body, Workflow: wf, Identity: identity, InputsJson: encoded, Origin: origin, DraftYaml: draft})
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
return taskValue(r.Task), nil
|
||||
}
|
||||
|
||||
func (c *Client) ClaimNext(ctx context.Context) (*task.Task, error) {
|
||||
r, err := c.task.ClaimNext(ctx, &pb.ClaimNextRequest{})
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
if !r.Found {
|
||||
return nil, nil
|
||||
}
|
||||
return taskValue(r.Task), nil
|
||||
}
|
||||
|
||||
func (c *Client) ClaimByIssue(ctx context.Context, owner, repo string, number int) (*task.Task, error) {
|
||||
r, err := c.task.ClaimByIssue(ctx, &pb.ClaimByIssueRequest{Owner: owner, Repo: repo, Number: int64(number)})
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
if !r.Found {
|
||||
return nil, nil
|
||||
}
|
||||
return taskValue(r.Task), nil
|
||||
}
|
||||
|
||||
func (c *Client) Transition(ctx context.Context, taskID int64, from, to, detail string) error {
|
||||
_, err := c.task.Transition(ctx, &pb.TransitionRequest{TaskId: taskID, From: from, To: to, Detail: detail})
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
// TaskModels returns what the task's org resolves models against.
|
||||
func (c *Client) TaskModels(ctx context.Context, taskID int64) (config.OrgModels, error) {
|
||||
r, err := c.task.TaskModels(ctx, &pb.TaskModelsRequest{TaskId: taskID})
|
||||
if err != nil {
|
||||
return config.OrgModels{}, unmapError(err)
|
||||
}
|
||||
providers := make(map[string]config.Provider, len(r.Providers))
|
||||
for name, p := range r.Providers {
|
||||
providers[name] = modelProviderFromPB(p)
|
||||
}
|
||||
return config.OrgModels{Org: r.OrgId, Aliases: r.Aliases, Providers: providers}, nil
|
||||
}
|
||||
|
||||
// TaskInstructions returns the instance and organisation instructions for the
|
||||
// task's org.
|
||||
func (c *Client) TaskInstructions(ctx context.Context, taskID int64) (instance, organisation string, err error) {
|
||||
r, err := c.task.TaskInstructions(ctx, &pb.TaskInstructionsRequest{TaskId: taskID})
|
||||
if err != nil {
|
||||
return "", "", unmapError(err)
|
||||
}
|
||||
return r.GetInstance(), r.GetOrganisation(), nil
|
||||
}
|
||||
|
||||
// CallerModels returns the calling principal's org and the aliases it may
|
||||
// use.
|
||||
func (c *Client) CallerModels(ctx context.Context) (string, map[string]string, error) {
|
||||
r, err := c.org.CallerModels(ctx, &pb.CallerModelsRequest{})
|
||||
if err != nil {
|
||||
return "", nil, unmapError(err)
|
||||
}
|
||||
return r.OrgId, r.Aliases, nil
|
||||
}
|
||||
|
||||
// RecordUsage appends one model call's usage record.
|
||||
func (c *Client) RecordUsage(ctx context.Context, r usage.Record) error {
|
||||
_, err := c.execution.RecordUsage(ctx, &pb.RecordUsageRequest{Record: usageProto(r)})
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
// StartStep and FinishStep record step executions and return the persisted
|
||||
// event.
|
||||
func (c *Client) StartStep(ctx context.Context, s task.StepStart) (int64, events.Event, error) {
|
||||
r, err := c.execution.StartStep(ctx, &pb.StartStepRequest{
|
||||
ExecutionId: s.ExecutionID, Attempt: int64(s.Attempt),
|
||||
ParentId: s.ParentID, Kind: s.Kind, Name: s.Name,
|
||||
CalledExecutionId: s.CalledExecutionID,
|
||||
})
|
||||
if err != nil {
|
||||
return 0, events.Event{}, unmapError(err)
|
||||
}
|
||||
event := events.Event{}
|
||||
if r.Event != nil {
|
||||
event = eventValue(r.Event)
|
||||
}
|
||||
return r.StepId, event, nil
|
||||
}
|
||||
|
||||
// CancelExecution is the one cancel path:
|
||||
// the State Store records the cancellation -- every non-terminal step of the
|
||||
// current attempt plus the execution's own move, and its waited-on callees --
|
||||
// and returns the callees it cancelled; the caller cancels the runs'
|
||||
// in-memory contexts afterwards.
|
||||
func (c *Client) CancelExecution(ctx context.Context, taskID int64, reason, to string) ([]int64, error) {
|
||||
r, err := c.execution.CancelExecution(ctx, &pb.CancelExecutionRequest{TaskId: taskID, Reason: reason, To: to})
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
return r.CancelledCalleeIds, nil
|
||||
}
|
||||
|
||||
func (c *Client) FinishStep(ctx context.Context, s task.StepFinish) (events.Event, error) {
|
||||
r, err := c.execution.FinishStep(ctx, &pb.FinishStepRequest{
|
||||
ExecutionId: s.ExecutionID, StepId: s.StepID,
|
||||
From: string(s.From), To: string(s.To), Detail: s.Detail, TokensUsed: s.TokensUsed, Results: s.Results,
|
||||
})
|
||||
if err != nil {
|
||||
return events.Event{}, unmapError(err)
|
||||
}
|
||||
event := events.Event{}
|
||||
if r.Event != nil {
|
||||
event = eventValue(r.Event)
|
||||
}
|
||||
return event, nil
|
||||
}
|
||||
|
||||
func (c *Client) ListSteps(ctx context.Context, executionID int64, attempt int) ([]task.StepExecution, error) {
|
||||
r, err := c.execution.ListSteps(ctx, &pb.ListStepsRequest{ExecutionId: executionID, Attempt: int64(attempt)})
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
steps := make([]task.StepExecution, len(r.Steps))
|
||||
for i, s := range r.Steps {
|
||||
steps[i] = stepExecutionValue(s)
|
||||
}
|
||||
return steps, nil
|
||||
}
|
||||
|
||||
func (c *Client) Update(ctx context.Context, t *task.Task) error {
|
||||
_, err := c.task.Update(ctx, &pb.UpdateRequest{Task: taskProto(t)})
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
func (c *Client) Requeue(ctx context.Context, taskID int64, fromStatus, wf string) error {
|
||||
_, err := c.task.Requeue(ctx, &pb.RequeueRequest{TaskId: taskID, FromStatus: fromStatus, Workflow: wf})
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
func (c *Client) ParkTask(ctx context.Context, taskID int64, from, detail, class, blocker string) error {
|
||||
_, err := c.task.ParkTask(ctx, &pb.ParkTaskRequest{TaskId: taskID, From: from, Detail: detail, Class: class, Blocker: blocker})
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
func (c *Client) RecoverStale(ctx context.Context) (int64, error) {
|
||||
r, err := c.task.RecoverStale(ctx, &pb.RecoverStaleRequest{})
|
||||
if err != nil {
|
||||
return 0, unmapError(err)
|
||||
}
|
||||
return r.Count, nil
|
||||
}
|
||||
|
||||
// Archive / Retry
|
||||
|
||||
func (c *Client) ArchiveTask(ctx context.Context, taskID int64, fromStatus string, audit events.Event) (int64, error) {
|
||||
r, err := c.task.ArchiveTask(ctx, &pb.ArchiveTaskRequest{TaskId: taskID, FromStatus: fromStatus, Audit: eventProto(audit)})
|
||||
if err != nil {
|
||||
return 0, unmapError(err)
|
||||
}
|
||||
return r.EventId, nil
|
||||
}
|
||||
|
||||
func (c *Client) RetryTask(ctx context.Context, taskID int64, fromStatus, wf, retryMode string, resume task.Resume, currentDefinition bool) error {
|
||||
_, err := c.task.RetryTask(ctx, &pb.RetryTaskRequest{
|
||||
TaskId: taskID, FromStatus: fromStatus, Workflow: wf, RetryMode: retryMode,
|
||||
ResumeFrom: resume.From, ResumeAfter: resume.After, CurrentDefinition: currentDefinition,
|
||||
})
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
func (c *Client) RespondReviewGate(ctx context.Context, taskID int64, fromStatus, gate string, rereview bool, maxRounds int) error {
|
||||
_, err := c.review.RespondReviewGate(ctx, &pb.RespondReviewGateRequest{
|
||||
TaskId: taskID, FromStatus: fromStatus, Gate: gate, Rereview: rereview, Cap: int64(maxRounds),
|
||||
})
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
func (c *Client) BeginRemediation(ctx context.Context, taskID int64, payload string) error {
|
||||
_, err := c.review.BeginRemediation(ctx, &pb.BeginRemediationRequest{TaskId: taskID, Payload: payload})
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
func (c *Client) UpdateReviewPayload(ctx context.Context, taskID int64, payload string) error {
|
||||
_, err := c.review.UpdateReviewPayload(ctx, &pb.UpdateReviewPayloadRequest{TaskId: taskID, Payload: payload})
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
func (c *Client) SetReviewCursors(ctx context.Context, taskID, reviewCursor, commentCursor int64) error {
|
||||
_, err := c.review.SetReviewCursors(ctx, &pb.SetReviewCursorsRequest{TaskId: taskID, ReviewCursor: reviewCursor, CommentCursor: commentCursor})
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
// Queries
|
||||
|
||||
func (c *Client) TaskByIssue(ctx context.Context, owner, repo string, number int) (*task.Task, error) {
|
||||
r, err := c.task.TaskByIssue(ctx, &pb.TaskByIssueRequest{Owner: owner, Repo: repo, Number: int64(number)})
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
if !r.Found {
|
||||
return nil, nil
|
||||
}
|
||||
return taskValue(r.Task), nil
|
||||
}
|
||||
|
||||
func (c *Client) OpenTaskByPR(ctx context.Context, owner, repo string, number int) (*task.Task, error) {
|
||||
r, err := c.task.OpenTaskByPR(ctx, &pb.OpenTaskByPRRequest{Owner: owner, Repo: repo, Number: int64(number)})
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
if !r.Found {
|
||||
return nil, nil
|
||||
}
|
||||
return taskValue(r.Task), nil
|
||||
}
|
||||
|
||||
func (c *Client) TaskByID(ctx context.Context, taskID int64) (*task.Task, error) {
|
||||
r, err := c.task.TaskByID(ctx, &pb.TaskByIDRequest{TaskId: taskID})
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
if !r.Found {
|
||||
return nil, nil
|
||||
}
|
||||
return taskValue(r.Task), nil
|
||||
}
|
||||
|
||||
func (c *Client) OpenPRs(ctx context.Context) ([]task.Task, error) {
|
||||
r, err := c.task.OpenPRs(ctx, &pb.OpenPRsRequest{})
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
return mapValues(r.Tasks, func(t *pb.Task) task.Task { return *taskValue(t) }), nil
|
||||
}
|
||||
|
||||
func (c *Client) ClearTerminalTasks(ctx context.Context) (int64, error) {
|
||||
r, err := c.task.ClearTerminalTasks(ctx, &pb.ClearTerminalTasksRequest{})
|
||||
if err != nil {
|
||||
return 0, unmapError(err)
|
||||
}
|
||||
return r.Count, nil
|
||||
}
|
||||
|
||||
// Tasks returns the most recently updated tasks.
|
||||
func (c *Client) Tasks(ctx context.Context, limit int) ([]task.Task, error) {
|
||||
return c.TasksPage(ctx, storecontract.TaskPage{Limit: limit})
|
||||
}
|
||||
|
||||
// TasksPage returns one page of tasks, most recently updated first.
|
||||
func (c *Client) TasksPage(ctx context.Context, page storecontract.TaskPage) ([]task.Task, error) {
|
||||
r, err := c.task.Tasks(ctx, &pb.TasksRequest{
|
||||
Limit: int64(page.Limit), Statuses: page.Statuses, Workflow: page.Workflow,
|
||||
AfterUpdatedAt: timestamp(page.After.UpdatedAt), AfterId: page.After.ID,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
return mapValues(r.Tasks, func(t *pb.Task) task.Task { return *taskValue(t) }), nil
|
||||
}
|
||||
|
||||
func (c *Client) StatusCounts(ctx context.Context) (map[string]int, error) {
|
||||
r, err := c.stats.StatusCounts(ctx, &pb.StatusCountsRequest{})
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
out := make(map[string]int, len(r.Counts))
|
||||
for k, v := range r.Counts {
|
||||
out[k] = int(v)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// Events
|
||||
|
||||
func (c *Client) InsertEvent(ctx context.Context, e events.Event) (int64, error) {
|
||||
r, err := c.taskEvent.InsertEvent(ctx, &pb.InsertEventRequest{Event: eventProto(e)})
|
||||
if err != nil {
|
||||
return 0, unmapError(err)
|
||||
}
|
||||
return r.Id, nil
|
||||
}
|
||||
|
||||
func (c *Client) EventsSince(ctx context.Context, cursor string, limit int) ([]events.Event, error) {
|
||||
r, err := c.taskEvent.EventsSince(ctx, &pb.EventsSinceRequest{SinceId: cursor, Limit: int64(limit)})
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
return mapValues(r.Events, eventValue), nil
|
||||
}
|
||||
|
||||
func (c *Client) TaskEvents(ctx context.Context, taskID int64) ([]events.Event, error) {
|
||||
r, err := c.taskEvent.TaskEvents(ctx, &pb.TaskEventsRequest{TaskId: taskID})
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
return mapValues(r.Events, eventValue), nil
|
||||
}
|
||||
|
||||
func (c *Client) WorkflowStats(ctx context.Context) ([]storecontract.WorkflowStat, error) {
|
||||
r, err := c.stats.WorkflowStats(ctx, &pb.WorkflowStatsRequest{})
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
return mapValues(r.Stats, workflowStatValue), nil
|
||||
}
|
||||
|
||||
func (c *Client) StageStats(ctx context.Context) ([]storecontract.StageStat, error) {
|
||||
r, err := c.stats.StageStats(ctx, &pb.StageStatsRequest{})
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
return mapValues(r.Stats, stageStatValue), nil
|
||||
}
|
||||
|
||||
func (c *Client) TokensByDay(ctx context.Context, days int) ([]storecontract.DayTokens, error) {
|
||||
r, err := c.stats.TokensByDay(ctx, &pb.TokensByDayRequest{Days: int64(days)})
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
return mapValues(r.Tokens, dayTokensValue), nil
|
||||
}
|
||||
|
||||
// Capture
|
||||
|
||||
func (c *Client) InsertCapture(ctx context.Context, ce storecontract.CapturedEvent, retention time.Duration, maxEvents int) (string, error) {
|
||||
r, err := c.capture.InsertCapture(ctx, &pb.InsertCaptureRequest{Capture: capturedEventProto(ce), RetentionSeconds: int64(retention.Seconds()), MaxEvents: int64(maxEvents)})
|
||||
if err != nil {
|
||||
return "", unmapError(err)
|
||||
}
|
||||
return r.Id, 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
|
||||
}
|
||||
|
||||
// RecordCaptureRefusal counts one event the network rules refused.
|
||||
func (c *Client) RecordCaptureRefusal(ctx context.Context, source, addr string, at time.Time) error {
|
||||
_, err := c.capture.RecordCaptureRefusal(ctx, &pb.RecordCaptureRefusalRequest{Source: source, Addr: addr, RefusedAt: timestamp(at)})
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
// CaptureRefusals reads each source's refusals since a time.
|
||||
func (c *Client) CaptureRefusals(ctx context.Context, since time.Time) ([]storecontract.CaptureRefusals, error) {
|
||||
reply, err := c.capture.ListCaptureRefusals(ctx, &pb.ListCaptureRefusalsRequest{Since: timestamp(since)})
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
out := make([]storecontract.CaptureRefusals, 0, len(reply.Sources))
|
||||
for _, s := range reply.Sources {
|
||||
out = append(out, storecontract.CaptureRefusals{Source: s.Source, Refused: s.Refused, Accepted: s.Accepted, Addrs: s.Addrs})
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// ListCaptures streams one capture per message; see StreamCaptures in
|
||||
// server.go.
|
||||
func (c *Client) ListCaptures(ctx context.Context, limit int) ([]storecontract.CapturedEvent, error) {
|
||||
stream, err := c.capture.StreamCaptures(ctx, &pb.StreamCapturesRequest{Limit: int64(limit)})
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
var captures []storecontract.CapturedEvent
|
||||
for {
|
||||
r, err := stream.Recv()
|
||||
if errors.Is(err, io.EOF) {
|
||||
return captures, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
captures = append(captures, capturedEventValue(r.Capture))
|
||||
}
|
||||
}
|
||||
|
||||
// Mapping
|
||||
|
||||
func (c *Client) InsertMapping(ctx context.Context, m mapping.Mapping) (string, error) {
|
||||
r, err := c.mapping.InsertMapping(ctx, &pb.InsertMappingRequest{Mapping: mappingProto(m)})
|
||||
if err != nil {
|
||||
return "", unmapError(err)
|
||||
}
|
||||
return r.Id, nil
|
||||
}
|
||||
|
||||
func (c *Client) GetMapping(ctx context.Context, id string) (*mapping.Mapping, error) {
|
||||
r, err := c.mapping.GetMapping(ctx, &pb.GetMappingRequest{Id: id})
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
if !r.Found {
|
||||
return nil, nil
|
||||
}
|
||||
v := mappingValue(r.Mapping)
|
||||
return &v, nil
|
||||
}
|
||||
|
||||
func (c *Client) ListMappings(ctx context.Context) ([]mapping.Mapping, error) {
|
||||
r, err := c.mapping.ListMappings(ctx, &pb.ListMappingsRequest{})
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
return mapValues(r.Mappings, mappingValue), nil
|
||||
}
|
||||
|
||||
func (c *Client) UpdateMapping(ctx context.Context, m mapping.Mapping) error {
|
||||
_, err := c.mapping.UpdateMapping(ctx, &pb.UpdateMappingRequest{Mapping: mappingProto(m)})
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
func (c *Client) DeleteMapping(ctx context.Context, id string) error {
|
||||
_, err := c.mapping.DeleteMapping(ctx, &pb.DeleteMappingRequest{Id: id})
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
func (c *Client) RecordMappingMatch(ctx context.Context, mappingID, captureID string) error {
|
||||
_, err := c.mapping.RecordMappingMatch(ctx, &pb.RecordMappingMatchRequest{MappingId: mappingID, CaptureId: captureID})
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
// Binding
|
||||
|
||||
func (c *Client) InsertBinding(ctx context.Context, b binding.Binding) (string, error) {
|
||||
r, err := c.binding.InsertBinding(ctx, &pb.InsertBindingRequest{Binding: bindingProto(b)})
|
||||
if err != nil {
|
||||
return "", unmapError(err)
|
||||
}
|
||||
return r.Id, nil
|
||||
}
|
||||
|
||||
func (c *Client) GetBinding(ctx context.Context, id string) (*binding.Binding, error) {
|
||||
r, err := c.binding.GetBinding(ctx, &pb.GetBindingRequest{Id: id})
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
if !r.Found {
|
||||
return nil, nil
|
||||
}
|
||||
v := bindingValue(r.Binding)
|
||||
return &v, nil
|
||||
}
|
||||
|
||||
func (c *Client) ListBindings(ctx context.Context) ([]binding.Binding, error) {
|
||||
r, err := c.binding.ListBindings(ctx, &pb.ListBindingsRequest{})
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
return mapValues(r.Bindings, bindingValue), nil
|
||||
}
|
||||
|
||||
func (c *Client) ListDispatches(ctx context.Context, filter storecontract.DispatchFilter) ([]storecontract.Dispatch, error) {
|
||||
r, err := c.dispatch.ListDispatches(ctx, &pb.ListDispatchesRequest{
|
||||
BindingId: filter.BindingID, CaptureId: filter.CaptureID, TaskId: filter.TaskID, Limit: int32(filter.Limit), // the store clamps it
|
||||
})
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
return mapValues(r.Dispatches, func(d *pb.Dispatch) storecontract.Dispatch {
|
||||
return storecontract.Dispatch{
|
||||
BindingID: d.BindingId, BindingVersion: d.BindingVersion, CaptureID: d.CaptureId,
|
||||
TaskID: d.TaskId, Reason: d.Reason, DispatchedAt: timeValue(d.DispatchedAt),
|
||||
}
|
||||
}), nil
|
||||
}
|
||||
|
||||
func (c *Client) UpdateBinding(ctx context.Context, b binding.Binding) error {
|
||||
_, err := c.binding.UpdateBinding(ctx, &pb.UpdateBindingRequest{Binding: bindingProto(b)})
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
func (c *Client) DeleteBinding(ctx context.Context, id string) error {
|
||||
_, err := c.binding.DeleteBinding(ctx, &pb.DeleteBindingRequest{Id: id})
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
func (c *Client) PauseBinding(ctx context.Context, id string) error {
|
||||
_, err := c.binding.PauseBinding(ctx, &pb.PauseBindingRequest{Id: id})
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
func (c *Client) ResumeBinding(ctx context.Context, id string) error {
|
||||
_, err := c.binding.ResumeBinding(ctx, &pb.ResumeBindingRequest{Id: id})
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
func (c *Client) ApproveBinding(ctx context.Context, id string) error {
|
||||
_, err := c.binding.ApproveBinding(ctx, &pb.ApproveBindingRequest{Id: id})
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
// Source
|
||||
|
||||
func (c *Client) InsertSource(ctx context.Context, s source.Source) error {
|
||||
_, err := c.source.InsertSource(ctx, &pb.InsertSourceRequest{Source: sourceProto(s)})
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
func (c *Client) GetSource(ctx context.Context, path string) (*source.Source, error) {
|
||||
r, err := c.source.GetSource(ctx, &pb.GetSourceRequest{Path: path})
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
if !r.Found {
|
||||
return nil, nil
|
||||
}
|
||||
v := sourceValue(r.Source)
|
||||
return &v, nil
|
||||
}
|
||||
|
||||
func (c *Client) ListSources(ctx context.Context) ([]source.Source, error) {
|
||||
r, err := c.source.ListSources(ctx, &pb.ListSourcesRequest{})
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
return mapValues(r.Sources, sourceValue), nil
|
||||
}
|
||||
|
||||
func (c *Client) SetSourceSigning(ctx context.Context, path string, from, to source.Signing) error {
|
||||
_, err := c.source.SetSourceSigning(ctx, &pb.SetSourceSigningRequest{Path: path, From: string(from), To: string(to)})
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
func (c *Client) SetSourceName(ctx context.Context, path, name string) error {
|
||||
_, err := c.source.SetSourceName(ctx, &pb.SetSourceNameRequest{Path: path, Name: name})
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
func (c *Client) SetSourceDeliveryHeader(ctx context.Context, path, header string) error {
|
||||
_, err := c.source.SetSourceDeliveryHeader(ctx, &pb.SetSourceDeliveryHeaderRequest{Path: path, Header: header})
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
func (c *Client) DeleteSource(ctx context.Context, path string) error {
|
||||
_, err := c.source.DeleteSource(ctx, &pb.DeleteSourceRequest{Path: path})
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
func (c *Client) SetSourceSecret(ctx context.Context, path, secret string) error {
|
||||
_, err := c.source.SetSourceSecret(ctx, &pb.SetSourceSecretRequest{Path: path, Secret: secret})
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
// Dispatch
|
||||
|
||||
func (c *Client) ArmedBindingsForSource(ctx context.Context, source string) ([]binding.Binding, error) {
|
||||
r, err := c.binding.ArmedBindingsForSource(ctx, &pb.ArmedBindingsForSourceRequest{Source: source})
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
return mapValues(r.Bindings, bindingValue), nil
|
||||
}
|
||||
|
||||
func (c *Client) RecordDispatch(ctx context.Context, bindingID string, bindingVersion int64, captureID string, taskID int64, reason string) error {
|
||||
_, err := c.dispatch.RecordDispatch(ctx, &pb.RecordDispatchRequest{BindingId: bindingID, BindingVersion: bindingVersion, CaptureId: captureID, TaskId: taskID, Reason: reason})
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
// Playbook dispatch
|
||||
|
||||
func (c *Client) RecordPlaybookDispatch(ctx context.Context, playbookID, playbookVersion, eventID, actionID string) error {
|
||||
_, err := c.dispatch.RecordPlaybookDispatch(ctx, &pb.RecordPlaybookDispatchRequest{PlaybookId: playbookID, PlaybookVersion: playbookVersion, EventId: eventID, ActionId: actionID})
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
func (c *Client) DeletePlaybookDispatches(ctx context.Context, playbookID string) error {
|
||||
_, err := c.dispatch.DeletePlaybookDispatches(ctx, &pb.DeletePlaybookDispatchesRequest{PlaybookId: playbookID})
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
// ListUndispatchedCaptures streams for the same reason as ListCaptures.
|
||||
func (c *Client) ListUndispatchedCaptures(ctx context.Context, bindings []string, limit int) ([]storecontract.CapturedEvent, error) {
|
||||
stream, err := c.capture.StreamUndispatchedCaptures(ctx, &pb.StreamUndispatchedCapturesRequest{Bindings: bindings, Limit: int64(limit)})
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
var captures []storecontract.CapturedEvent
|
||||
for {
|
||||
r, err := stream.Recv()
|
||||
if errors.Is(err, io.EOF) {
|
||||
return captures, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
captures = append(captures, capturedEventValue(r.Capture))
|
||||
}
|
||||
}
|
||||
|
||||
// BindingTaskCreator
|
||||
|
||||
func (c *Client) EnqueueBindingTask(ctx context.Context, owner, repo, title, body, wf, identity, bindingID string, bindingVersion int, captureID string, inputs map[string]any) (*task.Task, error) {
|
||||
encoded, err := task.EncodeInputs(inputs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
r, err := c.dispatch.EnqueueBindingTask(ctx, &pb.EnqueueBindingTaskRequest{Owner: owner, Repo: repo, Title: title, Body: body, Workflow: wf, Identity: identity, BindingId: bindingID, BindingVersion: int64(bindingVersion), InputsJson: encoded, CaptureId: captureID})
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
return taskValue(r.Task), nil
|
||||
}
|
||||
|
||||
// WorkflowCaller: the workflow.call step's two RPCs. StartCall encodes the
|
||||
// call's inputs in the same form every other wire crossing uses; CallStatus
|
||||
// rehydrates the sentinels the server maps, so a caller's errors.Is(err,
|
||||
// storecontract.ErrCallNotYours) works across the wire.
|
||||
|
||||
func (c *Client) StartCall(ctx context.Context, callerTaskID int64, callKey, wf string, inputs map[string]any) (*task.Task, error) {
|
||||
encoded, err := task.EncodeInputs(inputs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
r, err := c.task.EnqueueCallTask(ctx, &pb.EnqueueCallTaskRequest{CallerTaskId: callerTaskID, CallKey: callKey, Workflow: wf, InputsJson: encoded})
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
return taskValue(r.Task), nil
|
||||
}
|
||||
|
||||
func (c *Client) CallStatus(ctx context.Context, callerTaskID, callTaskID int64) (string, string, map[string]any, error) {
|
||||
r, err := c.task.WorkflowCallStatus(ctx, &pb.WorkflowCallStatusRequest{CallerTaskId: callerTaskID, CallTaskId: callTaskID})
|
||||
if err != nil {
|
||||
return "", "", nil, unmapError(err)
|
||||
}
|
||||
return r.Status, r.Detail, outputsValue(r.OutputsJson), nil
|
||||
}
|
||||
|
||||
// Task log
|
||||
|
||||
// TaskLog reads one task attempt's decoded log over the wire. found=false with
|
||||
// a nil error is the attempt-has-no-log answer; a process with no reader at
|
||||
// all comes back as an unavailable error, not as an empty page, so a caller
|
||||
// can tell the two apart.
|
||||
func (c *Client) TaskLog(ctx context.Context, taskID int64, attempt int, q logging.Query) (logging.TaskLogPage, error) {
|
||||
r, err := c.taskLog.ReadTaskLog(ctx, taskLogRequestProto(taskID, attempt, q))
|
||||
if err != nil {
|
||||
return logging.TaskLogPage{}, unmapError(err)
|
||||
}
|
||||
if !r.Found {
|
||||
return logging.TaskLogPage{
|
||||
Entries: []logging.Entry{},
|
||||
Components: []string{},
|
||||
}, nil
|
||||
}
|
||||
// Both collections are normalised: protobuf decodes an empty `repeated`
|
||||
// field to nil, and a nil slice marshals to JSON null rather than an empty
|
||||
// list, so the field's type would otherwise depend on whether it happened
|
||||
// to have contents. mapValues owns the entries side.
|
||||
components := r.Components
|
||||
if components == nil {
|
||||
components = []string{}
|
||||
}
|
||||
return logging.TaskLogPage{
|
||||
Entries: mapValues(r.Entries, taskLogEntryValue),
|
||||
Truncated: r.Truncated,
|
||||
MoreAvailable: r.MoreAvailable,
|
||||
Cursor: r.Cursor,
|
||||
File: r.File,
|
||||
Found: true,
|
||||
Components: components,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// TaskLogContent streams one attempt's log verbatim into w, for a download.
|
||||
// found is false, with a nil error, when the attempt has no log file.
|
||||
func (c *Client) TaskLogContent(ctx context.Context, taskID int64, attempt int, w io.Writer) (bool, error) {
|
||||
stream, err := c.taskLog.StreamTaskLogContent(ctx, &pb.StreamTaskLogContentRequest{TaskId: taskID, Attempt: int64(attempt)})
|
||||
if err != nil {
|
||||
return false, unmapError(err)
|
||||
}
|
||||
found := false
|
||||
for {
|
||||
r, err := stream.Recv()
|
||||
if errors.Is(err, io.EOF) {
|
||||
return found, nil
|
||||
}
|
||||
if err != nil {
|
||||
return found, unmapError(err)
|
||||
}
|
||||
found = found || r.Found
|
||||
if len(r.Chunk) == 0 {
|
||||
continue
|
||||
}
|
||||
if _, err := w.Write(r.Chunk); err != nil {
|
||||
return found, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// GetHarnessSecret returns the stored OAuth token set for an org/service
|
||||
// credential binding, or storecontract.ErrHarnessSecretNotFound.
|
||||
func (c *Client) GetHarnessSecret(ctx context.Context, org, service string) (harnesssecret.Secret, error) {
|
||||
r, err := c.harnessSecret.GetHarnessSecret(ctx, &pb.GetHarnessSecretRequest{Service: service, RunCredential: harnesssecret.RunCredential(ctx)})
|
||||
if err != nil {
|
||||
return harnesssecret.Secret{}, unmapError(err)
|
||||
}
|
||||
if r.Org != org {
|
||||
return harnesssecret.Secret{}, status.Error(codes.PermissionDenied, "harness secret belongs to another org")
|
||||
}
|
||||
value := harnessSecretValue(r.Secret)
|
||||
value.Org = r.Org
|
||||
return value, nil
|
||||
}
|
||||
|
||||
// PutHarnessSecret upserts an org/service credential binding's OAuth token
|
||||
// set, called by the setup terminal's first capture and by the egress
|
||||
// proxy's every refresh.
|
||||
func (c *Client) PutHarnessSecret(ctx context.Context, s harnesssecret.Secret) error {
|
||||
_, err := c.harnessSecret.PutHarnessSecret(ctx, &pb.PutHarnessSecretRequest{Secret: harnessSecretProto(s), RunCredential: harnesssecret.RunCredential(ctx)})
|
||||
return unmapError(err)
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
package staterpc
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
|
||||
pb "github.com/samcharles93/archie-core/internal/contracts/state/v1"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
|
||||
)
|
||||
|
||||
type dispatchServer struct {
|
||||
pb.UnimplementedDispatchServiceServer
|
||||
*server
|
||||
}
|
||||
|
||||
func (s *dispatchServer) RecordDispatch(ctx context.Context, r *pb.RecordDispatchRequest) (*pb.RecordDispatchResponse, error) {
|
||||
if s.deps.BindingDispatcher == nil {
|
||||
return nil, errBindingDispatchUnavailable
|
||||
}
|
||||
if err := s.deps.BindingDispatcher.RecordDispatch(ctx, r.BindingId, r.BindingVersion, r.CaptureId, r.TaskId, r.Reason); err != nil {
|
||||
return nil, s.logErr("RecordDispatch", err)
|
||||
}
|
||||
return &pb.RecordDispatchResponse{}, nil
|
||||
}
|
||||
|
||||
func (s *dispatchServer) ListDispatches(ctx context.Context, r *pb.ListDispatchesRequest) (*pb.ListDispatchesResponse, error) {
|
||||
bs, err := s.binding()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ledger, ok := bs.(storecontract.DispatchLedger)
|
||||
if !ok {
|
||||
return nil, errBindingDispatchUnavailable
|
||||
}
|
||||
dispatches, err := ledger.ListDispatches(ctx, storecontract.DispatchFilter{
|
||||
BindingID: r.BindingId, CaptureID: r.CaptureId, TaskID: r.TaskId, Limit: int(r.Limit),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, s.logErr("ListDispatches", err)
|
||||
}
|
||||
return &pb.ListDispatchesResponse{Dispatches: mapValues(dispatches, func(d storecontract.Dispatch) *pb.Dispatch {
|
||||
return &pb.Dispatch{
|
||||
BindingId: d.BindingID, BindingVersion: d.BindingVersion, CaptureId: d.CaptureID,
|
||||
TaskId: d.TaskID, Reason: d.Reason, DispatchedAt: timestamp(d.DispatchedAt),
|
||||
}
|
||||
})}, nil
|
||||
}
|
||||
|
||||
// Playbook dispatch
|
||||
|
||||
func (s *dispatchServer) RecordPlaybookDispatch(ctx context.Context, r *pb.RecordPlaybookDispatchRequest) (*pb.RecordPlaybookDispatchResponse, error) {
|
||||
if s.deps.PlaybookDispatcher == nil {
|
||||
return nil, errPlaybookDispatcherUnavailable
|
||||
}
|
||||
if err := s.deps.PlaybookDispatcher.RecordPlaybookDispatch(ctx, r.PlaybookId, r.PlaybookVersion, r.EventId, r.ActionId); err != nil {
|
||||
return nil, s.logErr("RecordPlaybookDispatch", err)
|
||||
}
|
||||
return &pb.RecordPlaybookDispatchResponse{}, nil
|
||||
}
|
||||
|
||||
func (s *dispatchServer) DeletePlaybookDispatches(ctx context.Context, r *pb.DeletePlaybookDispatchesRequest) (*pb.DeletePlaybookDispatchesResponse, error) {
|
||||
if s.deps.PlaybookDispatcher == nil {
|
||||
return nil, errPlaybookDispatcherUnavailable
|
||||
}
|
||||
if err := s.deps.PlaybookDispatcher.DeletePlaybookDispatches(ctx, r.PlaybookId); err != nil {
|
||||
return nil, s.logErr("DeletePlaybookDispatches", err)
|
||||
}
|
||||
return &pb.DeletePlaybookDispatchesResponse{}, nil
|
||||
}
|
||||
|
||||
func (s *dispatchServer) EnqueueBindingTask(ctx context.Context, r *pb.EnqueueBindingTaskRequest) (*pb.EnqueueBindingTaskResponse, error) {
|
||||
if s.deps.BindingTaskCreator == nil {
|
||||
return nil, errBindingTaskCreatorUnavailable
|
||||
}
|
||||
inputs, err := task.DecodeInputs(r.InputsJson)
|
||||
if err != nil {
|
||||
return nil, status.Error(codes.InvalidArgument, err.Error())
|
||||
}
|
||||
t, err := s.deps.BindingTaskCreator.EnqueueBindingTask(ctx, r.Owner, r.Repo, r.Title, r.Body, r.Workflow, r.Identity, r.BindingId, int(r.BindingVersion), r.CaptureId, inputs)
|
||||
if err != nil {
|
||||
return nil, s.logErr("EnqueueBindingTask", err)
|
||||
}
|
||||
return &pb.EnqueueBindingTaskResponse{Task: taskProto(t)}, nil
|
||||
}
|
||||
|
||||
// WorkflowCaller: a workflow.call step starts its callee and reads it back
|
||||
// while waiting. The grant already named the
|
||||
// caller; the store's own row checks are what make both calls answer only
|
||||
// for this caller's own callees.
|
||||
|
||||
func (c *Client) ListDispatches(ctx context.Context, filter storecontract.DispatchFilter) ([]storecontract.Dispatch, error) {
|
||||
r, err := c.dispatch.ListDispatches(ctx, &pb.ListDispatchesRequest{
|
||||
BindingId: filter.BindingID, CaptureId: filter.CaptureID, TaskId: filter.TaskID, Limit: int32(filter.Limit), // the store clamps it
|
||||
})
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
return mapValues(r.Dispatches, func(d *pb.Dispatch) storecontract.Dispatch {
|
||||
return storecontract.Dispatch{
|
||||
BindingID: d.BindingId, BindingVersion: d.BindingVersion, CaptureID: d.CaptureId,
|
||||
TaskID: d.TaskId, Reason: d.Reason, DispatchedAt: timeValue(d.DispatchedAt),
|
||||
}
|
||||
}), nil
|
||||
}
|
||||
|
||||
func (c *Client) RecordDispatch(ctx context.Context, bindingID string, bindingVersion int64, captureID string, taskID int64, reason string) error {
|
||||
_, err := c.dispatch.RecordDispatch(ctx, &pb.RecordDispatchRequest{BindingId: bindingID, BindingVersion: bindingVersion, CaptureId: captureID, TaskId: taskID, Reason: reason})
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
// Playbook dispatch
|
||||
|
||||
func (c *Client) RecordPlaybookDispatch(ctx context.Context, playbookID, playbookVersion, eventID, actionID string) error {
|
||||
_, err := c.dispatch.RecordPlaybookDispatch(ctx, &pb.RecordPlaybookDispatchRequest{PlaybookId: playbookID, PlaybookVersion: playbookVersion, EventId: eventID, ActionId: actionID})
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
func (c *Client) DeletePlaybookDispatches(ctx context.Context, playbookID string) error {
|
||||
_, err := c.dispatch.DeletePlaybookDispatches(ctx, &pb.DeletePlaybookDispatchesRequest{PlaybookId: playbookID})
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
func (c *Client) EnqueueBindingTask(ctx context.Context, owner, repo, title, body, wf, identity, bindingID string, bindingVersion int, captureID string, inputs map[string]any) (*task.Task, error) {
|
||||
encoded, err := task.EncodeInputs(inputs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
r, err := c.dispatch.EnqueueBindingTask(ctx, &pb.EnqueueBindingTaskRequest{Owner: owner, Repo: repo, Title: title, Body: body, Workflow: wf, Identity: identity, BindingId: bindingID, BindingVersion: int64(bindingVersion), InputsJson: encoded, CaptureId: captureID})
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
return taskValue(r.Task), nil
|
||||
}
|
||||
|
||||
// WorkflowCaller: the workflow.call step's two RPCs. StartCall encodes the
|
||||
// call's inputs in the same form every other wire crossing uses; CallStatus
|
||||
// rehydrates the sentinels the server maps, so a caller's errors.Is(err,
|
||||
// storecontract.ErrCallNotYours) works across the wire.
|
||||
@@ -143,3 +143,8 @@ func (c *Client) ListEventTypes(ctx context.Context) ([]eventtype.EventType, err
|
||||
}
|
||||
return mapValues(r.EventTypes, eventTypeValue), nil
|
||||
}
|
||||
|
||||
type eventTypeServer struct {
|
||||
pb.UnimplementedEventTypeServiceServer
|
||||
*server
|
||||
}
|
||||
@@ -0,0 +1,159 @@
|
||||
package staterpc
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
|
||||
pb "github.com/samcharles93/archie-core/internal/contracts/state/v1"
|
||||
"github.com/samcharles93/archie-core/internal/domain/org"
|
||||
"github.com/samcharles93/archie-core/internal/domain/usage"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
"github.com/samcharles93/archie-core/internal/taskstate"
|
||||
)
|
||||
|
||||
type executionServer struct {
|
||||
pb.UnimplementedExecutionServiceServer
|
||||
*server
|
||||
}
|
||||
|
||||
// StartStep and FinishStep write a step transition and return its event.
|
||||
func (s *executionServer) StartStep(ctx context.Context, r *pb.StartStepRequest) (*pb.StartStepResponse, error) {
|
||||
if s.deps.Steps == nil {
|
||||
return nil, status.Error(codes.Unavailable, "step recorder unavailable")
|
||||
}
|
||||
stepID, event, err := s.deps.Steps.StartStep(ctx, task.StepStart{
|
||||
ExecutionID: r.ExecutionId, Attempt: int(r.Attempt),
|
||||
ParentID: r.ParentId, Kind: r.Kind, Name: r.Name,
|
||||
CalledExecutionID: r.CalledExecutionId,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, s.logErr("StartStep", err)
|
||||
}
|
||||
return &pb.StartStepResponse{StepId: stepID, Event: eventProto(event)}, nil
|
||||
}
|
||||
|
||||
// RecordUsage appends one model call's usage record.
|
||||
func (s *executionServer) RecordUsage(ctx context.Context, r *pb.RecordUsageRequest) (*pb.RecordUsageResponse, error) {
|
||||
if s.deps.Usage == nil {
|
||||
return nil, status.Error(codes.Unavailable, "usage recorder unavailable")
|
||||
}
|
||||
if r.Record == nil {
|
||||
return nil, status.Error(codes.InvalidArgument, "record is required")
|
||||
}
|
||||
record := usageValue(r.Record)
|
||||
// The caller never names the org: a task's is its row's, which the store
|
||||
// applies; anything else is the calling principal's.
|
||||
record.Org = string(org.OrgFromContext(ctx))
|
||||
if err := s.deps.Usage.RecordUsage(ctx, record); err != nil {
|
||||
return nil, s.logErr("RecordUsage", err)
|
||||
}
|
||||
return &pb.RecordUsageResponse{}, nil
|
||||
}
|
||||
|
||||
// CancelExecution is the one cancel path:
|
||||
// the store records the cancellation and returns the callees it cancelled
|
||||
// with it; the caller delivers it by cancelling the runs' in-memory contexts
|
||||
// afterwards.
|
||||
func (s *executionServer) CancelExecution(ctx context.Context, r *pb.CancelExecutionRequest) (*pb.CancelExecutionResponse, error) {
|
||||
if s.deps.Canceller == nil {
|
||||
return nil, status.Error(codes.Unavailable, "execution canceller unavailable")
|
||||
}
|
||||
ids, err := s.deps.Canceller.CancelExecution(ctx, r.TaskId, r.Reason, r.To)
|
||||
if err != nil {
|
||||
return nil, s.logErr("CancelExecution", err)
|
||||
}
|
||||
return &pb.CancelExecutionResponse{CancelledCalleeIds: ids}, nil
|
||||
}
|
||||
|
||||
func (s *executionServer) FinishStep(ctx context.Context, r *pb.FinishStepRequest) (*pb.FinishStepResponse, error) {
|
||||
if s.deps.Steps == nil {
|
||||
return nil, status.Error(codes.Unavailable, "step recorder unavailable")
|
||||
}
|
||||
event, err := s.deps.Steps.FinishStep(ctx, task.StepFinish{
|
||||
StepID: r.StepId, ExecutionID: r.ExecutionId,
|
||||
From: taskstate.StepStatus(r.From), To: taskstate.StepStatus(r.To),
|
||||
Detail: r.Detail, TokensUsed: r.TokensUsed, Results: r.Results,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, s.logErr("FinishStep", err)
|
||||
}
|
||||
return &pb.FinishStepResponse{Event: eventProto(event)}, nil
|
||||
}
|
||||
|
||||
func (s *executionServer) ListSteps(ctx context.Context, r *pb.ListStepsRequest) (*pb.ListStepsResponse, error) {
|
||||
if s.deps.StepReader == nil {
|
||||
return nil, status.Error(codes.Unavailable, "step reader unavailable")
|
||||
}
|
||||
steps, err := s.deps.StepReader.ListSteps(ctx, r.ExecutionId, int(r.Attempt))
|
||||
if err != nil {
|
||||
return nil, s.logErr("ListSteps", err)
|
||||
}
|
||||
return &pb.ListStepsResponse{Steps: mapValues(steps, stepExecutionProto)}, nil
|
||||
}
|
||||
|
||||
// RecordUsage appends one model call's usage record.
|
||||
func (c *Client) RecordUsage(ctx context.Context, r usage.Record) error {
|
||||
_, err := c.execution.RecordUsage(ctx, &pb.RecordUsageRequest{Record: usageProto(r)})
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
// StartStep and FinishStep record step executions and return the persisted
|
||||
// event.
|
||||
func (c *Client) StartStep(ctx context.Context, s task.StepStart) (int64, events.Event, error) {
|
||||
r, err := c.execution.StartStep(ctx, &pb.StartStepRequest{
|
||||
ExecutionId: s.ExecutionID, Attempt: int64(s.Attempt),
|
||||
ParentId: s.ParentID, Kind: s.Kind, Name: s.Name,
|
||||
CalledExecutionId: s.CalledExecutionID,
|
||||
})
|
||||
if err != nil {
|
||||
return 0, events.Event{}, unmapError(err)
|
||||
}
|
||||
event := events.Event{}
|
||||
if r.Event != nil {
|
||||
event = eventValue(r.Event)
|
||||
}
|
||||
return r.StepId, event, nil
|
||||
}
|
||||
|
||||
// CancelExecution is the one cancel path:
|
||||
// the State Store records the cancellation -- every non-terminal step of the
|
||||
// current attempt plus the execution's own move, and its waited-on callees --
|
||||
// and returns the callees it cancelled; the caller cancels the runs'
|
||||
// in-memory contexts afterwards.
|
||||
func (c *Client) CancelExecution(ctx context.Context, taskID int64, reason, to string) ([]int64, error) {
|
||||
r, err := c.execution.CancelExecution(ctx, &pb.CancelExecutionRequest{TaskId: taskID, Reason: reason, To: to})
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
return r.CancelledCalleeIds, nil
|
||||
}
|
||||
|
||||
func (c *Client) FinishStep(ctx context.Context, s task.StepFinish) (events.Event, error) {
|
||||
r, err := c.execution.FinishStep(ctx, &pb.FinishStepRequest{
|
||||
ExecutionId: s.ExecutionID, StepId: s.StepID,
|
||||
From: string(s.From), To: string(s.To), Detail: s.Detail, TokensUsed: s.TokensUsed, Results: s.Results,
|
||||
})
|
||||
if err != nil {
|
||||
return events.Event{}, unmapError(err)
|
||||
}
|
||||
event := events.Event{}
|
||||
if r.Event != nil {
|
||||
event = eventValue(r.Event)
|
||||
}
|
||||
return event, nil
|
||||
}
|
||||
|
||||
func (c *Client) ListSteps(ctx context.Context, executionID int64, attempt int) ([]task.StepExecution, error) {
|
||||
r, err := c.execution.ListSteps(ctx, &pb.ListStepsRequest{ExecutionId: executionID, Attempt: int64(attempt)})
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
steps := make([]task.StepExecution, len(r.Steps))
|
||||
for i, s := range r.Steps {
|
||||
steps[i] = stepExecutionValue(s)
|
||||
}
|
||||
return steps, nil
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
package staterpc
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
|
||||
pb "github.com/samcharles93/archie-core/internal/contracts/state/v1"
|
||||
"github.com/samcharles93/archie-core/internal/domain/harnesssecret"
|
||||
"github.com/samcharles93/archie-core/internal/domain/org"
|
||||
)
|
||||
|
||||
type harnessSecretServer struct {
|
||||
pb.UnimplementedHarnessSecretServiceServer
|
||||
*server
|
||||
}
|
||||
|
||||
// harnessOrg derives scope from a live run credential or the authenticated caller.
|
||||
func (s *server) harnessOrg(ctx context.Context, credential string) (string, error) {
|
||||
if credential == "" {
|
||||
return string(org.OrgFromContext(ctx)), nil
|
||||
}
|
||||
if s.deps.Grants == nil || s.deps.Tasks == nil {
|
||||
return "", status.Error(codes.Unavailable, "task grants unavailable")
|
||||
}
|
||||
id, err := s.deps.Grants.taskFor(ctx, credential)
|
||||
if err != nil {
|
||||
return "", status.Error(codes.Unavailable, "run credentials unavailable")
|
||||
}
|
||||
if id == 0 {
|
||||
return "", status.Error(codes.Unauthenticated, "invalid task grant")
|
||||
}
|
||||
t, err := s.deps.Tasks.TaskByID(ctx, id)
|
||||
if err != nil {
|
||||
return "", mapError(err)
|
||||
}
|
||||
target := t.OrgOrDefault()
|
||||
if scope, ok := org.Scope(ctx); ok && string(scope) != target {
|
||||
return "", status.Error(codes.PermissionDenied, "harness secret belongs to another org")
|
||||
}
|
||||
return target, nil
|
||||
}
|
||||
|
||||
func (s *harnessSecretServer) GetHarnessSecret(ctx context.Context, r *pb.GetHarnessSecretRequest) (*pb.GetHarnessSecretResponse, error) {
|
||||
if s.deps.HarnessSecrets == nil {
|
||||
return nil, status.Error(codes.Unavailable, "harness secret store unavailable")
|
||||
}
|
||||
orgID, err := s.harnessOrg(ctx, r.RunCredential)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
secret, err := s.deps.HarnessSecrets.GetHarnessSecret(ctx, orgID, r.Service)
|
||||
if err != nil {
|
||||
return nil, s.logErr("GetHarnessSecret", err)
|
||||
}
|
||||
return &pb.GetHarnessSecretResponse{Secret: harnessSecretProto(secret), Org: orgID}, nil
|
||||
}
|
||||
|
||||
func (s *harnessSecretServer) PutHarnessSecret(ctx context.Context, r *pb.PutHarnessSecretRequest) (*pb.PutHarnessSecretResponse, error) {
|
||||
if s.deps.HarnessSecrets == nil {
|
||||
return nil, status.Error(codes.Unavailable, "harness secret store unavailable")
|
||||
}
|
||||
value := harnessSecretValue(r.Secret)
|
||||
orgID, err := s.harnessOrg(ctx, r.RunCredential)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
value.Org = orgID
|
||||
if err := s.deps.HarnessSecrets.PutHarnessSecret(ctx, value); err != nil {
|
||||
return nil, s.logErr("PutHarnessSecret", err)
|
||||
}
|
||||
return &pb.PutHarnessSecretResponse{}, nil
|
||||
}
|
||||
|
||||
// GetHarnessSecret returns the stored OAuth token set for an org/service
|
||||
// credential binding, or storecontract.ErrHarnessSecretNotFound.
|
||||
func (c *Client) GetHarnessSecret(ctx context.Context, org, service string) (harnesssecret.Secret, error) {
|
||||
r, err := c.harnessSecret.GetHarnessSecret(ctx, &pb.GetHarnessSecretRequest{Service: service, RunCredential: harnesssecret.RunCredential(ctx)})
|
||||
if err != nil {
|
||||
return harnesssecret.Secret{}, unmapError(err)
|
||||
}
|
||||
if r.Org != org {
|
||||
return harnesssecret.Secret{}, status.Error(codes.PermissionDenied, "harness secret belongs to another org")
|
||||
}
|
||||
value := harnessSecretValue(r.Secret)
|
||||
value.Org = r.Org
|
||||
return value, nil
|
||||
}
|
||||
|
||||
// PutHarnessSecret upserts an org/service credential binding's OAuth token
|
||||
// set, called by the setup terminal's first capture and by the egress
|
||||
// proxy's every refresh.
|
||||
func (c *Client) PutHarnessSecret(ctx context.Context, s harnesssecret.Secret) error {
|
||||
_, err := c.harnessSecret.PutHarnessSecret(ctx, &pb.PutHarnessSecretRequest{Secret: harnessSecretProto(s), RunCredential: harnesssecret.RunCredential(ctx)})
|
||||
return unmapError(err)
|
||||
}
|
||||
@@ -260,3 +260,8 @@ func identityClientError(err error) error {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
type identityServer struct {
|
||||
pb.UnimplementedIdentityServiceServer
|
||||
*server
|
||||
}
|
||||
@@ -0,0 +1,154 @@
|
||||
package staterpc
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
|
||||
pb "github.com/samcharles93/archie-core/internal/contracts/state/v1"
|
||||
"github.com/samcharles93/archie-core/internal/domain/mapping"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
)
|
||||
|
||||
type mappingServer struct {
|
||||
pb.UnimplementedMappingServiceServer
|
||||
*server
|
||||
}
|
||||
|
||||
func (s *server) mapping() (storecontract.MappingStore, error) {
|
||||
if s.deps.Mappings == nil {
|
||||
return nil, errMappingUnavailable
|
||||
}
|
||||
return s.deps.Mappings, nil
|
||||
}
|
||||
|
||||
func (s *mappingServer) InsertMapping(ctx context.Context, r *pb.InsertMappingRequest) (*pb.InsertMappingResponse, error) {
|
||||
ms, err := s.mapping()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
value := mappingValue(r.Mapping)
|
||||
if err := value.Validate(); err != nil {
|
||||
return nil, validationFailure(err)
|
||||
}
|
||||
id, err := ms.InsertMapping(ctx, value)
|
||||
if err != nil {
|
||||
return nil, s.logErr("InsertMapping", err)
|
||||
}
|
||||
return &pb.InsertMappingResponse{Id: id}, nil
|
||||
}
|
||||
|
||||
func (s *mappingServer) GetMapping(ctx context.Context, r *pb.GetMappingRequest) (*pb.GetMappingResponse, error) {
|
||||
ms, err := s.mapping()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
m, err := ms.GetMapping(ctx, r.Id)
|
||||
if err != nil {
|
||||
return nil, s.logErr("GetMapping", err)
|
||||
}
|
||||
return &pb.GetMappingResponse{Mapping: mappingProtoPtr(m), Found: m != nil}, nil
|
||||
}
|
||||
|
||||
func (s *mappingServer) ListMappings(ctx context.Context, _ *pb.ListMappingsRequest) (*pb.ListMappingsResponse, error) {
|
||||
ms, err := s.mapping()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
mappings, err := ms.ListMappings(ctx)
|
||||
if err != nil {
|
||||
return nil, s.logErr("ListMappings", err)
|
||||
}
|
||||
return &pb.ListMappingsResponse{Mappings: mapValues(mappings, mappingProto)}, nil
|
||||
}
|
||||
|
||||
func (s *mappingServer) UpdateMapping(ctx context.Context, r *pb.UpdateMappingRequest) (*pb.UpdateMappingResponse, error) {
|
||||
ms, err := s.mapping()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
value := mappingValue(r.Mapping)
|
||||
if err := value.Validate(); err != nil {
|
||||
return nil, validationFailure(err)
|
||||
}
|
||||
if err := ms.UpdateMapping(ctx, value); err != nil {
|
||||
return nil, s.logErr("UpdateMapping", err)
|
||||
}
|
||||
return &pb.UpdateMappingResponse{}, nil
|
||||
}
|
||||
|
||||
func (s *mappingServer) DeleteMapping(ctx context.Context, r *pb.DeleteMappingRequest) (*pb.DeleteMappingResponse, error) {
|
||||
ms, err := s.mapping()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := ms.DeleteMapping(ctx, r.Id); err != nil {
|
||||
return nil, s.logErr("DeleteMapping", err)
|
||||
}
|
||||
return &pb.DeleteMappingResponse{}, nil
|
||||
}
|
||||
|
||||
func (s *mappingServer) RecordMappingMatch(ctx context.Context, r *pb.RecordMappingMatchRequest) (*pb.RecordMappingMatchResponse, error) {
|
||||
if s.deps.MappingMatches == nil {
|
||||
return nil, status.Error(codes.Unavailable, "mapping match recorder unavailable")
|
||||
}
|
||||
if err := s.deps.MappingMatches.RecordMappingMatch(ctx, r.MappingId, r.CaptureId); err != nil {
|
||||
return nil, s.logErr("RecordMappingMatch", err)
|
||||
}
|
||||
return &pb.RecordMappingMatchResponse{}, nil
|
||||
}
|
||||
|
||||
// Binding
|
||||
|
||||
func mappingProtoPtr(m *mapping.Mapping) *pb.Mapping {
|
||||
if m == nil {
|
||||
return nil
|
||||
}
|
||||
return mappingProto(*m)
|
||||
}
|
||||
|
||||
func (c *Client) InsertMapping(ctx context.Context, m mapping.Mapping) (string, error) {
|
||||
r, err := c.mapping.InsertMapping(ctx, &pb.InsertMappingRequest{Mapping: mappingProto(m)})
|
||||
if err != nil {
|
||||
return "", unmapError(err)
|
||||
}
|
||||
return r.Id, nil
|
||||
}
|
||||
|
||||
func (c *Client) GetMapping(ctx context.Context, id string) (*mapping.Mapping, error) {
|
||||
r, err := c.mapping.GetMapping(ctx, &pb.GetMappingRequest{Id: id})
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
if !r.Found {
|
||||
return nil, nil
|
||||
}
|
||||
v := mappingValue(r.Mapping)
|
||||
return &v, nil
|
||||
}
|
||||
|
||||
func (c *Client) ListMappings(ctx context.Context) ([]mapping.Mapping, error) {
|
||||
r, err := c.mapping.ListMappings(ctx, &pb.ListMappingsRequest{})
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
return mapValues(r.Mappings, mappingValue), nil
|
||||
}
|
||||
|
||||
func (c *Client) UpdateMapping(ctx context.Context, m mapping.Mapping) error {
|
||||
_, err := c.mapping.UpdateMapping(ctx, &pb.UpdateMappingRequest{Mapping: mappingProto(m)})
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
func (c *Client) DeleteMapping(ctx context.Context, id string) error {
|
||||
_, err := c.mapping.DeleteMapping(ctx, &pb.DeleteMappingRequest{Id: id})
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
func (c *Client) RecordMappingMatch(ctx context.Context, mappingID, captureID string) error {
|
||||
_, err := c.mapping.RecordMappingMatch(ctx, &pb.RecordMappingMatchRequest{MappingId: mappingID, CaptureId: captureID})
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
// Binding
|
||||
@@ -245,3 +245,31 @@ func (s *orgServer) ListOrgAgents(ctx context.Context, r *pb.ListOrgAgentsReques
|
||||
}
|
||||
return &pb.ListOrgAgentsResponse{Agents: out}, nil
|
||||
}
|
||||
|
||||
type orgServer struct {
|
||||
pb.UnimplementedOrgServiceServer
|
||||
*server
|
||||
}
|
||||
|
||||
// CallerModels answers the aliases the calling principal's org may use.
|
||||
func (s *orgServer) CallerModels(ctx context.Context, _ *pb.CallerModelsRequest) (*pb.CallerModelsResponse, error) {
|
||||
if s.deps.OrgModels == nil {
|
||||
return nil, status.Error(codes.Unavailable, "model aliases unavailable")
|
||||
}
|
||||
orgID := string(org.OrgFromContext(ctx))
|
||||
aliases, _, err := s.deps.OrgModels(ctx, orgID)
|
||||
if err != nil {
|
||||
return nil, s.logErr("CallerModels", err)
|
||||
}
|
||||
return &pb.CallerModelsResponse{OrgId: orgID, Aliases: aliases}, nil
|
||||
}
|
||||
|
||||
// CallerModels returns the calling principal's org and the aliases it may
|
||||
// use.
|
||||
func (c *Client) CallerModels(ctx context.Context) (string, map[string]string, error) {
|
||||
r, err := c.org.CallerModels(ctx, &pb.CallerModelsRequest{})
|
||||
if err != nil {
|
||||
return "", nil, unmapError(err)
|
||||
}
|
||||
return r.OrgId, r.Aliases, nil
|
||||
}
|
||||
@@ -328,3 +328,8 @@ func (c *Client) SetPackageUpdatePolicy(ctx context.Context, name, policy string
|
||||
}
|
||||
return packageValue(response.GetPackage())
|
||||
}
|
||||
|
||||
type packageServer struct {
|
||||
pb.UnimplementedPackageServiceServer
|
||||
*server
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package staterpc
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
pb "github.com/samcharles93/archie-core/internal/contracts/state/v1"
|
||||
)
|
||||
|
||||
type reviewServer struct {
|
||||
pb.UnimplementedReviewServiceServer
|
||||
*server
|
||||
}
|
||||
|
||||
func (s *reviewServer) RespondReviewGate(ctx context.Context, r *pb.RespondReviewGateRequest) (*pb.RespondReviewGateResponse, error) {
|
||||
if err := s.deps.Tasks.RespondReviewGate(ctx, r.TaskId, r.FromStatus, r.Gate, r.Rereview, int(r.Cap)); err != nil {
|
||||
return nil, s.logErr("RespondReviewGate", err)
|
||||
}
|
||||
return &pb.RespondReviewGateResponse{}, nil
|
||||
}
|
||||
|
||||
func (s *reviewServer) BeginRemediation(ctx context.Context, r *pb.BeginRemediationRequest) (*pb.BeginRemediationResponse, error) {
|
||||
if err := s.deps.Tasks.BeginRemediation(ctx, r.TaskId, r.Payload); err != nil {
|
||||
return nil, s.logErr("BeginRemediation", err)
|
||||
}
|
||||
return &pb.BeginRemediationResponse{}, nil
|
||||
}
|
||||
|
||||
func (s *reviewServer) UpdateReviewPayload(ctx context.Context, r *pb.UpdateReviewPayloadRequest) (*pb.UpdateReviewPayloadResponse, error) {
|
||||
if err := s.deps.Tasks.UpdateReviewPayload(ctx, r.TaskId, r.Payload); err != nil {
|
||||
return nil, s.logErr("UpdateReviewPayload", err)
|
||||
}
|
||||
return &pb.UpdateReviewPayloadResponse{}, nil
|
||||
}
|
||||
|
||||
func (s *reviewServer) SetReviewCursors(ctx context.Context, r *pb.SetReviewCursorsRequest) (*pb.SetReviewCursorsResponse, error) {
|
||||
if err := s.deps.Tasks.SetReviewCursors(ctx, r.TaskId, r.ReviewCursor, r.CommentCursor); err != nil {
|
||||
return nil, s.logErr("SetReviewCursors", err)
|
||||
}
|
||||
return &pb.SetReviewCursorsResponse{}, nil
|
||||
}
|
||||
|
||||
// Queries
|
||||
|
||||
func (c *Client) RespondReviewGate(ctx context.Context, taskID int64, fromStatus, gate string, rereview bool, maxRounds int) error {
|
||||
_, err := c.review.RespondReviewGate(ctx, &pb.RespondReviewGateRequest{
|
||||
TaskId: taskID, FromStatus: fromStatus, Gate: gate, Rereview: rereview, Cap: int64(maxRounds),
|
||||
})
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
func (c *Client) BeginRemediation(ctx context.Context, taskID int64, payload string) error {
|
||||
_, err := c.review.BeginRemediation(ctx, &pb.BeginRemediationRequest{TaskId: taskID, Payload: payload})
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
func (c *Client) UpdateReviewPayload(ctx context.Context, taskID int64, payload string) error {
|
||||
_, err := c.review.UpdateReviewPayload(ctx, &pb.UpdateReviewPayloadRequest{TaskId: taskID, Payload: payload})
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
func (c *Client) SetReviewCursors(ctx context.Context, taskID, reviewCursor, commentCursor int64) error {
|
||||
_, err := c.review.SetReviewCursors(ctx, &pb.SetReviewCursorsRequest{TaskId: taskID, ReviewCursor: reviewCursor, CommentCursor: commentCursor})
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
// Queries
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,168 @@
|
||||
package staterpc
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
pb "github.com/samcharles93/archie-core/internal/contracts/state/v1"
|
||||
"github.com/samcharles93/archie-core/internal/domain/source"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
)
|
||||
|
||||
type sourceServer struct {
|
||||
pb.UnimplementedSourceServiceServer
|
||||
*server
|
||||
}
|
||||
|
||||
func (s *server) sources() (storecontract.SourceStore, error) {
|
||||
if s.deps.Sources == nil {
|
||||
return nil, errSourceUnavailable
|
||||
}
|
||||
return s.deps.Sources, nil
|
||||
}
|
||||
|
||||
func (s *sourceServer) InsertSource(ctx context.Context, r *pb.InsertSourceRequest) (*pb.InsertSourceResponse, error) {
|
||||
ss, err := s.sources()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := ss.InsertSource(ctx, sourceValue(r.Source)); err != nil {
|
||||
return nil, s.logErr("InsertSource", err)
|
||||
}
|
||||
return &pb.InsertSourceResponse{}, nil
|
||||
}
|
||||
|
||||
func (s *sourceServer) GetSource(ctx context.Context, r *pb.GetSourceRequest) (*pb.GetSourceResponse, error) {
|
||||
ss, err := s.sources()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
src, err := ss.GetSource(ctx, r.Path)
|
||||
if err != nil {
|
||||
return nil, s.logErr("GetSource", err)
|
||||
}
|
||||
if src == nil {
|
||||
return &pb.GetSourceResponse{}, nil
|
||||
}
|
||||
return &pb.GetSourceResponse{Source: sourceProto(*src), Found: true}, nil
|
||||
}
|
||||
|
||||
func (s *sourceServer) ListSources(ctx context.Context, _ *pb.ListSourcesRequest) (*pb.ListSourcesResponse, error) {
|
||||
ss, err := s.sources()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
list, err := ss.ListSources(ctx)
|
||||
if err != nil {
|
||||
return nil, s.logErr("ListSources", err)
|
||||
}
|
||||
return &pb.ListSourcesResponse{Sources: mapValues(list, sourceProto)}, nil
|
||||
}
|
||||
|
||||
func (s *sourceServer) SetSourceSigning(ctx context.Context, r *pb.SetSourceSigningRequest) (*pb.SetSourceSigningResponse, error) {
|
||||
ss, err := s.sources()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := ss.SetSourceSigning(ctx, r.Path, source.Signing(r.From), source.Signing(r.To)); err != nil {
|
||||
return nil, s.logErr("SetSourceSigning", err)
|
||||
}
|
||||
return &pb.SetSourceSigningResponse{}, nil
|
||||
}
|
||||
|
||||
func (s *sourceServer) SetSourceSecret(ctx context.Context, r *pb.SetSourceSecretRequest) (*pb.SetSourceSecretResponse, error) {
|
||||
ss, err := s.sources()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := ss.SetSourceSecret(ctx, r.Path, r.Secret); err != nil {
|
||||
return nil, s.logErr("SetSourceSecret", err)
|
||||
}
|
||||
return &pb.SetSourceSecretResponse{}, nil
|
||||
}
|
||||
|
||||
func (s *sourceServer) SetSourceName(ctx context.Context, r *pb.SetSourceNameRequest) (*pb.SetSourceNameResponse, error) {
|
||||
ss, err := s.sources()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := ss.SetSourceName(ctx, r.Path, r.Name); err != nil {
|
||||
return nil, s.logErr("SetSourceName", err)
|
||||
}
|
||||
return &pb.SetSourceNameResponse{}, nil
|
||||
}
|
||||
|
||||
func (s *sourceServer) SetSourceDeliveryHeader(ctx context.Context, r *pb.SetSourceDeliveryHeaderRequest) (*pb.SetSourceDeliveryHeaderResponse, error) {
|
||||
ss, err := s.sources()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := ss.SetSourceDeliveryHeader(ctx, r.Path, r.Header); err != nil {
|
||||
return nil, s.logErr("SetSourceDeliveryHeader", err)
|
||||
}
|
||||
return &pb.SetSourceDeliveryHeaderResponse{}, nil
|
||||
}
|
||||
|
||||
func (s *sourceServer) DeleteSource(ctx context.Context, r *pb.DeleteSourceRequest) (*pb.DeleteSourceResponse, error) {
|
||||
ss, err := s.sources()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := ss.DeleteSource(ctx, r.Path); err != nil {
|
||||
return nil, s.logErr("DeleteSource", err)
|
||||
}
|
||||
return &pb.DeleteSourceResponse{}, nil
|
||||
}
|
||||
|
||||
// Dispatch
|
||||
|
||||
func (c *Client) InsertSource(ctx context.Context, s source.Source) error {
|
||||
_, err := c.source.InsertSource(ctx, &pb.InsertSourceRequest{Source: sourceProto(s)})
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
func (c *Client) GetSource(ctx context.Context, path string) (*source.Source, error) {
|
||||
r, err := c.source.GetSource(ctx, &pb.GetSourceRequest{Path: path})
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
if !r.Found {
|
||||
return nil, nil
|
||||
}
|
||||
v := sourceValue(r.Source)
|
||||
return &v, nil
|
||||
}
|
||||
|
||||
func (c *Client) ListSources(ctx context.Context) ([]source.Source, error) {
|
||||
r, err := c.source.ListSources(ctx, &pb.ListSourcesRequest{})
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
return mapValues(r.Sources, sourceValue), nil
|
||||
}
|
||||
|
||||
func (c *Client) SetSourceSigning(ctx context.Context, path string, from, to source.Signing) error {
|
||||
_, err := c.source.SetSourceSigning(ctx, &pb.SetSourceSigningRequest{Path: path, From: string(from), To: string(to)})
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
func (c *Client) SetSourceName(ctx context.Context, path, name string) error {
|
||||
_, err := c.source.SetSourceName(ctx, &pb.SetSourceNameRequest{Path: path, Name: name})
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
func (c *Client) SetSourceDeliveryHeader(ctx context.Context, path, header string) error {
|
||||
_, err := c.source.SetSourceDeliveryHeader(ctx, &pb.SetSourceDeliveryHeaderRequest{Path: path, Header: header})
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
func (c *Client) DeleteSource(ctx context.Context, path string) error {
|
||||
_, err := c.source.DeleteSource(ctx, &pb.DeleteSourceRequest{Path: path})
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
func (c *Client) SetSourceSecret(ctx context.Context, path, secret string) error {
|
||||
_, err := c.source.SetSourceSecret(ctx, &pb.SetSourceSecretRequest{Path: path, Secret: secret})
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
// Dispatch
|
||||
@@ -0,0 +1,93 @@
|
||||
package staterpc
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
pb "github.com/samcharles93/archie-core/internal/contracts/state/v1"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
)
|
||||
|
||||
type statsServer struct {
|
||||
pb.UnimplementedStatsServiceServer
|
||||
*server
|
||||
}
|
||||
|
||||
func (s *statsServer) StatusCounts(ctx context.Context, _ *pb.StatusCountsRequest) (*pb.StatusCountsResponse, error) {
|
||||
counts, err := s.deps.Tasks.StatusCounts(ctx)
|
||||
if err != nil {
|
||||
return nil, s.logErr("StatusCounts", err)
|
||||
}
|
||||
out := make(map[string]int64, len(counts))
|
||||
for k, v := range counts {
|
||||
out[k] = int64(v)
|
||||
}
|
||||
return &pb.StatusCountsResponse{Counts: out}, nil
|
||||
}
|
||||
|
||||
// Events
|
||||
|
||||
func (s *statsServer) WorkflowStats(ctx context.Context, _ *pb.WorkflowStatsRequest) (*pb.WorkflowStatsResponse, error) {
|
||||
stats, err := s.deps.Tasks.WorkflowStats(ctx)
|
||||
if err != nil {
|
||||
return nil, s.logErr("WorkflowStats", err)
|
||||
}
|
||||
return &pb.WorkflowStatsResponse{Stats: mapValues(stats, workflowStatProto)}, nil
|
||||
}
|
||||
|
||||
func (s *statsServer) StageStats(ctx context.Context, _ *pb.StageStatsRequest) (*pb.StageStatsResponse, error) {
|
||||
stats, err := s.deps.Tasks.StageStats(ctx)
|
||||
if err != nil {
|
||||
return nil, s.logErr("StageStats", err)
|
||||
}
|
||||
return &pb.StageStatsResponse{Stats: mapValues(stats, stageStatProto)}, nil
|
||||
}
|
||||
|
||||
func (s *statsServer) TokensByDay(ctx context.Context, r *pb.TokensByDayRequest) (*pb.TokensByDayResponse, error) {
|
||||
tokens, err := s.deps.Tasks.TokensByDay(ctx, int(r.Days))
|
||||
if err != nil {
|
||||
return nil, s.logErr("TokensByDay", err)
|
||||
}
|
||||
return &pb.TokensByDayResponse{Tokens: mapValues(tokens, dayTokensProto)}, nil
|
||||
}
|
||||
|
||||
// Capture
|
||||
|
||||
func (c *Client) StatusCounts(ctx context.Context) (map[string]int, error) {
|
||||
r, err := c.stats.StatusCounts(ctx, &pb.StatusCountsRequest{})
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
out := make(map[string]int, len(r.Counts))
|
||||
for k, v := range r.Counts {
|
||||
out[k] = int(v)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// Events
|
||||
|
||||
func (c *Client) WorkflowStats(ctx context.Context) ([]storecontract.WorkflowStat, error) {
|
||||
r, err := c.stats.WorkflowStats(ctx, &pb.WorkflowStatsRequest{})
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
return mapValues(r.Stats, workflowStatValue), nil
|
||||
}
|
||||
|
||||
func (c *Client) StageStats(ctx context.Context) ([]storecontract.StageStat, error) {
|
||||
r, err := c.stats.StageStats(ctx, &pb.StageStatsRequest{})
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
return mapValues(r.Stats, stageStatValue), nil
|
||||
}
|
||||
|
||||
func (c *Client) TokensByDay(ctx context.Context, days int) ([]storecontract.DayTokens, error) {
|
||||
r, err := c.stats.TokensByDay(ctx, &pb.TokensByDayRequest{Days: int64(days)})
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
return mapValues(r.Tokens, dayTokensValue), nil
|
||||
}
|
||||
|
||||
// Capture
|
||||
@@ -0,0 +1,246 @@
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package staterpc
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
pb "github.com/samcharles93/archie-core/internal/contracts/state/v1"
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
)
|
||||
|
||||
type taskEventServer struct {
|
||||
pb.UnimplementedTaskEventServiceServer
|
||||
*server
|
||||
}
|
||||
|
||||
func (s *taskEventServer) InsertEvent(ctx context.Context, r *pb.InsertEventRequest) (*pb.InsertEventResponse, error) {
|
||||
id, err := s.deps.Tasks.InsertEvent(ctx, eventValue(r.Event))
|
||||
if err != nil {
|
||||
return nil, s.logErr("InsertEvent", err)
|
||||
}
|
||||
return &pb.InsertEventResponse{Id: id}, nil
|
||||
}
|
||||
|
||||
func (s *taskEventServer) EventsSince(ctx context.Context, r *pb.EventsSinceRequest) (*pb.EventsSinceResponse, error) {
|
||||
evs, err := s.deps.Tasks.EventsSince(ctx, r.SinceId, int(r.Limit))
|
||||
if err != nil {
|
||||
return nil, s.logErr("EventsSince", err)
|
||||
}
|
||||
return &pb.EventsSinceResponse{Events: mapValues(evs, eventProto)}, nil
|
||||
}
|
||||
|
||||
func (s *taskEventServer) TaskEvents(ctx context.Context, r *pb.TaskEventsRequest) (*pb.TaskEventsResponse, error) {
|
||||
evs, err := s.deps.Tasks.TaskEvents(ctx, r.TaskId)
|
||||
if err != nil {
|
||||
return nil, s.logErr("TaskEvents", err)
|
||||
}
|
||||
return &pb.TaskEventsResponse{Events: mapValues(evs, eventProto)}, nil
|
||||
}
|
||||
|
||||
func (c *Client) InsertEvent(ctx context.Context, e events.Event) (int64, error) {
|
||||
r, err := c.taskEvent.InsertEvent(ctx, &pb.InsertEventRequest{Event: eventProto(e)})
|
||||
if err != nil {
|
||||
return 0, unmapError(err)
|
||||
}
|
||||
return r.Id, nil
|
||||
}
|
||||
|
||||
func (c *Client) EventsSince(ctx context.Context, cursor string, limit int) ([]events.Event, error) {
|
||||
r, err := c.taskEvent.EventsSince(ctx, &pb.EventsSinceRequest{SinceId: cursor, Limit: int64(limit)})
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
return mapValues(r.Events, eventValue), nil
|
||||
}
|
||||
|
||||
func (c *Client) TaskEvents(ctx context.Context, taskID int64) ([]events.Event, error) {
|
||||
r, err := c.taskEvent.TaskEvents(ctx, &pb.TaskEventsRequest{TaskId: taskID})
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
return mapValues(r.Events, eventValue), nil
|
||||
}
|
||||
@@ -419,3 +419,8 @@ func (c *Client) TaskPrincipalForCredential(ctx context.Context, token string) (
|
||||
t, err := c.TaskByID(ctx, r.TaskId)
|
||||
return t, identity.IdentityID(r.PrincipalId), err
|
||||
}
|
||||
|
||||
type taskGrantServer struct {
|
||||
pb.UnimplementedTaskGrantServiceServer
|
||||
*server
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
package staterpc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
|
||||
pb "github.com/samcharles93/archie-core/internal/contracts/state/v1"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
"github.com/samcharles93/archie-core/internal/logging"
|
||||
)
|
||||
|
||||
type taskLogServer struct {
|
||||
pb.UnimplementedTaskLogServiceServer
|
||||
*server
|
||||
}
|
||||
|
||||
func (s *server) taskLogs() (storecontract.TaskLogStore, error) {
|
||||
if s.deps.TaskLogs == nil {
|
||||
return nil, errTaskLogsUnavailable
|
||||
}
|
||||
return s.deps.TaskLogs, nil
|
||||
}
|
||||
|
||||
func (s *taskLogServer) ReadTaskLog(ctx context.Context, r *pb.ReadTaskLogRequest) (*pb.ReadTaskLogResponse, error) {
|
||||
logs, err := s.taskLogs()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
page, err := logs.TaskLog(ctx, r.TaskId, int(r.Attempt), taskLogQueryValue(r))
|
||||
if err != nil {
|
||||
// A store that cannot read its own state directory keeps its own
|
||||
// identity: this is unavailability, not a missing log, and a caller
|
||||
// has to be able to tell them apart.
|
||||
if errors.Is(err, logging.ErrTaskLogsUnavailable) {
|
||||
return nil, errTaskLogsUnavailable
|
||||
}
|
||||
return nil, s.logErr("ReadTaskLog", err)
|
||||
}
|
||||
if !page.Found {
|
||||
return &pb.ReadTaskLogResponse{Attempt: r.Attempt}, nil
|
||||
}
|
||||
return &pb.ReadTaskLogResponse{
|
||||
Entries: mapValues(page.Entries, taskLogEntryProto),
|
||||
Truncated: page.Truncated,
|
||||
MoreAvailable: page.MoreAvailable,
|
||||
Cursor: page.Cursor,
|
||||
Attempt: r.Attempt,
|
||||
Found: true,
|
||||
Components: page.Components,
|
||||
File: page.File,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// StreamTaskLogContent sends one attempt's log verbatim, one chunk per
|
||||
// message, for a download. A single reply reports found=false with no chunks:
|
||||
// the attempt has no log file, which is a normal state rather than a failure.
|
||||
func (s *taskLogServer) StreamTaskLogContent(r *pb.StreamTaskLogContentRequest, stream pb.TaskLogService_StreamTaskLogContentServer) error {
|
||||
logs, err := s.taskLogs()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// The chunking the client sees is owned by the reader that writes into
|
||||
// this buffer, so the wire never holds a whole log to re-slice it.
|
||||
buf := &chunkWriter{stream: stream, attempt: r.Attempt}
|
||||
found, err := logs.TaskLogContent(stream.Context(), r.TaskId, int(r.Attempt), buf)
|
||||
if err != nil {
|
||||
if errors.Is(err, logging.ErrTaskLogsUnavailable) {
|
||||
return errTaskLogsUnavailable
|
||||
}
|
||||
return s.logErr("StreamTaskLogContent", err)
|
||||
}
|
||||
if err := buf.flush(); err != nil {
|
||||
return err
|
||||
}
|
||||
// A found log with no bytes still reports found: an attempt whose file is
|
||||
// empty did produce a log, and found is what tells the download it may
|
||||
// write an empty file rather than answer 404.
|
||||
if found && buf.sent {
|
||||
return nil
|
||||
}
|
||||
return stream.Send(&pb.StreamTaskLogContentResponse{Attempt: r.Attempt, Found: found})
|
||||
}
|
||||
|
||||
// chunkWriter packs everything a log reader writes into stream-sized messages
|
||||
// on its way through. It exists so the read can be one io.CopyBuffer of the
|
||||
// file into this writer: the reader keeps its own chunk size, and the bytes
|
||||
// are never all held at once.
|
||||
type chunkWriter struct {
|
||||
stream pb.TaskLogService_StreamTaskLogContentServer
|
||||
attempt int64
|
||||
pending []byte
|
||||
// sent records whether any chunk reached the client, which is how the
|
||||
// caller tells "the file was empty" from "the file was read".
|
||||
sent bool
|
||||
}
|
||||
|
||||
// TaskLog reads one task attempt's decoded log over the wire. found=false with
|
||||
// a nil error is the attempt-has-no-log answer; a process with no reader at
|
||||
// all comes back as an unavailable error, not as an empty page, so a caller
|
||||
// can tell the two apart.
|
||||
func (c *Client) TaskLog(ctx context.Context, taskID int64, attempt int, q logging.Query) (logging.TaskLogPage, error) {
|
||||
r, err := c.taskLog.ReadTaskLog(ctx, taskLogRequestProto(taskID, attempt, q))
|
||||
if err != nil {
|
||||
return logging.TaskLogPage{}, unmapError(err)
|
||||
}
|
||||
if !r.Found {
|
||||
return logging.TaskLogPage{
|
||||
Entries: []logging.Entry{},
|
||||
Components: []string{},
|
||||
}, nil
|
||||
}
|
||||
// Both collections are normalised: protobuf decodes an empty `repeated`
|
||||
// field to nil, and a nil slice marshals to JSON null rather than an empty
|
||||
// list, so the field's type would otherwise depend on whether it happened
|
||||
// to have contents. mapValues owns the entries side.
|
||||
components := r.Components
|
||||
if components == nil {
|
||||
components = []string{}
|
||||
}
|
||||
return logging.TaskLogPage{
|
||||
Entries: mapValues(r.Entries, taskLogEntryValue),
|
||||
Truncated: r.Truncated,
|
||||
MoreAvailable: r.MoreAvailable,
|
||||
Cursor: r.Cursor,
|
||||
File: r.File,
|
||||
Found: true,
|
||||
Components: components,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// TaskLogContent streams one attempt's log verbatim into w, for a download.
|
||||
// found is false, with a nil error, when the attempt has no log file.
|
||||
func (c *Client) TaskLogContent(ctx context.Context, taskID int64, attempt int, w io.Writer) (bool, error) {
|
||||
stream, err := c.taskLog.StreamTaskLogContent(ctx, &pb.StreamTaskLogContentRequest{TaskId: taskID, Attempt: int64(attempt)})
|
||||
if err != nil {
|
||||
return false, unmapError(err)
|
||||
}
|
||||
found := false
|
||||
for {
|
||||
r, err := stream.Recv()
|
||||
if errors.Is(err, io.EOF) {
|
||||
return found, nil
|
||||
}
|
||||
if err != nil {
|
||||
return found, unmapError(err)
|
||||
}
|
||||
found = found || r.Found
|
||||
if len(r.Chunk) == 0 {
|
||||
continue
|
||||
}
|
||||
if _, err := w.Write(r.Chunk); err != nil {
|
||||
return found, err
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,496 @@
|
||||
package staterpc
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
pb "github.com/samcharles93/archie-core/internal/contracts/state/v1"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
)
|
||||
|
||||
type taskServer struct {
|
||||
pb.UnimplementedTaskServiceServer
|
||||
*server
|
||||
}
|
||||
|
||||
func (s *taskServer) EnqueueIssue(ctx context.Context, r *pb.EnqueueIssueRequest) (*pb.EnqueueIssueResponse, error) {
|
||||
inserted, err := s.deps.Tasks.EnqueueIssue(ctx, r.Owner, r.Repo, int(r.Number), r.Title, r.Body, r.Labels, r.Identity)
|
||||
if err != nil {
|
||||
return nil, s.logErr("EnqueueIssue", err)
|
||||
}
|
||||
return &pb.EnqueueIssueResponse{Inserted: inserted}, nil
|
||||
}
|
||||
|
||||
func (s *taskServer) ActiveTasksByOrigin(ctx context.Context, r *pb.ActiveTasksByOriginRequest) (*pb.ActiveTasksByOriginResponse, error) {
|
||||
tasks, err := s.deps.Tasks.ActiveTasksByOrigin(ctx, r.Origin)
|
||||
if err != nil {
|
||||
return nil, s.logErr("ActiveTasksByOrigin", err)
|
||||
}
|
||||
out := make([]*pb.Task, 0, len(tasks))
|
||||
for _, t := range tasks {
|
||||
out = append(out, taskProto(t))
|
||||
}
|
||||
return &pb.ActiveTasksByOriginResponse{Tasks: out}, nil
|
||||
}
|
||||
|
||||
func (s *taskServer) UnpostedOutcomes(ctx context.Context, _ *pb.UnpostedOutcomesRequest) (*pb.UnpostedOutcomesResponse, error) {
|
||||
tasks, err := s.deps.Tasks.UnpostedOutcomes(ctx)
|
||||
if err != nil {
|
||||
return nil, s.logErr("UnpostedOutcomes", err)
|
||||
}
|
||||
out := make([]*pb.Task, 0, len(tasks))
|
||||
for _, t := range tasks {
|
||||
out = append(out, taskProto(t))
|
||||
}
|
||||
return &pb.UnpostedOutcomesResponse{Tasks: out}, nil
|
||||
}
|
||||
|
||||
func (s *taskServer) MarkOutcomePosted(ctx context.Context, r *pb.MarkOutcomePostedRequest) (*pb.MarkOutcomePostedResponse, error) {
|
||||
if err := s.deps.Tasks.MarkOutcomePosted(ctx, r.TaskId, r.Status); err != nil {
|
||||
return nil, s.logErr("MarkOutcomePosted", err)
|
||||
}
|
||||
return &pb.MarkOutcomePostedResponse{}, nil
|
||||
}
|
||||
|
||||
func (s *taskServer) EnqueueChatTask(ctx context.Context, r *pb.EnqueueChatTaskRequest) (*pb.EnqueueChatTaskResponse, error) {
|
||||
inputs, err := task.DecodeInputs(r.InputsJson)
|
||||
if err != nil {
|
||||
return nil, status.Error(codes.InvalidArgument, err.Error())
|
||||
}
|
||||
t, err := s.deps.Tasks.EnqueueChatTask(ctx, r.Owner, r.Repo, r.Title, r.Body, r.Workflow, r.Identity, r.Origin, r.DraftYaml, inputs)
|
||||
if err != nil {
|
||||
return nil, s.logErr("EnqueueChatTask", err)
|
||||
}
|
||||
return &pb.EnqueueChatTaskResponse{Task: taskProto(t)}, nil
|
||||
}
|
||||
|
||||
func (s *taskServer) ClaimNext(ctx context.Context, _ *pb.ClaimNextRequest) (*pb.ClaimNextResponse, error) {
|
||||
t, err := s.deps.Tasks.ClaimNext(ctx)
|
||||
if err != nil {
|
||||
return nil, s.logErr("ClaimNext", err)
|
||||
}
|
||||
return &pb.ClaimNextResponse{Task: taskProto(t), Found: t != nil}, nil
|
||||
}
|
||||
|
||||
func (s *taskServer) ClaimByIssue(ctx context.Context, r *pb.ClaimByIssueRequest) (*pb.ClaimByIssueResponse, error) {
|
||||
t, err := s.deps.Tasks.ClaimByIssue(ctx, r.Owner, r.Repo, int(r.Number))
|
||||
if err != nil {
|
||||
return nil, s.logErr("ClaimByIssue", err)
|
||||
}
|
||||
return &pb.ClaimByIssueResponse{Task: taskProto(t), Found: t != nil}, nil
|
||||
}
|
||||
|
||||
func (s *taskServer) Transition(ctx context.Context, r *pb.TransitionRequest) (*pb.TransitionResponse, error) {
|
||||
if err := s.deps.Tasks.Transition(ctx, r.TaskId, r.From, r.To, r.Detail); err != nil {
|
||||
return nil, s.logErr("Transition", err)
|
||||
}
|
||||
return &pb.TransitionResponse{}, nil
|
||||
}
|
||||
|
||||
// TaskModels answers what the task's org resolves models against.
|
||||
func (s *taskServer) TaskModels(ctx context.Context, r *pb.TaskModelsRequest) (*pb.TaskModelsResponse, error) {
|
||||
if s.deps.OrgModels == nil {
|
||||
return nil, status.Error(codes.Unavailable, "model aliases unavailable")
|
||||
}
|
||||
t, err := s.deps.Tasks.TaskByID(ctx, r.TaskId)
|
||||
if err != nil {
|
||||
return nil, s.logErr("TaskModels", err)
|
||||
}
|
||||
if t == nil {
|
||||
return nil, status.Error(codes.NotFound, "task not found")
|
||||
}
|
||||
orgID := t.OrgOrDefault()
|
||||
aliases, providers, err := s.deps.OrgModels(ctx, orgID)
|
||||
if err != nil {
|
||||
return nil, s.logErr("TaskModels", err)
|
||||
}
|
||||
out := make(map[string]*pb.ModelProvider, len(providers))
|
||||
for name, p := range providers {
|
||||
out[name] = modelProviderToPB(p)
|
||||
}
|
||||
return &pb.TaskModelsResponse{OrgId: orgID, Aliases: aliases, Providers: out}, nil
|
||||
}
|
||||
|
||||
// TaskInstructions answers the instance and organisation instructions for the
|
||||
// task's org.
|
||||
func (s *taskServer) TaskInstructions(ctx context.Context, r *pb.TaskInstructionsRequest) (*pb.TaskInstructionsResponse, error) {
|
||||
if s.deps.OrgInstructions == nil {
|
||||
return nil, status.Error(codes.Unavailable, "instructions unavailable")
|
||||
}
|
||||
t, err := s.deps.Tasks.TaskByID(ctx, r.TaskId)
|
||||
if err != nil {
|
||||
return nil, s.logErr("TaskInstructions", err)
|
||||
}
|
||||
if t == nil {
|
||||
return nil, status.Error(codes.NotFound, "task not found")
|
||||
}
|
||||
instance, organisation, err := s.deps.OrgInstructions(ctx, t.OrgOrDefault())
|
||||
if err != nil {
|
||||
return nil, s.logErr("TaskInstructions", err)
|
||||
}
|
||||
return &pb.TaskInstructionsResponse{Instance: instance, Organisation: organisation}, nil
|
||||
}
|
||||
|
||||
func (s *taskServer) Update(ctx context.Context, r *pb.UpdateRequest) (*pb.UpdateResponse, error) {
|
||||
if r.Task == nil {
|
||||
return nil, status.Error(codes.InvalidArgument, "task is required")
|
||||
}
|
||||
if err := s.deps.Tasks.Update(ctx, taskValue(r.Task)); err != nil {
|
||||
return nil, s.logErr("Update", err)
|
||||
}
|
||||
return &pb.UpdateResponse{}, nil
|
||||
}
|
||||
|
||||
func (s *taskServer) Requeue(ctx context.Context, r *pb.RequeueRequest) (*pb.RequeueResponse, error) {
|
||||
if err := s.deps.Tasks.Requeue(ctx, r.TaskId, r.FromStatus, r.Workflow); err != nil {
|
||||
return nil, s.logErr("Requeue", err)
|
||||
}
|
||||
return &pb.RequeueResponse{}, nil
|
||||
}
|
||||
|
||||
func (s *taskServer) ParkTask(ctx context.Context, r *pb.ParkTaskRequest) (*pb.ParkTaskResponse, error) {
|
||||
if err := s.deps.Tasks.ParkTask(ctx, r.TaskId, r.From, r.Detail, r.Class, r.Blocker); err != nil {
|
||||
return nil, s.logErr("ParkTask", err)
|
||||
}
|
||||
return &pb.ParkTaskResponse{}, nil
|
||||
}
|
||||
|
||||
func (s *taskServer) RecoverStale(ctx context.Context, _ *pb.RecoverStaleRequest) (*pb.RecoverStaleResponse, error) {
|
||||
n, err := s.deps.Tasks.RecoverStale(ctx)
|
||||
if err != nil {
|
||||
return nil, s.logErr("RecoverStale", err)
|
||||
}
|
||||
return &pb.RecoverStaleResponse{Count: n}, nil
|
||||
}
|
||||
|
||||
// Archive / Retry
|
||||
|
||||
func (s *taskServer) ArchiveTask(ctx context.Context, r *pb.ArchiveTaskRequest) (*pb.ArchiveTaskResponse, error) {
|
||||
eventID, err := s.deps.Tasks.ArchiveTask(ctx, r.TaskId, r.FromStatus, eventValue(r.Audit))
|
||||
if err != nil {
|
||||
return nil, s.logErr("ArchiveTask", err)
|
||||
}
|
||||
return &pb.ArchiveTaskResponse{EventId: eventID}, nil
|
||||
}
|
||||
|
||||
func (s *taskServer) RetryTask(ctx context.Context, r *pb.RetryTaskRequest) (*pb.RetryTaskResponse, error) {
|
||||
if err := s.deps.Tasks.RetryTask(ctx, r.TaskId, r.FromStatus, r.Workflow, r.RetryMode, task.Resume{From: r.ResumeFrom, After: r.ResumeAfter}, r.CurrentDefinition); err != nil {
|
||||
return nil, s.logErr("RetryTask", err)
|
||||
}
|
||||
return &pb.RetryTaskResponse{}, nil
|
||||
}
|
||||
|
||||
func (s *taskServer) TaskByIssue(ctx context.Context, r *pb.TaskByIssueRequest) (*pb.TaskByIssueResponse, error) {
|
||||
t, err := s.deps.Tasks.TaskByIssue(ctx, r.Owner, r.Repo, int(r.Number))
|
||||
if err != nil {
|
||||
return nil, s.logErr("TaskByIssue", err)
|
||||
}
|
||||
return &pb.TaskByIssueResponse{Task: taskProto(t), Found: t != nil}, nil
|
||||
}
|
||||
|
||||
func (s *taskServer) OpenTaskByPR(ctx context.Context, r *pb.OpenTaskByPRRequest) (*pb.OpenTaskByPRResponse, error) {
|
||||
t, err := s.deps.Tasks.OpenTaskByPR(ctx, r.Owner, r.Repo, int(r.Number))
|
||||
if err != nil {
|
||||
return nil, s.logErr("OpenTaskByPR", err)
|
||||
}
|
||||
return &pb.OpenTaskByPRResponse{Task: taskProto(t), Found: t != nil}, nil
|
||||
}
|
||||
|
||||
func (s *taskServer) TaskByID(ctx context.Context, r *pb.TaskByIDRequest) (*pb.TaskByIDResponse, error) {
|
||||
t, err := s.deps.Tasks.TaskByID(ctx, r.TaskId)
|
||||
if err != nil {
|
||||
return nil, s.logErr("TaskByID", err)
|
||||
}
|
||||
return &pb.TaskByIDResponse{Task: taskProto(t), Found: t != nil}, nil
|
||||
}
|
||||
|
||||
func (s *taskServer) OpenPRs(ctx context.Context, _ *pb.OpenPRsRequest) (*pb.OpenPRsResponse, error) {
|
||||
tasks, err := s.deps.Tasks.OpenPRs(ctx)
|
||||
if err != nil {
|
||||
return nil, s.logErr("OpenPRs", err)
|
||||
}
|
||||
out := make([]*pb.Task, len(tasks))
|
||||
for i := range tasks {
|
||||
out[i] = taskProto(&tasks[i])
|
||||
}
|
||||
return &pb.OpenPRsResponse{Tasks: out}, nil
|
||||
}
|
||||
|
||||
func (s *taskServer) ClearTerminalTasks(ctx context.Context, _ *pb.ClearTerminalTasksRequest) (*pb.ClearTerminalTasksResponse, error) {
|
||||
n, err := s.deps.Tasks.ClearTerminalTasks(ctx)
|
||||
if err != nil {
|
||||
return nil, s.logErr("ClearTerminalTasks", err)
|
||||
}
|
||||
return &pb.ClearTerminalTasksResponse{Count: n}, nil
|
||||
}
|
||||
|
||||
func (s *taskServer) Tasks(ctx context.Context, r *pb.TasksRequest) (*pb.TasksResponse, error) {
|
||||
tasks, err := s.deps.Tasks.TasksPage(ctx, storecontract.TaskPage{
|
||||
Statuses: r.Statuses, Workflow: r.Workflow, Limit: int(r.Limit),
|
||||
After: storecontract.TaskCursor{UpdatedAt: timeValue(r.AfterUpdatedAt), ID: r.AfterId},
|
||||
})
|
||||
if err != nil {
|
||||
return nil, s.logErr("Tasks", err)
|
||||
}
|
||||
out := make([]*pb.Task, len(tasks))
|
||||
for i := range tasks {
|
||||
out[i] = taskProto(&tasks[i])
|
||||
}
|
||||
return &pb.TasksResponse{Tasks: out}, nil
|
||||
}
|
||||
|
||||
func (s *taskServer) EnqueueCallTask(ctx context.Context, r *pb.EnqueueCallTaskRequest) (*pb.EnqueueCallTaskResponse, error) {
|
||||
if s.deps.WorkflowCalls == nil {
|
||||
return nil, errWorkflowCallerUnavailable
|
||||
}
|
||||
if r.CallerTaskId <= 0 {
|
||||
return nil, status.Error(codes.InvalidArgument, "caller task id is required")
|
||||
}
|
||||
// A call is identified by its key: without one the store would have to
|
||||
// guess, and two calls of one caller would collide or duplicate.
|
||||
if r.CallKey == "" {
|
||||
return nil, status.Error(codes.InvalidArgument, "call key is required")
|
||||
}
|
||||
inputs, err := task.DecodeInputs(r.InputsJson)
|
||||
if err != nil {
|
||||
return nil, status.Error(codes.InvalidArgument, err.Error())
|
||||
}
|
||||
t, err := s.deps.WorkflowCalls.StartCall(ctx, r.CallerTaskId, r.CallKey, r.Workflow, inputs)
|
||||
if err != nil {
|
||||
return nil, s.logErr("EnqueueCallTask", err)
|
||||
}
|
||||
return &pb.EnqueueCallTaskResponse{Task: taskProto(t), Accepted: true}, nil
|
||||
}
|
||||
|
||||
func (s *taskServer) WorkflowCallStatus(ctx context.Context, r *pb.WorkflowCallStatusRequest) (*pb.WorkflowCallStatusResponse, error) {
|
||||
if s.deps.WorkflowCalls == nil {
|
||||
return nil, errWorkflowCallerUnavailable
|
||||
}
|
||||
status, detail, outputs, err := s.deps.WorkflowCalls.CallStatus(ctx, r.CallerTaskId, r.CallTaskId)
|
||||
if err != nil {
|
||||
return nil, s.logErr("WorkflowCallStatus", err)
|
||||
}
|
||||
return &pb.WorkflowCallStatusResponse{Status: status, Detail: detail, OutputsJson: outputsJSON(outputs)}, nil
|
||||
}
|
||||
|
||||
// Task log
|
||||
|
||||
func (c *Client) EnqueueIssue(ctx context.Context, owner, repo string, number int, title, body, labels, identity string) (bool, error) {
|
||||
r, err := c.task.EnqueueIssue(ctx, &pb.EnqueueIssueRequest{Owner: owner, Repo: repo, Number: int64(number), Title: title, Body: body, Labels: labels, Identity: identity})
|
||||
if err != nil {
|
||||
return false, unmapError(err)
|
||||
}
|
||||
return r.Inserted, nil
|
||||
}
|
||||
|
||||
func (c *Client) ActiveTasksByOrigin(ctx context.Context, origin string) ([]*task.Task, error) {
|
||||
r, err := c.task.ActiveTasksByOrigin(ctx, &pb.ActiveTasksByOriginRequest{Origin: origin})
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
return mapValues(r.Tasks, taskValue), nil
|
||||
}
|
||||
|
||||
func (c *Client) UnpostedOutcomes(ctx context.Context) ([]*task.Task, error) {
|
||||
r, err := c.task.UnpostedOutcomes(ctx, &pb.UnpostedOutcomesRequest{})
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
return mapValues(r.Tasks, taskValue), nil
|
||||
}
|
||||
|
||||
func (c *Client) MarkOutcomePosted(ctx context.Context, taskID int64, status string) error {
|
||||
_, err := c.task.MarkOutcomePosted(ctx, &pb.MarkOutcomePostedRequest{TaskId: taskID, Status: status})
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
func (c *Client) EnqueueChatTask(ctx context.Context, owner, repo, title, body, wf, identity, origin, draft string, inputs map[string]any) (*task.Task, error) {
|
||||
encoded, err := task.EncodeInputs(inputs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
r, err := c.task.EnqueueChatTask(ctx, &pb.EnqueueChatTaskRequest{Owner: owner, Repo: repo, Title: title, Body: body, Workflow: wf, Identity: identity, InputsJson: encoded, Origin: origin, DraftYaml: draft})
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
return taskValue(r.Task), nil
|
||||
}
|
||||
|
||||
func (c *Client) ClaimNext(ctx context.Context) (*task.Task, error) {
|
||||
r, err := c.task.ClaimNext(ctx, &pb.ClaimNextRequest{})
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
if !r.Found {
|
||||
return nil, nil
|
||||
}
|
||||
return taskValue(r.Task), nil
|
||||
}
|
||||
|
||||
func (c *Client) ClaimByIssue(ctx context.Context, owner, repo string, number int) (*task.Task, error) {
|
||||
r, err := c.task.ClaimByIssue(ctx, &pb.ClaimByIssueRequest{Owner: owner, Repo: repo, Number: int64(number)})
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
if !r.Found {
|
||||
return nil, nil
|
||||
}
|
||||
return taskValue(r.Task), nil
|
||||
}
|
||||
|
||||
func (c *Client) Transition(ctx context.Context, taskID int64, from, to, detail string) error {
|
||||
_, err := c.task.Transition(ctx, &pb.TransitionRequest{TaskId: taskID, From: from, To: to, Detail: detail})
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
// TaskModels returns what the task's org resolves models against.
|
||||
func (c *Client) TaskModels(ctx context.Context, taskID int64) (config.OrgModels, error) {
|
||||
r, err := c.task.TaskModels(ctx, &pb.TaskModelsRequest{TaskId: taskID})
|
||||
if err != nil {
|
||||
return config.OrgModels{}, unmapError(err)
|
||||
}
|
||||
providers := make(map[string]config.Provider, len(r.Providers))
|
||||
for name, p := range r.Providers {
|
||||
providers[name] = modelProviderFromPB(p)
|
||||
}
|
||||
return config.OrgModels{Org: r.OrgId, Aliases: r.Aliases, Providers: providers}, nil
|
||||
}
|
||||
|
||||
// TaskInstructions returns the instance and organisation instructions for the
|
||||
// task's org.
|
||||
func (c *Client) TaskInstructions(ctx context.Context, taskID int64) (instance, organisation string, err error) {
|
||||
r, err := c.task.TaskInstructions(ctx, &pb.TaskInstructionsRequest{TaskId: taskID})
|
||||
if err != nil {
|
||||
return "", "", unmapError(err)
|
||||
}
|
||||
return r.GetInstance(), r.GetOrganisation(), nil
|
||||
}
|
||||
|
||||
func (c *Client) Update(ctx context.Context, t *task.Task) error {
|
||||
_, err := c.task.Update(ctx, &pb.UpdateRequest{Task: taskProto(t)})
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
func (c *Client) Requeue(ctx context.Context, taskID int64, fromStatus, wf string) error {
|
||||
_, err := c.task.Requeue(ctx, &pb.RequeueRequest{TaskId: taskID, FromStatus: fromStatus, Workflow: wf})
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
func (c *Client) ParkTask(ctx context.Context, taskID int64, from, detail, class, blocker string) error {
|
||||
_, err := c.task.ParkTask(ctx, &pb.ParkTaskRequest{TaskId: taskID, From: from, Detail: detail, Class: class, Blocker: blocker})
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
func (c *Client) RecoverStale(ctx context.Context) (int64, error) {
|
||||
r, err := c.task.RecoverStale(ctx, &pb.RecoverStaleRequest{})
|
||||
if err != nil {
|
||||
return 0, unmapError(err)
|
||||
}
|
||||
return r.Count, nil
|
||||
}
|
||||
|
||||
// Archive / Retry
|
||||
|
||||
func (c *Client) ArchiveTask(ctx context.Context, taskID int64, fromStatus string, audit events.Event) (int64, error) {
|
||||
r, err := c.task.ArchiveTask(ctx, &pb.ArchiveTaskRequest{TaskId: taskID, FromStatus: fromStatus, Audit: eventProto(audit)})
|
||||
if err != nil {
|
||||
return 0, unmapError(err)
|
||||
}
|
||||
return r.EventId, nil
|
||||
}
|
||||
|
||||
func (c *Client) RetryTask(ctx context.Context, taskID int64, fromStatus, wf, retryMode string, resume task.Resume, currentDefinition bool) error {
|
||||
_, err := c.task.RetryTask(ctx, &pb.RetryTaskRequest{
|
||||
TaskId: taskID, FromStatus: fromStatus, Workflow: wf, RetryMode: retryMode,
|
||||
ResumeFrom: resume.From, ResumeAfter: resume.After, CurrentDefinition: currentDefinition,
|
||||
})
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
func (c *Client) TaskByIssue(ctx context.Context, owner, repo string, number int) (*task.Task, error) {
|
||||
r, err := c.task.TaskByIssue(ctx, &pb.TaskByIssueRequest{Owner: owner, Repo: repo, Number: int64(number)})
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
if !r.Found {
|
||||
return nil, nil
|
||||
}
|
||||
return taskValue(r.Task), nil
|
||||
}
|
||||
|
||||
func (c *Client) OpenTaskByPR(ctx context.Context, owner, repo string, number int) (*task.Task, error) {
|
||||
r, err := c.task.OpenTaskByPR(ctx, &pb.OpenTaskByPRRequest{Owner: owner, Repo: repo, Number: int64(number)})
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
if !r.Found {
|
||||
return nil, nil
|
||||
}
|
||||
return taskValue(r.Task), nil
|
||||
}
|
||||
|
||||
func (c *Client) TaskByID(ctx context.Context, taskID int64) (*task.Task, error) {
|
||||
r, err := c.task.TaskByID(ctx, &pb.TaskByIDRequest{TaskId: taskID})
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
if !r.Found {
|
||||
return nil, nil
|
||||
}
|
||||
return taskValue(r.Task), nil
|
||||
}
|
||||
|
||||
func (c *Client) OpenPRs(ctx context.Context) ([]task.Task, error) {
|
||||
r, err := c.task.OpenPRs(ctx, &pb.OpenPRsRequest{})
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
return mapValues(r.Tasks, func(t *pb.Task) task.Task { return *taskValue(t) }), nil
|
||||
}
|
||||
|
||||
func (c *Client) ClearTerminalTasks(ctx context.Context) (int64, error) {
|
||||
r, err := c.task.ClearTerminalTasks(ctx, &pb.ClearTerminalTasksRequest{})
|
||||
if err != nil {
|
||||
return 0, unmapError(err)
|
||||
}
|
||||
return r.Count, nil
|
||||
}
|
||||
|
||||
// TasksPage returns one page of tasks, most recently updated first.
|
||||
func (c *Client) TasksPage(ctx context.Context, page storecontract.TaskPage) ([]task.Task, error) {
|
||||
r, err := c.task.Tasks(ctx, &pb.TasksRequest{
|
||||
Limit: int64(page.Limit), Statuses: page.Statuses, Workflow: page.Workflow,
|
||||
AfterUpdatedAt: timestamp(page.After.UpdatedAt), AfterId: page.After.ID,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
return mapValues(r.Tasks, func(t *pb.Task) task.Task { return *taskValue(t) }), nil
|
||||
}
|
||||
|
||||
func (c *Client) StartCall(ctx context.Context, callerTaskID int64, callKey, wf string, inputs map[string]any) (*task.Task, error) {
|
||||
encoded, err := task.EncodeInputs(inputs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
r, err := c.task.EnqueueCallTask(ctx, &pb.EnqueueCallTaskRequest{CallerTaskId: callerTaskID, CallKey: callKey, Workflow: wf, InputsJson: encoded})
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
return taskValue(r.Task), nil
|
||||
}
|
||||
|
||||
func (c *Client) CallStatus(ctx context.Context, callerTaskID, callTaskID int64) (string, string, map[string]any, error) {
|
||||
r, err := c.task.WorkflowCallStatus(ctx, &pb.WorkflowCallStatusRequest{CallerTaskId: callerTaskID, CallTaskId: callTaskID})
|
||||
if err != nil {
|
||||
return "", "", nil, unmapError(err)
|
||||
}
|
||||
return r.Status, r.Detail, outputsValue(r.OutputsJson), nil
|
||||
}
|
||||
|
||||
// Task log
|
||||
Reference in new issue
Block a user