Adam Štrauch
fdcdca237f
All checks were successful
continuous-integration/drone/push Build is passing
Docker client upgrade, Miner detection Flags for apps
97 lines
1.7 KiB
Go
97 lines
1.7 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"log"
|
|
"time"
|
|
|
|
"github.com/nats-io/nats.go"
|
|
"github.com/rosti-cz/node-api/apps"
|
|
"github.com/rosti-cz/node-api/common"
|
|
docker "github.com/rosti-cz/node-api/containers"
|
|
)
|
|
|
|
func errorReplyFormater(m *nats.Msg, message string, err error) error {
|
|
reply := ReplyMessage{
|
|
Error: true,
|
|
Payload: message,
|
|
}
|
|
|
|
log.Println("ERROR:", err.Error())
|
|
|
|
data, err := json.Marshal(reply)
|
|
if err != nil {
|
|
log.Println("ERROR:", err.Error())
|
|
return err
|
|
}
|
|
|
|
err = m.Respond(data)
|
|
if err != nil {
|
|
log.Println("ERROR:", err.Error())
|
|
return err
|
|
}
|
|
|
|
return err
|
|
}
|
|
|
|
func publish(appName string, state string, isErr bool) {
|
|
stateMessage := StateMessage{
|
|
AppName: appName,
|
|
Error: isErr,
|
|
Message: state,
|
|
}
|
|
|
|
data, err := stateMessage.JSON()
|
|
if err != nil {
|
|
log.Println("ERROR: publish:", err.Error())
|
|
}
|
|
|
|
subjectEvents := fmt.Sprintf("admin.apps.%s.states", config.NATSAlias)
|
|
err = nc.Publish(subjectEvents, data)
|
|
if err != nil {
|
|
log.Println("ERROR: publish:", err.Error())
|
|
}
|
|
}
|
|
|
|
// waitForApp waits until app is ready or timeout is reached.
|
|
// It's used in some async calls that need at least part of the
|
|
// environment prepared.
|
|
func waitForApp(appName string) error {
|
|
processor := apps.AppsProcessor{
|
|
DB: common.GetDBConnection(),
|
|
}
|
|
|
|
sleepFor := 5 * time.Second
|
|
loops := 6
|
|
|
|
for i := 0; i < loops; i++ {
|
|
err := updateState(appName)
|
|
if err != nil {
|
|
time.Sleep(sleepFor)
|
|
continue
|
|
}
|
|
|
|
app, err := processor.Get(appName)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
container := docker.Container{
|
|
App: app,
|
|
}
|
|
|
|
status, err := container.Status()
|
|
|
|
if status == "running" {
|
|
return nil
|
|
}
|
|
|
|
time.Sleep(sleepFor)
|
|
continue
|
|
}
|
|
|
|
return errors.New("timeout reached")
|
|
}
|