aboutsummaryrefslogtreecommitdiff
path: root/backend/internal/server2/operations.go
blob: c632cd10b246367a7cc391bcaab9cd75820e9d5f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package server2

import (
	"context"
	"errors"
	"fmt"
	"log/slog"
	"strings"

	spb "ibd-trader/api/gen/idb/stock/v1"
	"ibd-trader/internal/leader/manager/ibd/scrape"
	"ibd-trader/internal/redis/taskqueue"
	"ibd-trader/internal/server2/idb/stock/v1"

	"cloud.google.com/go/longrunning/autogen/longrunningpb"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
	"google.golang.org/protobuf/types/known/anypb"
	"google.golang.org/protobuf/types/known/timestamppb"
)

type operationServer struct {
	longrunningpb.UnimplementedOperationsServer

	scrape taskqueue.TaskQueue[scrape.TaskInfo]
}

func newOperationServer(scrapeQueue taskqueue.TaskQueue[scrape.TaskInfo]) *operationServer {
	return &operationServer{scrape: scrapeQueue}
}

func (o *operationServer) ListOperations(
	ctx context.Context,
	req *longrunningpb.ListOperationsRequest,
) (*longrunningpb.ListOperationsResponse, error) {
	var end taskqueue.TaskID
	if req.PageToken != "" {
		var err error
		end, err = taskqueue.ParseTaskID(req.PageToken)
		if err != nil {
			return nil, status.New(codes.InvalidArgument, err.Error()).Err()
		}
	} else {
		end = taskqueue.TaskID{}
	}

	switch req.Name {
	case stock.ScrapeOperationPrefix:
		tasks, err := o.scrape.List(ctx, taskqueue.TaskID{}, end, int64(req.PageSize))
		if err != nil {
			return nil, status.New(codes.Internal, "unable to list IDs").Err()
		}

		ops := make([]*longrunningpb.Operation, len(tasks))
		for i, task := range tasks {
			ops[i] = &longrunningpb.Operation{
				Name:     fmt.Sprintf("%s/%s", stock.ScrapeOperationPrefix, task.ID.String()),
				Metadata: new(anypb.Any),
				Done:     task.Done,
				Result:   nil,
			}
			err = ops[i].Metadata.MarshalFrom(&spb.StockScrapeOperationMetadata{
				Symbol:    task.Data.Symbol,
				StartTime: timestamppb.New(task.ID.Timestamp()),
			})
			if err != nil {
				return nil, status.New(codes.Internal, "unable to marshal metadata").Err()
			}

			if task.Done && task.Error != "" {
				s := status.New(codes.Unknown, task.Error)
				ops[i].Result = &longrunningpb.Operation_Error{Error: s.Proto()}
			}
		}

		var nextPageToken string
		if len(tasks) == int(req.PageSize) {
			nextPageToken = tasks[len(tasks)-1].ID.String()
		} else {
			nextPageToken = ""
		}

		return &longrunningpb.ListOperationsResponse{
			Operations:    ops,
			NextPageToken: nextPageToken,
		}, nil
	default:
		return nil, status.New(codes.NotFound, "unknown operation type").Err()
	}
}

