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 13, 2024
1 parent f336f54 commit acb55fd
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 3 deletions.
18 changes: 18 additions & 0 deletions pkg/cmd/purge.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,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
48 changes: 45 additions & 3 deletions pkg/server/purge.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,46 @@ func (s *Server) Purge(ctx context.Context, force bool) []error {
}
}

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

err := server.Start()
workspaces, err := s.WorkspaceService.ListWorkspaces(ctx, services.WorkspaceRetrievalParams{})
if err != nil {
s.trackPurgeError(ctx, force, err)
return []error{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{})
if err != nil {
s.trackPurgeError(ctx, force, err)
Expand All @@ -63,6 +95,16 @@ 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)
}
}

fmt.Println("Purging providers...")
err = s.ProviderManager.Purge()
if err != nil {
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 @@ -71,3 +73,26 @@ type TargetService struct {
func (s *TargetService) GetTargetLogReader(targetId string) (io.Reader, error) {
return s.loggerFactory.CreateTargetLogReader(targetId)
}

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 @@ -93,3 +95,26 @@ type WorkspaceService struct {
func (s *WorkspaceService) GetWorkspaceLogReader(ctx context.Context, workspaceId string) (io.Reader, error) {
return s.loggerFactory.CreateWorkspaceLogReader(workspaceId)
}

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 @@ -23,6 +24,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

SetTargetMetadata(ctx context.Context, targetId string, metadata *models.TargetMetadata) (*models.TargetMetadata, 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 @@ -20,6 +21,7 @@ type IWorkspaceService interface {
StopWorkspace(ctx context.Context, workspaceId string) error
RemoveWorkspace(ctx context.Context, workspaceId string) error
ForceRemoveWorkspace(ctx context.Context, workspaceId string) error
AwaitEmptyList(ctx context.Context, waitTime time.Duration) error

GetWorkspaceLogReader(ctx context.Context, workspaceId string) (io.Reader, error)
SetWorkspaceMetadata(ctx context.Context, workspaceId string, metadata *models.WorkspaceMetadata) (*models.WorkspaceMetadata, error)
Expand Down

0 comments on commit acb55fd

Please sign in to comment.