Fix: recover bot; llama.cpp fix
This commit is contained in:
@ -83,6 +83,7 @@ func saveFullInfo(fi *models.FullInfo) error {
|
||||
|
||||
func notifyBotIfNeeded(fi *models.FullInfo) {
|
||||
if botName := fi.Room.WhichBotToMove(); botName != "" {
|
||||
log.Debug("got botname", "name", botName)
|
||||
llmapi.SignalChanMap[botName] <- true
|
||||
}
|
||||
log.Debug("no bot", "room_id", fi.Room.ID)
|
||||
@ -231,7 +232,7 @@ func joinTeam(ctx context.Context, role, team string) (*models.FullInfo, error)
|
||||
}
|
||||
|
||||
// get all rooms
|
||||
func listPublicRooms() []*models.Room {
|
||||
func listRooms(allRooms bool) []*models.Room {
|
||||
cacheMap := memcache.GetAll()
|
||||
publicRooms := []*models.Room{}
|
||||
// no way to know if room is public until unmarshal -_-;
|
||||
@ -243,7 +244,7 @@ func listPublicRooms() []*models.Room {
|
||||
continue
|
||||
}
|
||||
log.Debug("consider room for list", "room", room, "key", key)
|
||||
if room.IsPublic {
|
||||
if room.IsPublic || allRooms {
|
||||
publicRooms = append(publicRooms, room)
|
||||
}
|
||||
}
|
||||
@ -251,6 +252,24 @@ func listPublicRooms() []*models.Room {
|
||||
return publicRooms
|
||||
}
|
||||
|
||||
// get bots
|
||||
func listBots() map[string]map[string]string {
|
||||
cacheMap := memcache.GetAll()
|
||||
resp := make(map[string]map[string]string)
|
||||
// no way to know if room is public until unmarshal -_-;
|
||||
for key, value := range cacheMap {
|
||||
if strings.HasPrefix(key, models.CacheBotPredix) {
|
||||
botMap := make(map[string]string)
|
||||
if err := json.Unmarshal(value, &botMap); err != nil {
|
||||
log.Warn("failed to unmarshal bot", "error", err)
|
||||
continue
|
||||
}
|
||||
resp[botMap["bot_name"]] = botMap
|
||||
}
|
||||
}
|
||||
return resp
|
||||
}
|
||||
|
||||
func notify(event, msg string) {
|
||||
Notifier.Notifier <- broker.NotificationEvent{
|
||||
EventName: event,
|
||||
@ -271,3 +290,22 @@ func loadCards(room *models.Room) {
|
||||
room.WCMap[card.Word] = card.Color
|
||||
}
|
||||
}
|
||||
|
||||
func recoverBots() {
|
||||
bots := listBots()
|
||||
for botName, botMap := range bots {
|
||||
if err := recoverBot(botMap); err != nil {
|
||||
log.Warn("failed to recover bot", "botName", botName, "error", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func recoverBot(bm map[string]string) error {
|
||||
// TODO: check if room still exists
|
||||
log.Debug("recovering bot", "bot", bm)
|
||||
_, err := llmapi.NewBot(bm["role"], bm["team"], bm["bot_name"], bm["room_id"], cfg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -107,7 +107,7 @@ func HandleFrontLogin(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
} else {
|
||||
log.Debug("no room_id in login")
|
||||
fi.List = listPublicRooms()
|
||||
fi.List = listRooms(false)
|
||||
// save state to cache
|
||||
if err := saveState(cleanName, userstate); err != nil {
|
||||
// if err := saveFullInfo(fi); err != nil {
|
||||
|
@ -28,6 +28,9 @@ func init() {
|
||||
cfg = config.LoadConfigOrDefault("")
|
||||
Notifier = broker.Notifier
|
||||
cache.MemCache.StartBackupRoutine(15 * time.Second) // Reduced backup interval
|
||||
// bot loader
|
||||
// check the rooms if it has bot_{digits} in them, create bots if have
|
||||
recoverBots()
|
||||
}
|
||||
|
||||
func HandlePing(w http.ResponseWriter, r *http.Request) {
|
||||
@ -51,7 +54,7 @@ func HandleHome(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
if fi != nil && fi.Room == nil {
|
||||
fi.List = listPublicRooms()
|
||||
fi.List = listRooms(false)
|
||||
}
|
||||
if err := tmpl.ExecuteTemplate(w, "base", fi); err != nil {
|
||||
log.Error("failed to exec templ;", "error", err, "templ", "base")
|
||||
@ -92,7 +95,7 @@ func HandleExit(w http.ResponseWriter, r *http.Request) {
|
||||
abortWithError(w, err.Error())
|
||||
return
|
||||
}
|
||||
fi.List = listPublicRooms()
|
||||
fi.List = listRooms(false)
|
||||
if err := tmpl.ExecuteTemplate(w, "base", fi); err != nil {
|
||||
log.Error("failed to exec templ;", "error", err, "templ", "base")
|
||||
}
|
||||
|
Reference in New Issue
Block a user