func (o *operationServer) GetOperation(ctx context.Context, req *longrunningpb.GetOperationRequest) (*longrunningpb.Operation, error) {
	prefix, id, ok := strings.Cut(req.Name, "/")
	if !ok || prefix == "" || id == "" {
		return nil, status.New(codes.InvalidArgument, "invalid operation name").Err()
	}

	taskID, err := taskqueue.ParseTaskID(id)
	if err != nil {
		return nil, status.New(codes.InvalidArgument, err.Error()).Err()
	}

	switch prefix {
	case stock.ScrapeOperationPrefix:
		task, err := o.scrape.Data(ctx, taskID)
		if errors.Is(err, taskqueue.ErrTaskNotFound) {
			return nil, status.New(codes.NotFound, "operation not found").Err()
		}
		if err != nil {
			slog.ErrorContext(ctx, "unable to get operation", "error", err)
			return nil, status.New(codes.Internal, "unable to get operation").Err()
		}
		op := &longrunningpb.Operation{
			Name:     req.Name,
			Metadata: new(anypb.Any),
			Done:     task.Done,
			Result:   nil,
		}
		err = op.Metadata.MarshalFrom(&spb.StockScrapeOperationMetadata{
			Symbol:    task.Data.Symbol,
			StartTime: timestamppb.New(task.ID.Timestamp()),
		})
		if err != nil {
			return nil, status.New(codes.Internal, "unable to marshal metadata").Err()
		}
		return op, nil
	default:
		return nil, status.New(codes.NotFound, "unknown operation type").Err()
	}
}
ojs/prefetch@0.1.0&id=1f890b3363d8ce232571612056b485c13983e5ef&follow=1'>Yield out potentional slot instructions when rendering dynamic tags (#4981)Gravatar Matthew Phillips 11-32/+132 * Yield out potentional slot instructions when rendering dynamic tags * Adding a changeset * yield instead of return * Handle the fact that renderComponent returns an iterable * Only yield out html once 2022-10-05Upgrade Astro compiler to 0.26.0 (#4990)Gravatar Matthew Phillips 3-5/+10 * Upgrade Astro compiler * Add changeset 2022-10-05Fix failing e2e test (#4983)Gravatar Bjorn Lu 2-1/+4 2022-10-04Updating sponsors list in README (#4979)Gravatar Tony Sullivan 12-48/+176 * updating all org and project sponsors * fix: copy/paste errors * sponsors should be in one row * using a logo cloud image for now * updating sponsors message to match other repos 2022-10-04[ci] formatGravatar matthewp 4-7/+11 2022-10-04Refactor hydration path handling (#4918)Gravatar Bjorn Lu 14-44/+137 * Refactor hydration path handling * Remove old code * Fix jsx strip * Postprocess fix * Handle jsx to tsx stuff * Skip bigint tests * Fix deno * Try fix windows * Fix windows * Add more comments 2022-10-04fix: import.meta.env.BASE_URL will be '/' in client loaded component on dev ↵Gravatar 董雨航 9-0/+82 mode (#4886) 2022-10-04fix object styles not escaped (#4887)Gravatar Calvin Liang 8-2/+71 * fix object styles not escaped * fix `shouldEscape` not passed down * add tests * fix package name * fix pnpm-lock * add changeset 2022-10-04closes #4633 (#4977)Gravatar Tony Sullivan 3-3/+11 * closes #4633 * chore: add changeset 2022-10-04[ci] update lockfile (#4946)Gravatar Fred K. Bot 1-150/+159 Co-authored-by: FredKSchott <FredKSchott@users.noreply.github.com> 2022-10-04[ci] formatGravatar matthewp 1-4/+4 2022-10-04Update config base type description (#4954)Gravatar Zihan Chen 1-1/+10 2022-10-04Support Astro.slots.render for mdx (#4973)Gravatar Bjorn Lu 8-2/+69 * Support Astro.slots.render for mdx * Remove extra imports 2022-10-03[ci] release (#4968)astro@1.4.4Gravatar Fred K. Bot 28-57/+58 Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> 2022-10-03Fix regression in rendering strings (#4967)Gravatar Matthew Phillips 2-2/+9 2022-10-03[ci] formatGravatar matthewp 1-1/+2 2022-10-03Adds a better test for benchmarking (#4966)Gravatar Matthew Phillips 2-7/+77 * Rendering: speed up rendering of HTML chunks * Add a changeset * Remove the fix * remove the changeset 2022-10-03Benchmark action: provide the PR number (#4964)Gravatar Matthew Phillips 1-0/+1 * Testing benchmarking * Provide the PR number 2022-10-03Build packages in the benchmark action (#4962)Gravatar Matthew Phillips 1-0/+11 * Just a test * Run the build * Add turbo stuff 2022-10-03Run benchmarks on comment (#4960)Gravatar Matthew Phillips 2-1/+85 * Run benchmarks on !bench command * Re-enable main 2022-10-03[ci] release (#4957)astro@1.4.3@astrojs/mdx@0.11.4Gravatar Fred K. Bot 32-70/+69 Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> 2022-10-03[ci] formatGravatar matthewp 5-6/+6 2022-10-03Improve rendering perf (#4956)Gravatar Matthew Phillips 8-31/+73 * Improve rendering perf * Adding a changeset * Disable eslint warning 2022-10-03Suppress eslint warnings (#4953)Gravatar Bjorn Lu 3-3/+14 2022-10-03[ci] formatGravatar bluwy 1-1/+1 2022-10-03Refactor ViteConfigWithSSR type (#4952)Gravatar Bjorn Lu 7-20/+20 2022-10-01[ci] release (#4945)@astrojs/image@0.9.11.3.0Gravatar Fred K. Bot 3-6/+7 Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> 2022-09-30Astro image cache dependency fix scottaw66 (#4944)Gravatar Scott Willsey 3-3/+7 * Moves http-cache-semantics from dev dependency to dependency * Pull request for astro image http-cache-semantics dependency 2022-09-30[ci] release (#4943)astro@1.4.2Gravatar Fred K. Bot 29-60/+58 Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> 2022-09-30[ci] formatGravatar natemoo-re 1-1/+1 2022-09-30Convert HTMLString to regular string, preventing hydration script fro… (#4932)Gravatar Matthew Phillips 2-1/+6 * Convert HTMLString to regular string, preventing hydration script from being missing * Add a changeset 2022-10-01Fix missing language tag on README code block (#4940)Gravatar Chris Swithinbank 1-1/+1 2022-09-30[ci] formatGravatar natemoo-re 2-2/+2 2022-09-30P5: fix MDX memory leak (#4939)Gravatar Nate Moore 10-1/+84 * fix(astro): tag jsx vnodes with renderer so errors are properly handled * chore: fix missing package in test Co-authored-by: Nate Moore <nate@astro.build> 2022-09-30chore: delete .stackblitzrc files from examples (#4922)Gravatar Nate Moore 22-132/+0 Co-authored-by: Nate Moore <nate@astro.build>