Implemented worker command

This commit is contained in:
ErickSkrauch
2020-01-03 00:51:57 +03:00
parent 1e91aef0a6
commit 5a0c10c1a1
13 changed files with 367 additions and 53 deletions

View File

@@ -5,16 +5,16 @@ import (
"os"
"strings"
"github.com/elyby/chrly/bootstrap"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/elyby/chrly/version"
)
var RootCmd = &cobra.Command{
Use: "chrly",
Short: "Implementation of Minecraft skins system server",
Version: bootstrap.GetVersion(),
Version: version.Version(),
}
// Execute adds all child commands to the root command and sets flags appropriately.

View File

@@ -3,8 +3,6 @@ package cmd
import (
"fmt"
"log"
"net/url"
"time"
"github.com/mono83/slf/wd"
"github.com/spf13/cobra"
@@ -19,7 +17,7 @@ import (
var serveCmd = &cobra.Command{
Use: "serve",
Short: "Starts http handler for the skins system",
Short: "Starts HTTP handler for the skins system",
Run: func(cmd *cobra.Command, args []string) {
// TODO: this is a mess, need to organize this code somehow to make services initialization more compact
logger, err := bootstrap.CreateLogger(viper.GetString("statsd.addr"), viper.GetString("sentry.dsn"))
@@ -55,32 +53,17 @@ var serveCmd = &cobra.Command{
return
}
var uuidsProvider mojangtextures.UuidsProvider
preferredUuidsProvider := viper.GetString("mojang_textures.uuids_provider.driver")
if preferredUuidsProvider == "remote" {
remoteUrl, err := url.Parse(viper.GetString("mojang_textures.uuids_provider.url"))
if err != nil {
logger.Emergency("Unable to parse remote url :err", wd.ErrParam(err))
return
}
uuidsProvider = &mojangtextures.RemoteApiUuidsProvider{
Url: *remoteUrl,
Logger: logger,
}
} else {
uuidsProvider = &mojangtextures.BatchUuidsProvider{
IterationDelay: time.Duration(viper.GetInt("queue.loop_delay")) * time.Millisecond,
IterationSize: viper.GetInt("queue.batch_size"),
Logger: logger,
}
uuidsProvider, err := bootstrap.CreateMojangUUIDsProvider(logger)
if err != nil {
logger.Emergency("Unable to parse remote url :err", wd.ErrParam(err))
return
}
texturesStorage := mojangtextures.NewInMemoryTexturesStorage()
texturesStorage.Start()
mojangTexturesProvider := &mojangtextures.Provider{
Logger: logger,
UuidsProvider: uuidsProvider,
UUIDsProvider: uuidsProvider,
TexturesProvider: &mojangtextures.MojangApiTexturesProvider{
Logger: logger,
},
@@ -115,6 +98,4 @@ func init() {
viper.SetDefault("storage.redis.poll", 10)
viper.SetDefault("storage.filesystem.basePath", "data")
viper.SetDefault("storage.filesystem.capesDirName", "capes")
viper.SetDefault("queue.loop_delay", 2_500)
viper.SetDefault("queue.batch_size", 10)
}

View File

@@ -2,18 +2,20 @@ package cmd
import (
"fmt"
"runtime"
"github.com/spf13/cobra"
"github.com/elyby/chrly/bootstrap"
"runtime"
"github.com/elyby/chrly/version"
)
var versionCmd = &cobra.Command{
Use: "version",
Short: "Show the Chrly version information",
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("Version: %s\n", bootstrap.GetVersion())
fmt.Printf("Go version: %s\n", runtime.Version())
fmt.Printf("Version: %s\n", version.Version())
fmt.Printf("Commit: %s\n", version.Commit())
fmt.Printf("Go version: %s\n", runtime.Version())
fmt.Printf("OS/Arch: %s/%s\n", runtime.GOOS, runtime.GOARCH)
},
}

45
cmd/worker.go Normal file
View File

@@ -0,0 +1,45 @@
package cmd
import (
"fmt"
"log"
"github.com/mono83/slf/wd"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/elyby/chrly/bootstrap"
"github.com/elyby/chrly/http"
)
var workerCmd = &cobra.Command{
Use: "worker",
Short: "Starts HTTP handler for the Mojang usernames to UUIDs worker",
Run: func(cmd *cobra.Command, args []string) {
logger, err := bootstrap.CreateLogger(viper.GetString("statsd.addr"), viper.GetString("sentry.dsn"))
if err != nil {
log.Fatal(fmt.Printf("Cannot initialize logger: %v", err))
}
logger.Info("Logger successfully initialized")
uuidsProvider, err := bootstrap.CreateMojangUUIDsProvider(logger)
if err != nil {
logger.Emergency("Unable to parse remote url :err", wd.ErrParam(err))
return
}
cfg := &http.UUIDsWorker{
ListenSpec: fmt.Sprintf("%s:%d", viper.GetString("server.host"), viper.GetInt("server.port")),
UUIDsProvider: uuidsProvider,
Logger: logger,
}
if err := cfg.Run(); err != nil {
logger.Error(fmt.Sprintf("Error in main(): %v", err))
}
},
}
func init() {
RootCmd.AddCommand(workerCmd)
}