142 lines
5.9 KiB
Go
142 lines
5.9 KiB
Go
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.
|