Add pre-alarm action
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing

This commit is contained in:
nemunaire 2024-10-11 17:07:42 +02:00
commit a3b00fe38d
4 changed files with 86 additions and 13 deletions

45
app.go
View file

@ -16,11 +16,12 @@ import (
)
type App struct {
cfg *config.Config
db *reveil.LevelDBStorage
router *gin.Engine
srv *http.Server
nextAlarm *time.Timer
cfg *config.Config
db *reveil.LevelDBStorage
router *gin.Engine
srv *http.Server
nextAlarm *time.Timer
nextPreAlarm *time.Timer
}
func NewApp(cfg *config.Config) *App {
@ -76,11 +77,45 @@ func (app *App) Start() {
func (app *App) ResetTimer() {
if app.nextAlarm != nil {
app.nextAlarm.Stop()
app.nextPreAlarm = nil
app.nextAlarm = nil
}
settings, _ := reveil.ReadSettings(app.cfg.SettingsFile)
if na, routines, federated, err := reveil.GetNextAlarm(app.cfg, app.db); err == nil && na != nil {
if settings != nil && settings.PreAlarmAction != "" {
app.nextPreAlarm = time.AfterFunc(time.Until(*na)-settings.PreAlarmActionDelay*time.Minute, func() {
app.nextPreAlarm = nil
settings, err := reveil.ReadSettings(app.cfg.SettingsFile)
if err != nil {
log.Println("Unable to read settings:", err.Error())
return
}
action, err := reveil.LoadAction(app.cfg, settings.PreAlarmAction)
if err != nil {
log.Println("Unable to load pre-alarm action:", err.Error())
}
cmd, err := action.Launch(settings)
if err != nil {
log.Println(err.Error())
return
}
go func() {
err := cmd.Wait()
if err != nil {
log.Printf("%q: %s", action.Name, err.Error())
}
}()
})
log.Println("Next pre-alarm programmed for", time.Time(*na).Add(settings.PreAlarmActionDelay*-1*time.Minute))
}
app.nextAlarm = time.AfterFunc(time.Until(*na), func() {
app.nextPreAlarm = nil
app.nextAlarm = nil
reveil.RemoveOldAlarmsSingle(app.db)