morningalarm/internal/cron.go

40 lines
868 B
Go

package morningalarm
import (
"time"
"github.com/robfig/cron/v3"
)
// nextAlarm returns the time of the next alarm to fire from the perspective of cron
func (ma *MorningAlarm) nextAlarm() *time.Time {
if len(ma.cr.Entries()) == 0 {
return nil
}
min := ma.cr.Entries()[0].Next
for _, entry := range ma.cr.Entries() {
if entry.Next.Before(min) {
min = entry.Next
}
}
return &min
}
// armAlarm arms an alarm to fire at the specified time
func (ma *MorningAlarm) armAlarm(alarm *alarm) (cron.EntryID, error) {
return ma.cr.AddFunc(alarm.Time, ma.fireAlarm)
}
// disarmAlarm disarms an alarm and prevents it from firing at the specified time
func (ma *MorningAlarm) disarmAlarm(alarm *alarm) {
ma.cr.Remove(alarm.cronID)
}
// fireAlarm function that is called when an alarm is fired
func (ma *MorningAlarm) fireAlarm() {
ma.playWakeUpMusic()
}