From e8d952cac08f652b812389c304370dd62c581391 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20=C5=A0trauch?= Date: Fri, 27 May 2022 18:24:39 +0200 Subject: [PATCH] Fast get for get without status --- Makefile | 6 ++-- glue/main.go | 80 +++++++++++++++++++++++++---------------------- glue/main_test.go | 14 ++++----- handlers.go | 2 +- handlers_nats.go | 41 ++++++++++++++++++++++-- 5 files changed, 92 insertions(+), 51 deletions(-) diff --git a/Makefile b/Makefile index f7dbe2b..729282b 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,8 @@ .PHONY: test test: - go test -v apps/*.go - go test -v apps/drivers/*.go - go test -v detector/*.go + go test -race -v apps/*.go + go test -race -v apps/drivers/*.go + go test -race -v detector/*.go # env DOCKER_SOCKET="unix:///var/run/docker.sock" go test -v containers/*.go # Doesn't work in Drone right now build: diff --git a/glue/main.go b/glue/main.go index 9025e6f..e850772 100644 --- a/glue/main.go +++ b/glue/main.go @@ -132,62 +132,66 @@ func (p *Processor) List(noUpdate bool) (apps.Apps, error) { } // Get returns one app -func (p *Processor) Get() (apps.App, error) { +func (p *Processor) Get(noUpdate bool) (apps.App, error) { app := apps.App{} - statsProcessor := StatsProcessor{ - DB: p.DB, - DockerSock: p.DockerSock, - BindIPHTTP: p.BindIPHTTP, - BindIPSSH: p.BindIPSSH, - AppsPath: p.AppsPath, - } + if !noUpdate { + statsProcessor := StatsProcessor{ + DB: p.DB, + DockerSock: p.DockerSock, + BindIPHTTP: p.BindIPHTTP, + BindIPSSH: p.BindIPSSH, + AppsPath: p.AppsPath, + } - err := statsProcessor.UpdateState(p.AppName) - if err != nil { - return app, err + err := statsProcessor.UpdateState(p.AppName) + if err != nil { + return app, err + } } processor := p.getAppProcessor() - app, err = processor.Get(p.AppName) + app, err := processor.Get(p.AppName) if err != nil { return app, err } // Gather runtime info about the container - container := docker.Container{ - App: &app, - DockerSock: p.DockerSock, - BindIPHTTP: p.BindIPHTTP, - BindIPSSH: p.BindIPSSH, - AppsPath: p.AppsPath, - } - - status, err := container.Status() - if err != nil { - return app, err - } - if status == "running" { - var err error - app.Techs, err = container.GetTechs() - if err != nil { - return app, err - } - app.PrimaryTech, err = container.GetPrimaryTech() - if err != nil { - return app, err + if !noUpdate { + container := docker.Container{ + App: &app, + DockerSock: p.DockerSock, + BindIPHTTP: p.BindIPHTTP, + BindIPSSH: p.BindIPSSH, + AppsPath: p.AppsPath, } - processList, err := container.GetSystemProcesses() + status, err := container.Status() if err != nil { return app, err } + if status == "running" { + var err error + app.Techs, err = container.GetTechs() + if err != nil { + return app, err + } + app.PrimaryTech, err = container.GetPrimaryTech() + if err != nil { + return app, err + } - flags, err := detector.Check(processList) - if err != nil { - return app, err + processList, err := container.GetSystemProcesses() + if err != nil { + return app, err + } + + flags, err := detector.Check(processList) + if err != nil { + return app, err + } + app.Flags = flags.String() } - app.Flags = flags.String() } return app, nil diff --git a/glue/main_test.go b/glue/main_test.go index df96c4d..235f7d9 100644 --- a/glue/main_test.go +++ b/glue/main_test.go @@ -84,7 +84,7 @@ func TestProcessorGet(t *testing.T) { err := processor.Create(testAppTemplate) assert.Nil(t, err) - app, err := processor.Get() + app, err := processor.Get(false) assert.Nil(t, err) assert.Equal(t, "running", app.State) @@ -96,7 +96,7 @@ func TestProcessorRegister(t *testing.T) { err := processor.Register(testAppTemplate) assert.Nil(t, err) - app, err := processor.Get() + app, err := processor.Get(false) assert.Nil(t, err) assert.Equal(t, "no-container", app.State) @@ -108,7 +108,7 @@ func TestProcessorUpdate(t *testing.T) { err := processor.Create(testAppTemplate) assert.Nil(t, err) - app, err := processor.Get() + app, err := processor.Get(false) assert.Nil(t, err) assert.Equal(t, "running", app.State) @@ -119,7 +119,7 @@ func TestProcessorUpdate(t *testing.T) { assert.Nil(t, err) assert.Equal(t, "running", app.State) - app, err = processor.Get() + app, err = processor.Get(false) assert.Nil(t, err) assert.Equal(t, 1024, app.Memory) @@ -141,7 +141,7 @@ func TestProcessorStop(t *testing.T) { err = processor.Stop() assert.Nil(t, err) - app, err := processor.Get() + app, err := processor.Get(false) assert.Nil(t, err) assert.Equal(t, "exited", app.State) @@ -156,14 +156,14 @@ func TestProcessorStart(t *testing.T) { err = processor.Stop() assert.Nil(t, err) - app, err := processor.Get() + app, err := processor.Get(false) assert.Nil(t, err) assert.Equal(t, "exited", app.State) err = processor.Start() assert.Nil(t, err) - app, err = processor.Get() + app, err = processor.Get(false) assert.Nil(t, err) assert.Equal(t, "running", app.State) diff --git a/handlers.go b/handlers.go index 7fa7c7d..04b83b4 100644 --- a/handlers.go +++ b/handlers.go @@ -49,7 +49,7 @@ func getAppHandler(c echo.Context) error { BindIPSSH: config.AppsBindIPSSH, AppsPath: config.AppsPath, } - app, err := processor.Get() + app, err := processor.Get(false) if err != nil { return c.JSONPretty(http.StatusInternalServerError, Message{Message: err.Error()}, JSONIndent) } diff --git a/handlers_nats.go b/handlers_nats.go index 321623c..2ff2e51 100644 --- a/handlers_nats.go +++ b/handlers_nats.go @@ -42,6 +42,7 @@ func _messageHandler(m *nats.Msg) error { eventHandlerMap := map[string](func(m *nats.Msg, message *RequestMessage) error){ "list": listEventHandler, "get": getEventHandler, + "fast_get": fastGetEventHandler, // same as get but without status update "create": createEventHandler, "register": registerEventHandler, "update": updateEventHandler, @@ -134,7 +135,43 @@ func getEventHandler(m *nats.Msg, message *RequestMessage) error { AppsPath: config.AppsPath, } - app, err := processor.Get() + app, err := processor.Get(false) + if err != nil { + log.Printf("backend error: %v\n", err) + return errorReplyFormater(m, "backend error", err) + } + + // Assembling reply message + reply := ReplyMessage{ + AppName: app.Name, + Payload: app, + } + + data, err := json.Marshal(reply) + if err != nil { + return errorReplyFormater(m, "reply formatter error", err) + } + + err = m.Respond(data) + if err != nil { + log.Println("ERROR: get app:", err.Error()) + } + + return err +} + +// Returns one app fast whicn mean with no immediate status update +func fastGetEventHandler(m *nats.Msg, message *RequestMessage) error { + processor := glue.Processor{ + AppName: message.AppName, + DB: common.GetDBConnection(), + DockerSock: config.DockerSocket, + BindIPHTTP: config.AppsBindIPHTTP, + BindIPSSH: config.AppsBindIPSSH, + AppsPath: config.AppsPath, + } + + app, err := processor.Get(true) if err != nil { log.Printf("backend error: %v\n", err) return errorReplyFormater(m, "backend error", err) @@ -215,7 +252,7 @@ func registerEventHandler(m *nats.Msg, message *RequestMessage) error { BindIPSSH: config.AppsBindIPSSH, AppsPath: config.AppsPath, } - err = processor.Create(appTemplate) + err = processor.Register(appTemplate) if err != nil && strings.Contains(err.Error(), "validation error") { publish(message.AppName, "validation error", true) return err