Fast get for get without status
continuous-integration/drone/push Build is passing Details
continuous-integration/drone Build is passing Details

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
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:

View File

@ -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

View File

@ -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)

View File

@ -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)
}

View File

@ -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