Skip to content

Commit

Permalink
Add error handling for invalid labels to AddInstance() (#195)
Browse files Browse the repository at this point in the history
* Add error handling for invalid labels to AddInstance()

* fix to use const type
  • Loading branch information
site0801 authored Mar 14, 2024
1 parent 907a272 commit 257d0b4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pkg/shoes/shoes.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"github.com/whywaita/myshoes/pkg/datastore"

"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

// GetClient retrieve ShoesClient use shoes-plugin
Expand Down Expand Up @@ -88,6 +90,10 @@ func (c *GRPCClient) AddInstance(ctx context.Context, runnerName, setupScript st
}
resp, err := c.client.AddInstance(ctx, req)
if err != nil {
// will delete a job if labels of a job are invalid
if stat, _ := status.FromError(err); stat.Code() == codes.InvalidArgument {
return "", "", "", datastore.ResourceTypeUnknown, err
}
return "", "", "", datastore.ResourceTypeUnknown, fmt.Errorf("failed to AddInstance: %w", err)
}

Expand Down
17 changes: 17 additions & 0 deletions pkg/starter/starter.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (

"golang.org/x/sync/errgroup"
"golang.org/x/sync/semaphore"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/google/go-github/v47/github"
uuid "github.com/satori/go.uuid"
Expand Down Expand Up @@ -198,6 +200,18 @@ func (s *Starter) ProcessJob(ctx context.Context, job datastore.Job) error {
if err != nil {
logger.Logf(false, "failed to bung (target ID: %s, job ID: %s): %+v\n", job.TargetID, job.UUID, err)

if stat, _ := status.FromError(err); stat.Code() == codes.InvalidArgument {
if err := s.ds.DeleteJob(ctx, job.UUID); err != nil {
logger.Logf(false, "failed to delete job: %+v\n", err)

if err := datastore.UpdateTargetStatus(ctx, s.ds, job.TargetID, datastore.TargetStatusErr, fmt.Sprintf("job id: %s", job.UUID)); err != nil {
return fmt.Errorf("failed to update target status (target ID: %s, job ID: %s): %w", job.TargetID, job.UUID, err)
}

return fmt.Errorf("failed to delete job: %w", err)
}
}

if err := datastore.UpdateTargetStatus(ctx, s.ds, job.TargetID, datastore.TargetStatusErr, fmt.Sprintf("failed to create an instance (job ID: %s)", job.UUID)); err != nil {
return fmt.Errorf("failed to update target status (target ID: %s, job ID: %s): %w", job.TargetID, job.UUID, err)
}
Expand Down Expand Up @@ -288,6 +302,9 @@ func (s *Starter) bung(ctx context.Context, job datastore.Job, target datastore.

cloudID, ipAddress, shoesType, resourceType, err := client.AddInstance(ctx, runnerName, script, target.ResourceType, labels)
if err != nil {
if stat, _ := status.FromError(err); stat.Code() == codes.InvalidArgument {
return "", "", "", datastore.ResourceTypeUnknown, err
}
return "", "", "", datastore.ResourceTypeUnknown, fmt.Errorf("failed to add instance: %w", err)
}

Expand Down

0 comments on commit 257d0b4

Please sign in to comment.