Do federation wakeup/stop
This commit is contained in:
parent
435d1d8f98
commit
9fa5a378e1
20
api/alarm.go
20
api/alarm.go
@ -1,11 +1,14 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
|
||||||
"git.nemunai.re/nemunaire/reveil/config"
|
"git.nemunai.re/nemunaire/reveil/config"
|
||||||
|
"git.nemunai.re/nemunaire/reveil/model"
|
||||||
"git.nemunai.re/nemunaire/reveil/player"
|
"git.nemunai.re/nemunaire/reveil/player"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -20,7 +23,7 @@ func declareAlarmRoutes(cfg *config.Config, router *gin.RouterGroup) {
|
|||||||
|
|
||||||
router.POST("/alarm/run", func(c *gin.Context) {
|
router.POST("/alarm/run", func(c *gin.Context) {
|
||||||
if player.CommonPlayer == nil {
|
if player.CommonPlayer == nil {
|
||||||
err := player.WakeUp(cfg, nil)
|
err := player.WakeUp(cfg, nil, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
||||||
return
|
return
|
||||||
@ -51,6 +54,21 @@ func declareAlarmRoutes(cfg *config.Config, router *gin.RouterGroup) {
|
|||||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
settings, err := reveil.ReadSettings(cfg.SettingsFile)
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": fmt.Errorf("Unable to read settings: %s", err.Error())})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for k, srv := range settings.Federation {
|
||||||
|
err = player.FederatedStop(srv)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Unable to do federated wakeup on %s: %s", k, err.Error())
|
||||||
|
} else {
|
||||||
|
log.Printf("Federated wakeup on %s: launched!", k)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, true)
|
c.JSON(http.StatusOK, true)
|
||||||
|
@ -13,7 +13,7 @@ import (
|
|||||||
|
|
||||||
func declareAlarmsRoutes(cfg *config.Config, db *reveil.LevelDBStorage, resetTimer func(), router *gin.RouterGroup) {
|
func declareAlarmsRoutes(cfg *config.Config, db *reveil.LevelDBStorage, resetTimer func(), router *gin.RouterGroup) {
|
||||||
router.GET("/alarms/next", func(c *gin.Context) {
|
router.GET("/alarms/next", func(c *gin.Context) {
|
||||||
alarm, _, err := reveil.GetNextAlarm(cfg, db)
|
alarm, _, _, err := reveil.GetNextAlarm(cfg, db)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
||||||
return
|
return
|
||||||
|
50
api/federation.go
Normal file
50
api/federation.go
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
|
||||||
|
"git.nemunai.re/nemunaire/reveil/config"
|
||||||
|
"git.nemunai.re/nemunaire/reveil/player"
|
||||||
|
)
|
||||||
|
|
||||||
|
func declareFederationRoutes(cfg *config.Config, router *gin.RouterGroup) {
|
||||||
|
router.POST("/federation/wakeup", func(c *gin.Context) {
|
||||||
|
var s map[string]interface{}
|
||||||
|
c.ShouldBind(s)
|
||||||
|
|
||||||
|
if player.CommonPlayer == nil {
|
||||||
|
var seed int64
|
||||||
|
if tmp, ok := s["seed"].(int64); ok {
|
||||||
|
seed = tmp
|
||||||
|
} else {
|
||||||
|
seed := time.Now().Unix()
|
||||||
|
seed -= seed % 172800
|
||||||
|
}
|
||||||
|
|
||||||
|
err := player.WakeUpFromFederation(cfg, seed, nil)
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": "Player already running"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, true)
|
||||||
|
})
|
||||||
|
router.POST("/federation/wakeok", func(c *gin.Context) {
|
||||||
|
if player.CommonPlayer != nil {
|
||||||
|
err := player.CommonPlayer.Stop()
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, true)
|
||||||
|
})
|
||||||
|
}
|
@ -13,6 +13,7 @@ func DeclareRoutes(router *gin.Engine, cfg *config.Config, db *reveil.LevelDBSto
|
|||||||
declareActionsRoutes(cfg, apiRoutes)
|
declareActionsRoutes(cfg, apiRoutes)
|
||||||
declareAlarmRoutes(cfg, apiRoutes)
|
declareAlarmRoutes(cfg, apiRoutes)
|
||||||
declareAlarmsRoutes(cfg, db, resetTimer, apiRoutes)
|
declareAlarmsRoutes(cfg, db, resetTimer, apiRoutes)
|
||||||
|
declareFederationRoutes(cfg, apiRoutes)
|
||||||
declareGongsRoutes(cfg, apiRoutes)
|
declareGongsRoutes(cfg, apiRoutes)
|
||||||
declareHistoryRoutes(cfg, apiRoutes)
|
declareHistoryRoutes(cfg, apiRoutes)
|
||||||
declareQuotesRoutes(cfg, apiRoutes)
|
declareQuotesRoutes(cfg, apiRoutes)
|
||||||
|
4
app.go
4
app.go
@ -79,7 +79,7 @@ func (app *App) ResetTimer() {
|
|||||||
app.nextAlarm = nil
|
app.nextAlarm = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if na, routines, err := reveil.GetNextAlarm(app.cfg, app.db); err == nil && na != nil {
|
if na, routines, federated, err := reveil.GetNextAlarm(app.cfg, app.db); err == nil && na != nil {
|
||||||
app.nextAlarm = time.AfterFunc(time.Until(*na), func() {
|
app.nextAlarm = time.AfterFunc(time.Until(*na), func() {
|
||||||
app.nextAlarm = nil
|
app.nextAlarm = nil
|
||||||
reveil.RemoveOldAlarmsSingle(app.db)
|
reveil.RemoveOldAlarmsSingle(app.db)
|
||||||
@ -87,7 +87,7 @@ func (app *App) ResetTimer() {
|
|||||||
// Rearm timer for the next time
|
// Rearm timer for the next time
|
||||||
app.ResetTimer()
|
app.ResetTimer()
|
||||||
|
|
||||||
err := player.WakeUp(app.cfg, routines)
|
err := player.WakeUp(app.cfg, routines, federated)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err.Error())
|
log.Println(err.Error())
|
||||||
return
|
return
|
||||||
|
@ -40,25 +40,27 @@ func (h *Hour) UnmarshalJSON(src []byte) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetNextAlarm(cfg *config.Config, db *LevelDBStorage) (*time.Time, []Identifier, error) {
|
func GetNextAlarm(cfg *config.Config, db *LevelDBStorage) (*time.Time, []Identifier, bool, error) {
|
||||||
alarmsRepeated, err := GetAlarmsRepeated(db)
|
alarmsRepeated, err := GetAlarmsRepeated(db)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var closestAlarm *time.Time
|
var closestAlarm *time.Time
|
||||||
var closestAlarmRoutines []Identifier
|
var closestAlarmRoutines []Identifier
|
||||||
|
var closestAlarmFederated bool
|
||||||
for _, alarm := range alarmsRepeated {
|
for _, alarm := range alarmsRepeated {
|
||||||
next := alarm.GetNextOccurence(cfg, db)
|
next := alarm.GetNextOccurence(cfg, db)
|
||||||
if next != nil && (closestAlarm == nil || closestAlarm.After(*next)) {
|
if next != nil && (closestAlarm == nil || closestAlarm.After(*next)) {
|
||||||
closestAlarm = next
|
closestAlarm = next
|
||||||
closestAlarmRoutines = alarm.FollowingRoutines
|
closestAlarmRoutines = alarm.FollowingRoutines
|
||||||
|
closestAlarmFederated = alarm.EnableFederation
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
alarmsSingle, err := GetAlarmsSingle(db)
|
alarmsSingle, err := GetAlarmsSingle(db)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
@ -66,10 +68,11 @@ func GetNextAlarm(cfg *config.Config, db *LevelDBStorage) (*time.Time, []Identif
|
|||||||
if closestAlarm == nil || (closestAlarm.After(alarm.Time) && alarm.Time.After(now)) {
|
if closestAlarm == nil || (closestAlarm.After(alarm.Time) && alarm.Time.After(now)) {
|
||||||
closestAlarm = &alarm.Time
|
closestAlarm = &alarm.Time
|
||||||
closestAlarmRoutines = alarm.FollowingRoutines
|
closestAlarmRoutines = alarm.FollowingRoutines
|
||||||
|
closestAlarmFederated = alarm.EnableFederation
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return closestAlarm, closestAlarmRoutines, nil
|
return closestAlarm, closestAlarmRoutines, closestAlarmFederated, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetNextException(cfg *config.Config, db *LevelDBStorage) (*time.Time, error) {
|
func GetNextException(cfg *config.Config, db *LevelDBStorage) (*time.Time, error) {
|
||||||
@ -90,7 +93,7 @@ func GetNextException(cfg *config.Config, db *LevelDBStorage) (*time.Time, error
|
|||||||
}
|
}
|
||||||
|
|
||||||
func DropNextAlarm(cfg *config.Config, db *LevelDBStorage) error {
|
func DropNextAlarm(cfg *config.Config, db *LevelDBStorage) error {
|
||||||
timenext, _, err := GetNextAlarm(cfg, db)
|
timenext, _, _, err := GetNextAlarm(cfg, db)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
35
player/federation.go
Normal file
35
player/federation.go
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
package player
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"git.nemunai.re/nemunaire/reveil/model"
|
||||||
|
)
|
||||||
|
|
||||||
|
func FederatedWakeUp(srv reveil.FederationSettings, seed int64) error {
|
||||||
|
req := map[string]interface{}{"seed": seed}
|
||||||
|
req_enc, err := json.Marshal(req)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
res, err := http.Post(srv.URL+"/api/federation/wakeup", "application/json", bytes.NewBuffer(req_enc))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
res.Body.Close()
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func FederatedStop(srv reveil.FederationSettings) error {
|
||||||
|
res, err := http.Post(srv.URL+"/api/federation/wakeok", "application/json", nil)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
res.Body.Close()
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
@ -43,13 +43,34 @@ type Player struct {
|
|||||||
playedItem int
|
playedItem int
|
||||||
}
|
}
|
||||||
|
|
||||||
func WakeUp(cfg *config.Config, routine []reveil.Identifier) (err error) {
|
func WakeUp(cfg *config.Config, routine []reveil.Identifier, federated bool) (err error) {
|
||||||
if CommonPlayer != nil {
|
if CommonPlayer != nil {
|
||||||
return fmt.Errorf("Unable to start the player: a player is already running")
|
return fmt.Errorf("Unable to start the player: a player is already running")
|
||||||
}
|
}
|
||||||
|
|
||||||
seed := time.Now().Unix()
|
seed := time.Now().Unix()
|
||||||
seed -= seed % 172800
|
seed -= seed % 172800
|
||||||
|
|
||||||
|
if federated {
|
||||||
|
settings, err := reveil.ReadSettings(cfg.SettingsFile)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Unable to read settings: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for k, srv := range settings.Federation {
|
||||||
|
err = FederatedWakeUp(srv, seed)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Unable to do federated wakeup on %s: %s", k, err.Error())
|
||||||
|
} else {
|
||||||
|
log.Printf("Federated wakeup on %s: launched!", k)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return WakeUpFromFederation(cfg, seed, routine)
|
||||||
|
}
|
||||||
|
|
||||||
|
func WakeUpFromFederation(cfg *config.Config, seed int64, routine []reveil.Identifier) (err error) {
|
||||||
rand.Seed(seed)
|
rand.Seed(seed)
|
||||||
|
|
||||||
CommonPlayer, err = NewPlayer(cfg, routine)
|
CommonPlayer, err = NewPlayer(cfg, routine)
|
||||||
|
@ -24,7 +24,7 @@ func DeclareNoJSRoutes(router *gin.Engine, cfg *config.Config, db *reveil.LevelD
|
|||||||
router.SetHTMLTemplate(templ)
|
router.SetHTMLTemplate(templ)
|
||||||
|
|
||||||
router.GET("/nojs.html", func(c *gin.Context) {
|
router.GET("/nojs.html", func(c *gin.Context) {
|
||||||
alarm, _, err := reveil.GetNextAlarm(cfg, db)
|
alarm, _, _, err := reveil.GetNextAlarm(cfg, db)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.HTML(http.StatusInternalServerError, "error.tmpl", gin.H{"errmsg": err.Error()})
|
c.HTML(http.StatusInternalServerError, "error.tmpl", gin.H{"errmsg": err.Error()})
|
||||||
return
|
return
|
||||||
@ -122,7 +122,7 @@ func DeclareNoJSRoutes(router *gin.Engine, cfg *config.Config, db *reveil.LevelD
|
|||||||
|
|
||||||
case "start":
|
case "start":
|
||||||
if player.CommonPlayer == nil {
|
if player.CommonPlayer == nil {
|
||||||
err := player.WakeUp(cfg, nil)
|
err := player.WakeUp(cfg, nil, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.HTML(http.StatusInternalServerError, "error.tmpl", gin.H{"errmsg": err.Error()})
|
c.HTML(http.StatusInternalServerError, "error.tmpl", gin.H{"errmsg": err.Error()})
|
||||||
return
|
return
|
||||||
|
Loading…
Reference in New Issue
Block a user