Skip to content

Commit cd144de

Browse files
jeanp413roboquat
authored andcommitted
Add IDEClientService
1 parent fb29551 commit cd144de

File tree

15 files changed

+1162
-828
lines changed

15 files changed

+1162
-828
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2+
// Licensed under the GNU Affero General Public License (AGPL).
3+
// See License-AGPL.txt in the project root for license information.
4+
5+
package apiv1
6+
7+
import (
8+
"context"
9+
"fmt"
10+
11+
connect "github.com/bufbuild/connect-go"
12+
v1 "github.com/gitpod-io/gitpod/components/public-api/go/experimental/v1"
13+
"github.com/gitpod-io/gitpod/components/public-api/go/experimental/v1/v1connect"
14+
protocol "github.com/gitpod-io/gitpod/gitpod-protocol"
15+
"github.com/gitpod-io/gitpod/public-api-server/pkg/proxy"
16+
"github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus/ctxlogrus"
17+
)
18+
19+
func NewIDEClientService(pool proxy.ServerConnectionPool) *IDEClientService {
20+
return &IDEClientService{
21+
connectionPool: pool,
22+
}
23+
}
24+
25+
var _ v1connect.IDEClientServiceHandler = (*IDEClientService)(nil)
26+
27+
type IDEClientService struct {
28+
connectionPool proxy.ServerConnectionPool
29+
30+
v1connect.UnimplementedIDEClientServiceHandler
31+
}
32+
33+
func (this *IDEClientService) SendHeartbeat(ctx context.Context, req *connect.Request[v1.SendHeartbeatRequest]) (*connect.Response[v1.SendHeartbeatResponse], error) {
34+
logger := ctxlogrus.Extract(ctx)
35+
36+
conn, err := getConnection(ctx, this.connectionPool)
37+
if err != nil {
38+
return nil, err
39+
}
40+
41+
workspace, err := conn.GetWorkspace(ctx, req.Msg.GetWorkspaceId())
42+
if err != nil {
43+
logger.WithError(err).Error("Failed to get workspace.")
44+
return nil, proxy.ConvertError(err)
45+
}
46+
47+
if workspace.LatestInstance == nil {
48+
logger.WithError(err).Error("Failed to get latest instance.")
49+
return nil, connect.NewError(connect.CodeFailedPrecondition, fmt.Errorf("instance not found"))
50+
}
51+
52+
err = conn.SendHeartBeat(ctx, &protocol.SendHeartBeatOptions{
53+
InstanceID: workspace.LatestInstance.ID,
54+
WasClosed: false,
55+
})
56+
if err != nil {
57+
return nil, proxy.ConvertError(err)
58+
}
59+
60+
return connect.NewResponse(&v1.SendHeartbeatResponse{}), nil
61+
}
62+
63+
func (this *IDEClientService) SendDidClose(ctx context.Context, req *connect.Request[v1.SendDidCloseRequest]) (*connect.Response[v1.SendDidCloseResponse], error) {
64+
logger := ctxlogrus.Extract(ctx)
65+
66+
conn, err := getConnection(ctx, this.connectionPool)
67+
if err != nil {
68+
return nil, err
69+
}
70+
71+
workspace, err := conn.GetWorkspace(ctx, req.Msg.GetWorkspaceId())
72+
if err != nil {
73+
logger.WithError(err).Error("Failed to get workspace.")
74+
return nil, proxy.ConvertError(err)
75+
}
76+
77+
if workspace.LatestInstance == nil {
78+
logger.WithError(err).Error("Failed to get latest instance.")
79+
return nil, connect.NewError(connect.CodeFailedPrecondition, fmt.Errorf("instance not found"))
80+
}
81+
82+
err = conn.SendHeartBeat(ctx, &protocol.SendHeartBeatOptions{
83+
InstanceID: workspace.LatestInstance.ID,
84+
WasClosed: true,
85+
})
86+
if err != nil {
87+
return nil, proxy.ConvertError(err)
88+
}
89+
90+
return connect.NewResponse(&v1.SendDidCloseResponse{}), nil
91+
}

components/public-api-server/pkg/apiv1/workspace.go

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -122,40 +122,6 @@ func (s *WorkspaceService) ListWorkspaces(ctx context.Context, req *connect.Requ
122122
), nil
123123
}
124124

