Simple implementation of the health checker for the worker

This commit is contained in:
ErickSkrauch 2020-04-10 03:07:53 +03:00
parent db19fe62f2
commit 677f48ff3f
5 changed files with 85 additions and 0 deletions

9
Gopkg.lock generated
View File

@ -39,6 +39,14 @@
revision = "346938d642f2ec3594ed81d874461961cd0faa76" revision = "346938d642f2ec3594ed81d874461961cd0faa76"
version = "v1.1.0" version = "v1.1.0"
[[projects]]
digest = "1:2e7c296138d042515eb2995fe58026eaef2c08f660a5f36584faecf34eea3cf0"
name = "github.com/etherlabsio/healthcheck"
packages = ["."]
pruneopts = ""
revision = "dd3d2fd8c3f620a32b7f3cd9b4f0d2f7d0875ab1"
version = "2.0.3"
[[projects]] [[projects]]
digest = "1:9f1e571696860f2b4f8a241b43ce91c6085e7aaed849ccca53f590a4dc7b95bd" digest = "1:9f1e571696860f2b4f8a241b43ce91c6085e7aaed849ccca53f590a4dc7b95bd"
name = "github.com/fsnotify/fsnotify" name = "github.com/fsnotify/fsnotify"
@ -311,6 +319,7 @@
"github.com/SermoDigital/jose/crypto", "github.com/SermoDigital/jose/crypto",
"github.com/SermoDigital/jose/jws", "github.com/SermoDigital/jose/jws",
"github.com/asaskevich/EventBus", "github.com/asaskevich/EventBus",
"github.com/etherlabsio/healthcheck",
"github.com/getsentry/raven-go", "github.com/getsentry/raven-go",
"github.com/gorilla/mux", "github.com/gorilla/mux",
"github.com/h2non/gock", "github.com/h2non/gock",

View File

@ -41,6 +41,10 @@ ignored = ["github.com/elyby/chrly"]
source = "https://github.com/erickskrauch/EventBus.git" source = "https://github.com/erickskrauch/EventBus.git"
branch = "publish_nil_values" branch = "publish_nil_values"
[[constraint]]
name = "github.com/etherlabsio/healthcheck"
version = "2.0.3"
# Testing dependencies # Testing dependencies
[[constraint]] [[constraint]]

View File

@ -5,6 +5,7 @@ import (
"log" "log"
"os" "os"
"github.com/etherlabsio/healthcheck"
"github.com/mono83/slf/wd" "github.com/mono83/slf/wd"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
@ -51,6 +52,12 @@ var workerCmd = &cobra.Command{
Emitter: dispatcher, Emitter: dispatcher,
UUIDsProvider: uuidsProvider, UUIDsProvider: uuidsProvider,
}).CreateHandler() }).CreateHandler()
handler.Handle("/healthcheck", healthcheck.Handler(
healthcheck.WithChecker(
"mojang-batch-uuids-provider-response",
eventsubscribers.MojangBatchUuidsProviderChecker(dispatcher),
),
)).Methods("GET")
finishChan := make(chan bool) finishChan := make(chan bool)
go func() { go func() {

View File

@ -0,0 +1,30 @@
package eventsubscribers
import (
"context"
"sync"
"github.com/etherlabsio/healthcheck"
"github.com/elyby/chrly/api/mojang"
)
func MojangBatchUuidsProviderChecker(dispatcher Subscriber) healthcheck.CheckerFunc {
var mutex sync.Mutex
var lastCallErr error // TODO: need to reset this value after some time
dispatcher.Subscribe(
"mojang_textures:batch_uuids_provider:result",
func(usernames []string, profiles []*mojang.ProfileInfo, err error) {
mutex.Lock()
lastCallErr = err
mutex.Unlock()
},
)
return func(ctx context.Context) error {
mutex.Lock()
defer mutex.Unlock()
return lastCallErr
}
}

View File

@ -0,0 +1,35 @@
package eventsubscribers
import (
"context"
"errors"
"testing"
"github.com/stretchr/testify/assert"
"github.com/elyby/chrly/api/mojang"
"github.com/elyby/chrly/bootstrap"
)
func TestMojangBatchUuidsProviderChecker(t *testing.T) {
t.Run("empty state", func(t *testing.T) {
dispatcher := bootstrap.CreateEventDispatcher()
checker := MojangBatchUuidsProviderChecker(dispatcher)
assert.Nil(t, checker(context.Background()))
})
t.Run("when no error occurred", func(t *testing.T) {
dispatcher := bootstrap.CreateEventDispatcher()
checker := MojangBatchUuidsProviderChecker(dispatcher)
dispatcher.Emit("mojang_textures:batch_uuids_provider:result", []string{"username"}, []*mojang.ProfileInfo{}, nil)
assert.Nil(t, checker(context.Background()))
})
t.Run("when error occurred", func(t *testing.T) {
dispatcher := bootstrap.CreateEventDispatcher()
checker := MojangBatchUuidsProviderChecker(dispatcher)
err := errors.New("some error occurred")
dispatcher.Emit("mojang_textures:batch_uuids_provider:result", []string{"username"}, nil, err)
assert.Equal(t, err, checker(context.Background()))
})
}