Fast get for get without status
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone Build is passing

This commit is contained in:
Adam Štrauch 2022-05-27 18:24:39 +02:00
parent 69e147ccf9
commit e8d952cac0
Signed by: cx
GPG Key ID: 018304FFA8988F8D
5 changed files with 92 additions and 51 deletions

View File

@ -1,8 +1,8 @@
.PHONY: test .PHONY: test
test: test:
go test -v apps/*.go go test -race -v apps/*.go
go test -v apps/drivers/*.go go test -race -v apps/drivers/*.go
go test -v detector/*.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 # env DOCKER_SOCKET="unix:///var/run/docker.sock" go test -v containers/*.go # Doesn't work in Drone right now
build: build:

View File

@ -132,62 +132,66 @@ func (p *Processor) List(noUpdate bool) (apps.Apps, error) {
} }
// Get returns one app // Get returns one app
func (p *Processor) Get() (apps.App, error) { func (p *Processor) Get(noUpdate bool) (apps.App, error) {
app := apps.App{} app := apps.App{}
statsProcessor := StatsProcessor{ if !noUpdate {
DB: p.DB, statsProcessor := StatsProcessor{
DockerSock: p.DockerSock, DB: p.DB,
BindIPHTTP: p.BindIPHTTP, DockerSock: p.DockerSock,
BindIPSSH: p.BindIPSSH, BindIPHTTP: p.BindIPHTTP,
AppsPath: p.AppsPath, BindIPSSH: p.BindIPSSH,
} AppsPath: p.AppsPath,
}
err := statsProcessor.UpdateState(p.AppName) err := statsProcessor.UpdateState(p.AppName)
if err != nil { if err != nil {
return app, err return app, err
}
} }
processor := p.getAppProcessor() processor := p.getAppProcessor()
app, err = processor.Get(p.AppName) app, err := processor.Get(p.AppName)
if err != nil { if err != nil {
return app, err return app, err
} }
// Gather runtime info about the container // Gather runtime info about the container
container := docker.Container{ if !noUpdate {
App: &app, container := docker.Container{
DockerSock: p.DockerSock, App: &app,
BindIPHTTP: p.BindIPHTTP, DockerSock: p.DockerSock,
BindIPSSH: p.BindIPSSH, BindIPHTTP: p.BindIPHTTP,
AppsPath: p.AppsPath, 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
} }
processList, err := container.GetSystemProcesses() status, err := container.Status()
if err != nil { if err != nil {
return app, err 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) processList, err := container.GetSystemProcesses()
if err != nil { if err != nil {
return app, err 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 return app, nil

View File

@ -84,7 +84,7 @@ func TestProcessorGet(t *testing.T) {
err := processor.Create(testAppTemplate) err := processor.Create(testAppTemplate)
assert.Nil(t, err) assert.Nil(t, err)
app, err := processor.Get() app, err := processor.Get(false)
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, "running", app.State) assert.Equal(t, "running", app.State)
@ -96,7 +96,7 @@ func TestProcessorRegister(t *testing.T) {
err := processor.Register(testAppTemplate) err := processor.Register(testAppTemplate)
assert.Nil(t, err) assert.Nil(t, err)
app, err := processor.Get() app, err := processor.Get(false)
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, "no-container", app.State) assert.Equal(t, "no-container", app.State)
@ -108,7 +108,7 @@ func TestProcessorUpdate(t *testing.T) {
err := processor.Create(testAppTemplate) err := processor.Create(testAppTemplate)
assert.Nil(t, err) assert.Nil(t, err)
app, err := processor.Get() app, err := processor.Get(false)
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, "running", app.State) assert.Equal(t, "running", app.State)
@ -119,7 +119,7 @@ func TestProcessorUpdate(t *testing.T) {
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, "running", app.State) assert.Equal(t, "running", app.State)
app, err = processor.Get() app, err = processor.Get(false)
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, 1024, app.Memory) assert.Equal(t, 1024, app.Memory)
@ -141,7 +141,7 @@ func TestProcessorStop(t *testing.T) {
err = processor.Stop() err = processor.Stop()
assert.Nil(t, err) assert.Nil(t, err)
app, err := processor.Get() app, err := processor.Get(false)
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, "exited", app.State) assert.Equal(t, "exited", app.State)
@ -156,14 +156,14 @@ func TestProcessorStart(t *testing.T) {
err = processor.Stop() err = processor.Stop()
assert.Nil(t, err) assert.Nil(t, err)
app, err := processor.Get() app, err := processor.Get(false)
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, "exited", app.State) assert.Equal(t, "exited", app.State)
err = processor.Start() err = processor.Start()
assert.Nil(t, err) assert.Nil(t, err)
app, err = processor.Get() app, err = processor.Get(false)
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, "running", app.State) assert.Equal(t, "running", app.State)

View File

@ -49,7 +49,7 @@ func getAppHandler(c echo.Context) error {
BindIPSSH: config.AppsBindIPSSH, BindIPSSH: config.AppsBindIPSSH,
AppsPath: config.AppsPath, AppsPath: config.AppsPath,
} }
app, err := processor.Get() app, err := processor.Get(false)
if err != nil { if err != nil {
return c.JSONPretty(http.StatusInternalServerError, Message{Message: err.Error()}, JSONIndent) return c.JSONPretty(http.StatusInternalServerError, Message{Message: err.Error()}, JSONIndent)
} }

View File

@ -42,6 +42,7 @@ func _messageHandler(m *nats.Msg) error {
eventHandlerMap := map[string](func(m *nats.Msg, message *RequestMessage) error){ eventHandlerMap := map[string](func(m *nats.Msg, message *RequestMessage) error){
"list": listEventHandler, "list": listEventHandler,
"get": getEventHandler, "get": getEventHandler,
"fast_get": fastGetEventHandler, // same as get but without status update
"create": createEventHandler, "create": createEventHandler,
"register": registerEventHandler, "register": registerEventHandler,
"update": updateEventHandler, "update": updateEventHandler,
@ -134,7 +135,43 @@ func getEventHandler(m *nats.Msg, message *RequestMessage) error {
AppsPath: config.AppsPath, 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 { if err != nil {
log.Printf("backend error: %v\n", err) log.Printf("backend error: %v\n", err)
return errorReplyFormater(m, "backend error", err) return errorReplyFormater(m, "backend error", err)
@ -215,7 +252,7 @@ func registerEventHandler(m *nats.Msg, message *RequestMessage) error {
BindIPSSH: config.AppsBindIPSSH, BindIPSSH: config.AppsBindIPSSH,
AppsPath: config.AppsPath, AppsPath: config.AppsPath,
} }
err = processor.Create(appTemplate) err = processor.Register(appTemplate)
if err != nil && strings.Contains(err.Error(), "validation error") { if err != nil && strings.Contains(err.Error(), "validation error") {
publish(message.AppName, "validation error", true) publish(message.AppName, "validation error", true)
return err return err