Skip to content

Commit

Permalink
refa: improve signature of StartDiscoveryTask
Browse files Browse the repository at this point in the history
  • Loading branch information
joshiste committed Oct 7, 2023
1 parent fb19415 commit e2a5664
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 25 deletions.
5 changes: 3 additions & 2 deletions extaz/availablity_zone_discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/steadybit/extension-kit/extutil"
"net/http"
"os"
"time"
)

var (
Expand All @@ -36,7 +37,7 @@ func RegisterDiscoveryHandlers(stopCh chan os.Signal) {
utils.StartDiscoveryTask(
stopCh,
"availability zone",
config.Config.DiscoveryIntervalZone,
time.Duration(config.Config.DiscoveryIntervalZone)*time.Second,
getTargetsForAccount,
func(updatedTargets []discovery_kit_api.Target, err *extension_kit.ExtensionError) {
targets = &updatedTargets
Expand Down Expand Up @@ -79,7 +80,7 @@ func getAZTargetDescription() discovery_kit_api.TargetDescription {
}
}

func getAZDiscoveryResults(w http.ResponseWriter, r *http.Request, _ []byte) {
func getAZDiscoveryResults(w http.ResponseWriter, _ *http.Request, _ []byte) {
if discoveryError != nil {
exthttp.WriteError(w, *discoveryError)
} else {
Expand Down
3 changes: 2 additions & 1 deletion extec2/instance_discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"net/http"
"os"
"strings"
"time"
)

var (
Expand Down Expand Up @@ -51,7 +52,7 @@ func RegisterDiscoveryHandlers(stopCh chan os.Signal) {
utils.StartDiscoveryTask(
stopCh,
"ec2 instance",
config.Config.DiscoveryIntervalEc2,
time.Duration(config.Config.DiscoveryIntervalEc2)*time.Second,
getTargetsForAccount,
func(updatedTargets []discovery_kit_api.Target, err *extension_kit.ExtensionError) {
targets = &updatedTargets
Expand Down
10 changes: 5 additions & 5 deletions extfis/template_discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func RegisterFisInstanceDiscoveryHandlers(stopCh chan os.Signal) {
utils.StartDiscoveryTask(
stopCh,
"fis template",
config.Config.DiscoveryIntervalFis,
time.Duration(config.Config.DiscoveryIntervalFis)*time.Second,
getTargetsForAccount,
func(updatedTargets []discovery_kit_api.Target, err *extension_kit.ExtensionError) {
targets = &updatedTargets
Expand Down Expand Up @@ -112,7 +112,7 @@ func getFisTemplateAttributeDescriptions() discovery_kit_api.AttributeDescriptio
}
}

func getFisTemplateTargets(w http.ResponseWriter, r *http.Request, _ []byte) {
func getFisTemplateTargets(w http.ResponseWriter, _ *http.Request, _ []byte) {
if discoveryError != nil {
exthttp.WriteError(w, *discoveryError)
} else {
Expand Down Expand Up @@ -255,9 +255,9 @@ func calculateTotalDuration(experimentTemplate *types.ExperimentTemplate) *time.

log.Debug().Msgf("Calculated total durations for FIS Experiment %s: %v", *experimentTemplate.Id, totalDurations)
longestDuration := time.Duration(0)
for _, duration := range totalDurations {
if duration > longestDuration {
longestDuration = duration
for _, d := range totalDurations {
if d > longestDuration {
longestDuration = d
}
}
return &longestDuration
Expand Down
5 changes: 3 additions & 2 deletions extlambda/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"net/http"
"os"
"strconv"
"time"
)

const discoveryBasePath = "/lambda/discovery"
Expand All @@ -42,7 +43,7 @@ func RegisterDiscoveryHandlers(stopCh chan os.Signal) {
utils.StartDiscoveryTask(
stopCh,
"lambda function",
config.Config.DiscoveryIntervalLambda,
time.Duration(config.Config.DiscoveryIntervalLambda)*time.Second,
getTargetsForAccount,
func(updatedTargets []discovery_kit_api.Target, err *extension_kit.ExtensionError) {
targets = &updatedTargets
Expand Down Expand Up @@ -188,7 +189,7 @@ func getAttributeDescriptions() discovery_kit_api.AttributeDescriptions {
}
}

func getDiscoveredTargets(w http.ResponseWriter, r *http.Request, _ []byte) {
func getDiscoveredTargets(w http.ResponseWriter, _ *http.Request, _ []byte) {
if discoveryError != nil {
exthttp.WriteError(w, *discoveryError)
} else {
Expand Down
5 changes: 3 additions & 2 deletions extrds/cluster_discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/steadybit/extension-kit/extutil"
"net/http"
"os"
"time"
)

var (
Expand All @@ -37,7 +38,7 @@ func RegisterClusterDiscoveryHandlers(stopCh chan os.Signal) {
utils.StartDiscoveryTask(
stopCh,
"rds cluster",
config.Config.DiscoveryIntervalRds,
time.Duration(config.Config.DiscoveryIntervalRds)*time.Second,
getClusterTargetsForAccount,
func(updatedTargets []discovery_kit_api.Target, err *extension_kit.ExtensionError) {
clusterTargets = &updatedTargets
Expand Down Expand Up @@ -131,7 +132,7 @@ func getRdsClusterAttributeDescriptions() discovery_kit_api.AttributeDescription
}
}

func getRdsClusterDiscoveryResults(w http.ResponseWriter, r *http.Request, _ []byte) {
func getRdsClusterDiscoveryResults(w http.ResponseWriter, _ *http.Request, _ []byte) {
if clusterDiscoveryError != nil {
exthttp.WriteError(w, *clusterDiscoveryError)
} else {
Expand Down
5 changes: 3 additions & 2 deletions extrds/instance_discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/steadybit/extension-kit/extutil"
"net/http"
"os"
"time"
)

var (
Expand All @@ -38,7 +39,7 @@ func RegisterInstanceDiscoveryHandlers(stopCh chan os.Signal) {
utils.StartDiscoveryTask(
stopCh,
"rds instance",
config.Config.DiscoveryIntervalRds,
time.Duration(config.Config.DiscoveryIntervalRds)*time.Second,
getInstanceTargetsForAccount,
func(updatedTargets []discovery_kit_api.Target, err *extension_kit.ExtensionError) {
instanceTargets = &updatedTargets
Expand Down Expand Up @@ -116,7 +117,7 @@ func getRdsInstanceAttributeDescriptions() discovery_kit_api.AttributeDescriptio
}
}

func getRdsInstanceDiscoveryResults(w http.ResponseWriter, r *http.Request, _ []byte) {
func getRdsInstanceDiscoveryResults(w http.ResponseWriter, _ *http.Request, _ []byte) {
if discoveryError != nil {
exthttp.WriteError(w, *discoveryError)
} else {
Expand Down
22 changes: 11 additions & 11 deletions utils/discovery_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,37 @@ import (

func StartDiscoveryTask(
stopCh chan os.Signal,
discovery string,
interval int,
discoveryName string,
interval time.Duration,
supplier func(account *AwsAccount, ctx context.Context) (*[]discovery_kit_api.Target, error),
updateResults func(updatedTargets []discovery_kit_api.Target, err *extension_kit.ExtensionError)) {
//init empty results
updateResults([]discovery_kit_api.Target{}, nil)
//start first discovery immediately
discover(supplier, discovery, updateResults)
discover(supplier, discoveryName, updateResults)
//start loop
go func() {
log.Info().Msgf("Starting %s discovery", discovery)
log.Info().Msgf("Starting %s discovery", discoveryName)
for {
select {
case <-stopCh:
log.Info().Msgf("Stopping %s discovery", discovery)
log.Info().Msgf("Stopping %s discovery", discoveryName)
return
case <-time.After(time.Duration(interval) * time.Second):
discover(supplier, discovery, updateResults)
case <-time.After(interval):
discover(supplier, discoveryName, updateResults)
}
}
}()
}

func discover(supplier func(account *AwsAccount, ctx context.Context) (*[]discovery_kit_api.Target, error), discovery string, updateResults func(updatedTargets []discovery_kit_api.Target, err *extension_kit.ExtensionError)) {
func discover(supplier func(account *AwsAccount, ctx context.Context) (*[]discovery_kit_api.Target, error), discoveryName string, updateResults func(updatedTargets []discovery_kit_api.Target, err *extension_kit.ExtensionError)) {
start := time.Now()
updatedTargets, err := ForEveryAccount(Accounts, supplier, context.Background(), discovery)
updatedTargets, err := ForEveryAccount(Accounts, supplier, context.Background(), discoveryName)
if err != nil {
updateResults([]discovery_kit_api.Target{}, extutil.Ptr(extension_kit.ToError(fmt.Sprintf("Failed to collect %s information", discovery), err)))
updateResults([]discovery_kit_api.Target{}, extutil.Ptr(extension_kit.ToError(fmt.Sprintf("Failed to collect %s information", discoveryName), err)))
} else {
updateResults(*updatedTargets, nil)
elapsed := time.Since(start)
log.Debug().Msgf("Updated %d %s targets in %s", len(*updatedTargets), discovery, elapsed)
log.Debug().Msgf("Updated %d %s targets in %s", len(*updatedTargets), discoveryName, elapsed)
}
}

0 comments on commit e2a5664

Please sign in to comment.