Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/supervisor/cmd/prepare-ide-prebuild.go
2498 views
1
// Copyright (c) 2023 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 cmd
6
7
import (
8
"context"
9
"strings"
10
"time"
11
12
"github.com/gitpod-io/gitpod/common-go/log"
13
"github.com/gitpod-io/gitpod/common-go/util"
14
supervisor "github.com/gitpod-io/gitpod/supervisor/api"
15
"github.com/spf13/cobra"
16
"golang.org/x/xerrors"
17
"google.golang.org/grpc"
18
"google.golang.org/grpc/credentials/insecure"
19
)
20
21
var prepareIDEPrebuildCmd = &cobra.Command{
22
Use: "prepare-ide-prebuild",
23
Short: "awaits when it is time to run IDE prebuild",
24
Run: func(cmd *cobra.Command, args []string) {
25
ctx := cmd.Context()
26
for {
27
conn, err := dial(ctx)
28
if err == nil {
29
err = checkTasks(ctx, conn)
30
}
31
32
if err == nil || ctx.Err() != nil {
33
return
34
}
35
36
log.WithError(err).Error("supervisor: failed to check tasks status")
37
38
select {
39
case <-ctx.Done():
40
return
41
case <-time.After(1 * time.Second):
42
}
43
}
44
},
45
}
46
47
func checkTasks(ctx context.Context, conn *grpc.ClientConn) error {
48
client := supervisor.NewStatusServiceClient(conn)
49
tasksResponse, err := client.TasksStatus(ctx, &supervisor.TasksStatusRequest{Observe: true})
50
if err != nil {
51
return xerrors.Errorf("failed get tasks status client: %w", err)
52
}
53
54
for {
55
var runningTasksCounter int
56
57
resp, err := tasksResponse.Recv()
58
if err != nil {
59
return err
60
}
61
62
for _, task := range resp.Tasks {
63
idePrebuildTask := strings.Contains(task.Presentation.Name, "ide-prebuild-")
64
if task.State != supervisor.TaskState_closed && !idePrebuildTask {
65
runningTasksCounter++
66
}
67
}
68
if runningTasksCounter == 0 {
69
break
70
}
71
}
72
73
return nil
74
}
75
76
func dial(ctx context.Context) (*grpc.ClientConn, error) {
77
supervisorConn, err := grpc.DialContext(ctx, util.GetSupervisorAddress(), grpc.WithTransportCredentials(insecure.NewCredentials()))
78
if err != nil {
79
err = xerrors.Errorf("failed connecting to supervisor: %w", err)
80
}
81
return supervisorConn, err
82
}
83
84
func init() {
85
rootCmd.AddCommand(prepareIDEPrebuildCmd)
86
}
87
88