gitpod/components/supervisor/cmd/prepare-ide-prebuild.go
Anton Kosyakov fdb7e5ea16
validate jb warmup (#18050)
* [jb] move warmup tasks to integration

* move composition of ide prebuild tasks in supervisor
2023-06-30 14:51:48 +08:00

87 lines
2.1 KiB
Go

// Copyright (c) 2023 Gitpod GmbH. All rights reserved.
// Licensed under the GNU Affero General Public License (AGPL).
// See License.AGPL.txt in the project root for license information.
package cmd
import (
"context"
"strings"
"time"
"github.com/gitpod-io/gitpod/common-go/log"
"github.com/gitpod-io/gitpod/common-go/util"
supervisor "github.com/gitpod-io/gitpod/supervisor/api"
"github.com/spf13/cobra"
"golang.org/x/xerrors"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
var prepareIDEPrebuildCmd = &cobra.Command{
Use: "prepare-ide-prebuild",
Short: "awaits when it is time to run IDE prebuild",
Run: func(cmd *cobra.Command, args []string) {
ctx := cmd.Context()
for {
conn, err := dial(ctx)
if err == nil {
err = checkTasks(ctx, conn)
}
if err == nil || ctx.Err() != nil {
return
}
log.WithError(err).Error("supervisor: failed to check tasks status")
select {
case <-ctx.Done():
return
case <-time.After(1 * time.Second):
}
}
},
}
func checkTasks(ctx context.Context, conn *grpc.ClientConn) error {
client := supervisor.NewStatusServiceClient(conn)
tasksResponse, err := client.TasksStatus(ctx, &supervisor.TasksStatusRequest{Observe: true})
if err != nil {
return xerrors.Errorf("failed get tasks status client: %w", err)
}
for {
var runningTasksCounter int
resp, err := tasksResponse.Recv()
if err != nil {
return err
}
for _, task := range resp.Tasks {
idePrebuildTask := strings.Contains(task.Presentation.Name, "ide-prebuild-")
if task.State != supervisor.TaskState_closed && !idePrebuildTask {
runningTasksCounter++
}
}
if runningTasksCounter == 0 {
break
}
}
return nil
}
func dial(ctx context.Context) (*grpc.ClientConn, error) {
supervisorConn, err := grpc.DialContext(ctx, util.GetSupervisorAddress(), grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
err = xerrors.Errorf("failed connecting to supervisor: %w", err)
}
return supervisorConn, err
}
func init() {
rootCmd.AddCommand(prepareIDEPrebuildCmd)
}