Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Enhance Timer to be used for timed loops beyond bootstrapping #141

Merged
merged 1 commit into from
Jan 4, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions bootstrap/startup/timer.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,30 @@ func NewStartUpTimer(serviceKey string) Timer {
}
}

// NewTimer is a factory method that returns a Timer initialized with passed in duration and interval.
func NewTimer(duration int, interval int) Timer {
return Timer{
startTime: time.Now(),
duration: time.Second * time.Duration(duration),
interval: time.Second * time.Duration(interval),
}
}

// SinceAsString returns the time since the timer was created as a string.
func (t Timer) SinceAsString() string {
return time.Since(t.startTime).String()
}

// RemainingAsString returns the time remaining on the timer as a string.
func (t Timer) RemainingAsString() string {

remaining := t.duration - time.Since(t.startTime)
if remaining < 0 {
remaining = 0
}
return remaining.String()
}

// HasNotElapsed returns whether or not the duration specified during construction has elapsed.
func (t Timer) HasNotElapsed() bool {
return time.Now().Before(t.startTime.Add(t.duration))
Expand Down