Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
felladrin committed Dec 13, 2022
1 parent 399d72b commit 349565e
Showing 1 changed file with 31 additions and 27 deletions.
58 changes: 31 additions & 27 deletions components/ide/jetbrains/launcher/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,22 +161,7 @@ func main() {
}

if launchCtx.warmup {
ctx, cancel := context.WithCancel(context.Background())

out:
for {
log.Debug("launcher: wait for tasks to finish before running warmup")
err := waitForTasksToFinish(ctx, cancel)
if err != nil {
log.WithError(err).Warn("launcher: failed to observe tasks completion")
}
select {
case <-ctx.Done():
break out
case <-time.After(1 * time.Second):
}
}

starWarmup()
launch(launchCtx)
return
}
Expand All @@ -187,6 +172,31 @@ func main() {
serve(launchCtx)
}

func starWarmup() {
ctx, cancel := context.WithCancel(context.Background())
var conn *grpc.ClientConn
var err error

for {
conn, err = dial(ctx)
if err == nil {
log.Debug("launcher: wait for tasks to finish before running warmup")
finished, err := waitForTasksToFinish(ctx, conn)
if err != nil {
log.WithError(err).Warn("launcher: failed to observe tasks completion")
}
if finished {
cancel()
}
}
select {
case <-ctx.Done():
return
case <-time.After(1 * time.Second):
}
}
}

func serve(launchCtx *LaunchContext) {
debugAgentPrefix := "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:"
http.HandleFunc("/debug", func(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -870,20 +880,16 @@ func resolveProjectContextDir(launchCtx *LaunchContext) string {
return launchCtx.projectDir
}

func waitForTasksToFinish(ctx context.Context, cancel context.CancelFunc) error {
conn, err := dial(ctx)
if err != nil {
return err
}
func waitForTasksToFinish(ctx context.Context, conn *grpc.ClientConn) (bool, 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)
return false, xerrors.Errorf("failed get tasks status client: %w", err)
}
for {
resp, err := tasksResponse.Recv()
if err != nil {
return err
return false, err
}

var runningTasksCounter int
Expand All @@ -893,12 +899,10 @@ func waitForTasksToFinish(ctx context.Context, cancel context.CancelFunc) error
}
}
if runningTasksCounter == 0 {
cancel()
return true, nil
}

return nil
}

return false, nil
}

func dial(ctx context.Context) (*grpc.ClientConn, error) {
Expand Down

0 comments on commit 349565e

Please sign in to comment.