Skip to content

Commit

Permalink
fix: purge
Browse files Browse the repository at this point in the history
Signed-off-by: Luka Brecic <[email protected]>
  • Loading branch information
lbrecic committed Dec 23, 2024
1 parent b67a139 commit b6bdf4c
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 0 deletions.
18 changes: 18 additions & 0 deletions pkg/cmd/purge.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,24 @@ var purgeCmd = &cobra.Command{
return err
}

err = server.Start()
if err != nil {
return err
}

log.Info("Starting job runner...")
jobRunner, err := bootstrap.GetJobRunner(serverConfig, serverConfigDir, internal.Version, telemetryService)
if err != nil {
return err
}

// TODO: context?
err = jobRunner.StartRunner(context.Background())
if err != nil {
return err
}
log.Info("Job runner started")

errs := server.Purge(ctx, forceFlag)
if len(errs) > 0 {
errMessage := ""
Expand Down
58 changes: 58 additions & 0 deletions pkg/server/purge.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,44 @@ func (s *Server) Purge(ctx context.Context, force bool) []error {
}
}

fmt.Println("Deleting all workspaces...")

workspaces, err := s.WorkspaceService.ListWorkspaces(ctx, services.WorkspaceRetrievalParams{})
if err != nil {
s.trackPurgeError(ctx, force, err)
if !force {
return []error{err}
}
}

if err == nil {
for _, workspace := range workspaces {
err := s.WorkspaceService.RemoveWorkspace(ctx, workspace.Id)
if err != nil {
s.trackPurgeError(ctx, force, err)
if !force {
return []error{err}
} else {
fmt.Printf("Failed to delete %s: %v\n", workspace.Name, err)
}
} else {
fmt.Printf("Workspace %s deleted\n", workspace.Name)
}
}
} else {
fmt.Printf("Failed to list workspaces: %v\n", err)
}

err = s.WorkspaceService.AwaitEmptyList(ctx, time.Minute)
if err != nil {
s.trackPurgeError(ctx, force, err)
if !force {
return []error{err}
} else {
fmt.Printf("Failed to await empty workspace list: %v\n", err)
}
}

fmt.Println("Deleting all targets...")

targets, err := s.TargetService.ListTargets(ctx, nil, services.TargetRetrievalParams{})
Expand Down Expand Up @@ -57,6 +95,26 @@ func (s *Server) Purge(ctx context.Context, force bool) []error {
fmt.Printf("Failed to list targets: %v\n", err)
}

err = s.TargetService.AwaitEmptyList(ctx, time.Minute)
if err != nil {
s.trackPurgeError(ctx, force, err)
if !force {
return []error{err}
} else {
fmt.Printf("Failed to await empty target list: %v\n", err)
}
}

err = s.TargetService.AwaitEmptyList(ctx, time.Minute)
if err != nil {
s.trackPurgeError(ctx, force, err)
if !force {
return []error{err}
} else {
fmt.Printf("Failed to await empty target list: %v\n", err)
}
}

// FIXME: todo
// fmt.Println("Purging providers...")
// err = s.ProviderManager.Purge()
Expand Down
25 changes: 25 additions & 0 deletions pkg/server/targets/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ package targets

import (
"context"
"errors"
"io"
"time"

"github.com/daytonaio/daytona/pkg/logs"
"github.com/daytonaio/daytona/pkg/models"
Expand Down Expand Up @@ -88,3 +90,26 @@ func (s *TargetService) UpdateTargetProviderMetadata(ctx context.Context, target
tg.ProviderMetadata = &metadata
return s.targetStore.Save(ctx, tg)
}

func (s *TargetService) AwaitEmptyList(ctx context.Context, waitTime time.Duration) error {
timeout := time.NewTimer(waitTime)
defer timeout.Stop()

for {
select {
case <-timeout.C:
return errors.New("awaiting empty build list timed out")
default:
targets, err := s.ListTargets(ctx, nil, services.TargetRetrievalParams{})
if err != nil {
return err
}

if len(targets) == 0 {
return nil
}

time.Sleep(time.Second)
}
}
}
25 changes: 25 additions & 0 deletions pkg/server/workspaces/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ package workspaces

import (
"context"
"errors"
"io"
"time"

"github.com/daytonaio/daytona/pkg/gitprovider"
"github.com/daytonaio/daytona/pkg/logs"
Expand Down Expand Up @@ -103,3 +105,26 @@ func (s *WorkspaceService) UpdateWorkspaceProviderMetadata(ctx context.Context,
w.ProviderMetadata = &metadata
return s.workspaceStore.Save(ctx, w)
}

func (s *WorkspaceService) AwaitEmptyList(ctx context.Context, waitTime time.Duration) error {
timeout := time.NewTimer(waitTime)
defer timeout.Stop()

for {
select {
case <-timeout.C:
return errors.New("awaiting empty build list timed out")
default:
workspaces, err := s.ListWorkspaces(ctx, services.WorkspaceRetrievalParams{})
if err != nil {
return err
}

if len(workspaces) == 0 {
return nil
}

time.Sleep(time.Second)
}
}
}
2 changes: 2 additions & 0 deletions pkg/services/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"errors"
"io"
"time"

"github.com/daytonaio/daytona/pkg/models"
"github.com/daytonaio/daytona/pkg/stores"
Expand All @@ -24,6 +25,7 @@ type ITargetService interface {
RemoveTarget(ctx context.Context, targetId string) error
ForceRemoveTarget(ctx context.Context, targetId string) error
HandleSuccessfulCreation(ctx context.Context, targetId string) error
AwaitEmptyList(ctx context.Context, waitTime time.Duration) error

GetTargetLogReader(ctx context.Context, targetId string) (io.Reader, error)
GetTargetLogWriter(ctx context.Context, targetId string) (io.WriteCloser, error)
Expand Down
2 changes: 2 additions & 0 deletions pkg/services/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"errors"
"io"
"time"

"github.com/daytonaio/daytona/pkg/gitprovider"
"github.com/daytonaio/daytona/pkg/models"
Expand All @@ -21,6 +22,7 @@ type IWorkspaceService interface {
RemoveWorkspace(ctx context.Context, workspaceId string) error
ForceRemoveWorkspace(ctx context.Context, workspaceId string) error
UpdateWorkspaceProviderMetadata(ctx context.Context, workspaceId, metadata string) error
AwaitEmptyList(ctx context.Context, waitTime time.Duration) error

GetWorkspaceLogReader(ctx context.Context, workspaceId string) (io.Reader, error)
GetWorkspaceLogWriter(ctx context.Context, workspaceId string) (io.WriteCloser, error)
Expand Down

0 comments on commit b6bdf4c

Please sign in to comment.