Skip to content

Commit

Permalink
Proper handle empty string + error handling (#86)
Browse files Browse the repository at this point in the history
* empty strings should be ignored

Signed-off-by: Lasse Gaardsholt <[email protected]>

* better error handling

Signed-off-by: Lasse Gaardsholt <[email protected]>

* oops

Signed-off-by: Lasse Gaardsholt <[email protected]>

---------

Signed-off-by: Lasse Gaardsholt <[email protected]>
  • Loading branch information
Gaardsholt authored Oct 30, 2023
1 parent 6955239 commit 4b9d5c9
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 14 deletions.
6 changes: 4 additions & 2 deletions pubsub/listen.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ func Listen() error {
// Unmarshal message
var m PubSubMsg
if err := json.Unmarshal(msg.Data, &m); err != nil {
logger.Log.Fatal().Err(err).Msg("Failed to unmarshal message")
logger.Log.Error().Err(err).Msg("Failed to unmarshal message")
return
}

if m.Cluster != config.Config.ClusterName {
Expand All @@ -53,7 +54,8 @@ func Listen() error {
logger.Log.Info().Msgf("Scaling %s namespace %s up", m.Cluster, m.Namespace)
convertIntToTimeDuration, err := time.ParseDuration(fmt.Sprintf("%dh", m.Duration))
if err != nil {
logger.Log.Fatal().Err(err).Msg("Failed to parse duration")
logger.Log.Error().Err(err).Msg("Failed to parse duration")
return
}

logger.Log.Debug().Msgf("Duration: %d, Duration in time.Duration: %s", m.Duration, convertIntToTimeDuration)
Expand Down
22 changes: 18 additions & 4 deletions pubsub/sendNamespaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,42 @@ import (
"github.com/BESTSELLER/nightscaler/k8s"
"github.com/BESTSELLER/nightscaler/logger"
"github.com/BESTSELLER/nightscaler/timeutil"
v1 "k8s.io/api/core/v1"
)

func SendNamespaces() {
namespaces, err := k8s.List()
if err != nil {
logger.Log.Fatal().Err(err).Msg("failed to list namespaces")
logger.Log.Error().Err(err).Msg("failed to list namespaces")
}

tempNamespaces := []v1.Namespace{}

for _, ns := range namespaces {
s := ns.Annotations[config.NAMESPACE_ANNOTATION]

startTime, endTime := timeutil.GetUptimes(s)
if s == "" {
continue
}

startTime, endTime, err := timeutil.GetUptimes(s)
if err != nil {
logger.Log.Error().Err(err).Msg("failed to parse uptime")
return
}

ns.Annotations["nightscaler/uptime-start"] = strconv.FormatInt(startTime.UnixMilli(), 10)
ns.Annotations["nightscaler/uptime-end"] = strconv.FormatInt(endTime.UnixMilli(), 10)

tempNamespaces = append(tempNamespaces, ns)
}

err = Publish(namespaces, Attributes{
err = Publish(tempNamespaces, Attributes{
Entity: "namespace",
Action: "create",
})
if err != nil {
logger.Log.Fatal().Err(err).Msg("failed to send Pub/Sub message")
logger.Log.Error().Err(err).Msg("failed to send Pub/Sub message")
return
}
}
21 changes: 13 additions & 8 deletions timeutil/timeutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,36 +24,41 @@ var WeekDay = map[string]Day{
"Sun": {"Sunday", 6},
}

func GetUptimes(s string) (time.Time, time.Time) {
_, startTime, endTime := isUptimeNow(s)
func GetUptimes(s string) (time.Time, time.Time, error) {
_, startTime, endTime, err := isUptimeNow(s)

return startTime, endTime
return startTime, endTime, err
}

func isUptimeNow(s string) (bool, time.Time, time.Time) {
func isUptimeNow(s string) (bool, time.Time, time.Time, error) {
now := time.Now()

uptimeSplit := strings.Split(s, " ")

if len(uptimeSplit) != 3 {
return false, time.Time{}, time.Time{}, fmt.Errorf("'%s' is an invalid uptime format", s)
}

weekDays := uptimeSplit[0]
timeRange := uptimeSplit[1]
timeZone := uptimeSplit[2]

days, err := getWeekDays(weekDays)
if err != nil {
panic(err)
return false, time.Time{}, time.Time{}, err
}

isIt := isCurrentDay(days, now)
if !isIt {
return false, time.Time{}, time.Time{}
return false, time.Time{}, time.Time{}, nil
}

startTime, endTime, err := getTimeBoundary(timeRange, timeZone, now)
if err != nil {
panic(err)
return false, time.Time{}, time.Time{}, err
}

return now.After(startTime) && now.Before(endTime), startTime, endTime
return now.After(startTime) && now.Before(endTime), startTime, endTime, nil
}

func getWeekDays(weekDays string) ([]Day, error) {
Expand Down

0 comments on commit 4b9d5c9

Please sign in to comment.