497 lines
18 KiB
Go
497 lines
18 KiB
Go
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
|