125-
func (s *WorkspaceService) SendHeartbeat(ctx context.Context, req *connect.Request[v1.SendHeartbeatRequest]) (*connect.Response[v1.SendHeartbeatResponse], error) {
126-
conn, err := getConnection(ctx, s.connectionPool)
127-
if err != nil {
128-
return nil, err
129-
}
130-
131-
err = conn.SendHeartBeat(ctx, &protocol.SendHeartBeatOptions{
132-
InstanceID: req.Msg.GetWorkspaceId(),
133-
WasClosed: false,
134-
})
135-
if err != nil {
136-
return nil, proxy.ConvertError(err)
137-
}
138-
139-
return connect.NewResponse(&v1.SendHeartbeatResponse{}), nil
140-
}
141-
142-
func (s *WorkspaceService) SendCloseSignal(ctx context.Context, req *connect.Request[v1.SendCloseSignalRequest]) (*connect.Response[v1.SendCloseSignalResponse], error) {
143-
conn, err := getConnection(ctx, s.connectionPool)
144-
if err != nil {
145-
return nil, err
146-
}
147-
148-
err = conn.SendHeartBeat(ctx, &protocol.SendHeartBeatOptions{
149-
InstanceID: req.Msg.GetWorkspaceId(),
150-
WasClosed: true,
151-
})
152-
if err != nil {
153-
return nil, proxy.ConvertError(err)
154-
}
155-
156-
return connect.NewResponse(&v1.SendCloseSignalResponse{}), nil
157-
}
158-
159125
func getLimitFromPagination(pagination *v1.Pagination) (int, error) {
160126
const (
161127
defaultLimit = 20

components/public-api-server/pkg/server/server.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ func register(srv *baseserver.Server, connPool proxy.ServerConnectionPool, expCl
126126
userRoute, userServiceHandler := v1connect.NewUserServiceHandler(apiv1.NewUserService(connPool), handlerOptions...)
127127
srv.HTTPMux().Handle(userRoute, userServiceHandler)
128128

129+
ideClientRoute, ideClientServiceHandler := v1connect.NewIDEClientServiceHandler(apiv1.NewIDEClientService(connPool), handlerOptions...)
130+
srv.HTTPMux().Handle(ideClientRoute, ideClientServiceHandler)
131+
129132
return nil
130133
}
131134

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
syntax = "proto3";
2+
3+
package gitpod.experimental.v1;
4+
5+
option go_package = "github.com/gitpod-io/gitpod/components/public-api/go/experimental/v1";
6+
7+
service IDEClientService {
8+
// SendHeartbeat sends a clientheartbeat signal for a running workspace.
9+
rpc SendHeartbeat(SendHeartbeatRequest) returns (SendHeartbeatResponse) {}
10+
11+
// SendDidClose sends a client close signal for a running workspace.
12+
rpc SendDidClose(SendDidCloseRequest) returns (SendDidCloseResponse) {}
13+
}
14+
15+
message SendHeartbeatRequest {
16+
string workspace_id = 1;
17+
}
18+
message SendHeartbeatResponse {}
19+
20+
message SendDidCloseRequest {
21+
string workspace_id = 1;
22+
}
23+
message SendDidCloseResponse {}

components/public-api/gitpod/experimental/v1/workspaces.proto

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,6 @@ service WorkspacesService {
2727
// NOT_FOUND: the workspace_id is unkown
2828
// FAILED_PRECONDITION: if there's no running instance
2929
rpc StopWorkspace(StopWorkspaceRequest) returns (stream StopWorkspaceResponse) {}
30-
31-
// SendHeartbeat sends a heartbeat signal to a running workspace.
32-
rpc SendHeartbeat(SendHeartbeatRequest) returns (SendHeartbeatResponse) {}
33-
34-
// SendCloseSignal sends a close signal to a running workspace.
35-
rpc SendCloseSignal(SendCloseSignalRequest) returns (SendCloseSignalResponse) {}
3630
}
3731

3832
message ListWorkspacesRequest {
@@ -91,16 +85,6 @@ message StopWorkspaceRequest {
9185
}
9286
message StopWorkspaceResponse {}
9387

94-
message SendHeartbeatRequest {
95-
string workspace_id = 1;
96-
}
97-
message SendHeartbeatResponse {}
98-
99-
message SendCloseSignalRequest {
100-
string workspace_id = 1;
101-
}
102-
message SendCloseSignalResponse {}
103-
10488
////////////////////////////////
10589
// Shared messages come here
10690
////////////////////////////////

0 commit comments

Comments
 (0)