Skip to content

Commit

Permalink
Resolves #376 - Cleans up shutdown handling
Browse files Browse the repository at this point in the history
  • Loading branch information
steve-r-west committed Oct 1, 2023
1 parent d19c621 commit 1c108e5
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 20 deletions.
6 changes: 3 additions & 3 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (
"github.com/elasticpath/epcc-cli/external/aliases"
"github.com/elasticpath/epcc-cli/external/autofill"
"github.com/elasticpath/epcc-cli/external/completion"
"github.com/elasticpath/epcc-cli/external/crud"
"github.com/elasticpath/epcc-cli/external/encoding"
"github.com/elasticpath/epcc-cli/external/httpclient"
"github.com/elasticpath/epcc-cli/external/json"
"github.com/elasticpath/epcc-cli/external/resources"
"github.com/elasticpath/epcc-cli/external/shutdown"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"io"
Expand Down Expand Up @@ -215,8 +215,8 @@ func NewCreateCommand(parentCmd *cobra.Command) func() {
}

func createInternal(ctx context.Context, overrides *httpclient.HttpParameterOverrides, args []string, autoFillOnCreate bool, aliasName string) (string, error) {
crud.OutstandingRequestCounter.Add(1)
defer crud.OutstandingRequestCounter.Done()
shutdown.OutstandingOpCounter.Add(1)
defer shutdown.OutstandingOpCounter.Done()

// Find Resource
resource, ok := resources.GetResourceByName(args[0])
Expand Down
6 changes: 3 additions & 3 deletions cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"fmt"
"github.com/elasticpath/epcc-cli/external/aliases"
"github.com/elasticpath/epcc-cli/external/completion"
"github.com/elasticpath/epcc-cli/external/crud"
"github.com/elasticpath/epcc-cli/external/httpclient"
"github.com/elasticpath/epcc-cli/external/json"
"github.com/elasticpath/epcc-cli/external/resources"
"github.com/elasticpath/epcc-cli/external/shutdown"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"io"
Expand Down Expand Up @@ -171,8 +171,8 @@ func NewDeleteCommand(parentCmd *cobra.Command) func() {
return resetFunc
}
func deleteInternal(ctx context.Context, overrides *httpclient.HttpParameterOverrides, allow404 bool, args []string) (string, error) {
crud.OutstandingRequestCounter.Add(1)
defer crud.OutstandingRequestCounter.Done()
shutdown.OutstandingOpCounter.Add(1)
defer shutdown.OutstandingOpCounter.Done()

resource, ok := resources.GetResourceByName(args[0])
if !ok {
Expand Down
6 changes: 3 additions & 3 deletions cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
"fmt"
"github.com/elasticpath/epcc-cli/external/aliases"
"github.com/elasticpath/epcc-cli/external/completion"
"github.com/elasticpath/epcc-cli/external/crud"
"github.com/elasticpath/epcc-cli/external/httpclient"
"github.com/elasticpath/epcc-cli/external/json"
"github.com/elasticpath/epcc-cli/external/resources"
"github.com/elasticpath/epcc-cli/external/shutdown"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"io"
Expand Down Expand Up @@ -326,8 +326,8 @@ func getUrl(resource resources.Resource, args []string) (*resources.CrudEntityIn
}

func getResource(ctx context.Context, overrides *httpclient.HttpParameterOverrides, args []string) (*http.Response, error) {
crud.OutstandingRequestCounter.Add(1)
defer crud.OutstandingRequestCounter.Done()
shutdown.OutstandingOpCounter.Add(1)
defer shutdown.OutstandingOpCounter.Done()

// Find Resource
resource, ok := resources.GetResourceByName(args[0])
Expand Down
7 changes: 4 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"github.com/elasticpath/epcc-cli/config"
"github.com/elasticpath/epcc-cli/external/aliases"
"github.com/elasticpath/epcc-cli/external/crud"
"github.com/elasticpath/epcc-cli/external/clictx"
"github.com/elasticpath/epcc-cli/external/httpclient"
"github.com/elasticpath/epcc-cli/external/logger"
"github.com/elasticpath/epcc-cli/external/profiles"
Expand Down Expand Up @@ -221,6 +221,7 @@ func Execute() {
case sig := <-sigs:
log.Warnf("Shutting down program due to signal [%v]", sig)
shutdown.ShutdownFlag.Store(true)
clictx.Cancel()
exit = true
case <-normalShutdown:
}
Expand All @@ -231,10 +232,10 @@ func Execute() {

go func() {
time.Sleep(2 * time.Second)
log.Infof("Waiting for all outstanding requests to finish")
log.Infof("Waiting for all outstanding operations to finish")
}()

crud.OutstandingRequestCounter.Wait()
shutdown.OutstandingOpCounter.Wait()

httpclient.LogStats()
aliases.FlushAliases()
Expand Down
6 changes: 3 additions & 3 deletions cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
"fmt"
"github.com/elasticpath/epcc-cli/external/aliases"
"github.com/elasticpath/epcc-cli/external/completion"
"github.com/elasticpath/epcc-cli/external/crud"
"github.com/elasticpath/epcc-cli/external/httpclient"
"github.com/elasticpath/epcc-cli/external/json"
"github.com/elasticpath/epcc-cli/external/resources"
"github.com/elasticpath/epcc-cli/external/shutdown"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"io"
Expand Down Expand Up @@ -188,8 +188,8 @@ func NewUpdateCommand(parentCmd *cobra.Command) func() {
}

func updateInternal(ctx context.Context, overrides *httpclient.HttpParameterOverrides, args []string) (string, error) {
crud.OutstandingRequestCounter.Add(1)
defer crud.OutstandingRequestCounter.Done()
shutdown.OutstandingOpCounter.Add(1)
defer shutdown.OutstandingOpCounter.Done()

// Find Resource
resource, ok := resources.GetResourceByName(args[0])
Expand Down
11 changes: 11 additions & 0 deletions external/clictx/ctx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package clictx

import "context"

var Ctx context.Context = nil

var Cancel context.CancelFunc = nil

func init() {
Ctx, Cancel = context.WithCancel(context.Background())
}
5 changes: 0 additions & 5 deletions external/crud/outstanding_request_counter.go

This file was deleted.

5 changes: 5 additions & 0 deletions external/shutdown/op_counter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package shutdown

import "sync"

var OutstandingOpCounter = sync.WaitGroup{}

0 comments on commit 1c108e5

Please sign in to comment.