2020-04-10 05:37:53 +05:30
|
|
|
package eventsubscribers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
2020-04-10 17:49:00 +05:30
|
|
|
"time"
|
2020-04-10 05:37:53 +05:30
|
|
|
|
|
|
|
"github.com/etherlabsio/healthcheck"
|
|
|
|
|
|
|
|
"github.com/elyby/chrly/api/mojang"
|
|
|
|
)
|
|
|
|
|
2020-04-10 17:49:00 +05:30
|
|
|
func MojangBatchUuidsProviderChecker(dispatcher Subscriber, resetDuration time.Duration) healthcheck.CheckerFunc {
|
2020-04-10 05:37:53 +05:30
|
|
|
var mutex sync.Mutex
|
2020-04-10 17:49:00 +05:30
|
|
|
var lastCallErr error
|
|
|
|
var expireTimer *time.Timer
|
2020-04-10 05:37:53 +05:30
|
|
|
dispatcher.Subscribe(
|
|
|
|
"mojang_textures:batch_uuids_provider:result",
|
|
|
|
func(usernames []string, profiles []*mojang.ProfileInfo, err error) {
|
|
|
|
mutex.Lock()
|
2020-04-10 17:49:00 +05:30
|
|
|
defer mutex.Unlock()
|
|
|
|
|
2020-04-10 05:37:53 +05:30
|
|
|
lastCallErr = err
|
2020-04-10 17:49:00 +05:30
|
|
|
if expireTimer != nil {
|
|
|
|
expireTimer.Stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
expireTimer = time.AfterFunc(resetDuration, func() {
|
|
|
|
mutex.Lock()
|
|
|
|
lastCallErr = nil
|
|
|
|
mutex.Unlock()
|
|
|
|
})
|
2020-04-10 05:37:53 +05:30
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
return func(ctx context.Context) error {
|
|
|
|
mutex.Lock()
|
|
|
|
defer mutex.Unlock()
|
|
|
|
|
|
|
|
return lastCallErr
|
|
|
|
}
|
|
|
|
}
|