Skip to content

Commit

Permalink
(ociswrapper): separate ociswrapper logs from ocis logs (#6776)
Browse files Browse the repository at this point in the history
  • Loading branch information
saw-jan committed Jul 13, 2023
1 parent 2e29a5d commit a0a088b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 22 deletions.
11 changes: 11 additions & 0 deletions tests/ociswrapper/log/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package log

import "log"

func Println(message string) {
log.Println("[ociswrapper]", message)
}

func Panic(err error) {
log.Panic("[ociswrapper] ", err.Error())
}
41 changes: 21 additions & 20 deletions tests/ociswrapper/ocis/ocis.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"bufio"
"crypto/tls"
"fmt"
"log"
"net/http"
"os"
"os/exec"
"strconv"
"time"

"ociswrapper/common"
"ociswrapper/log"
"ociswrapper/ocis/config"
)

Expand All @@ -33,45 +33,48 @@ func Start(envMap map[string]any) {
}
cmd.Env = append(cmd.Env, environments...)

stderr, err := cmd.StderrPipe()
logs, err := cmd.StderrPipe()
if err != nil {
fmt.Println(err)
log.Panic(err)
}
stdout, err := cmd.StdoutPipe()
output, err := cmd.StdoutPipe()
if err != nil {
fmt.Println(err)
log.Panic(err)
}

err = cmd.Start()
if err != nil {
fmt.Println(err)
log.Panic(err)
}

stderrScanner := bufio.NewScanner(stderr)
for stderrScanner.Scan() {
m := stderrScanner.Text()
// Read and print the logs when the 'ocis server' command is running
logScanner := bufio.NewScanner(logs)
for logScanner.Scan() {
m := logScanner.Text()
fmt.Println(m)
}
stdoutScanner := bufio.NewScanner(stdout)
for stdoutScanner.Scan() {
m := stdoutScanner.Text()
// Read output when the 'ocis server' command gets exited
outputScanner := bufio.NewScanner(output)
for outputScanner.Scan() {
m := outputScanner.Text()
fmt.Println(m)
retryCount++

maxRetry, _ := strconv.Atoi(config.Get("retry"))
if retryCount <= maxRetry {
fmt.Println(fmt.Sprintf("Retry starting oCIS server... (retry %v)", retryCount))
log.Println(fmt.Sprintf("Retry starting oCIS server... (retry %v)", retryCount))
// Stop and start again
Stop()
Start(envMap)
}
}
cmd.Wait()
}

func Stop() {
err := cmd.Process.Kill()
if err != nil {
log.Panic("Cannot kill oCIS server")
log.Panic(err)
}
cmd.Wait()
}
Expand All @@ -93,14 +96,12 @@ func WaitForConnection() bool {
for {
select {
case <-timeout:
fmt.Println(fmt.Sprintf("%v seconds timeout waiting for oCIS server", int64(timeoutValue.Seconds())))
log.Println(fmt.Sprintf("%v seconds timeout waiting for oCIS server", int64(timeoutValue.Seconds())))
return false
default:
_, err := client.Get(config.Get("url"))
if err != nil {
fmt.Println("Waiting for oCIS server...")
} else {
fmt.Println(fmt.Sprintf("oCIS server is ready to accept requests"))
if err == nil {
log.Println("oCIS server is ready to accept requests")
return true
}
// 500 milliseconds poll interval
Expand All @@ -110,7 +111,7 @@ func WaitForConnection() bool {
}

func Restart(envMap map[string]any) bool {
log.Print("Restarting oCIS server...")
log.Println("Restarting oCIS server...")
Stop()

common.Wg.Add(1)
Expand Down
5 changes: 3 additions & 2 deletions tests/ociswrapper/wrapper/wrapper.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package wrapper

import (
"log"
"fmt"
"net/http"
"ociswrapper/common"
"ociswrapper/log"
"ociswrapper/ocis/config"
"ociswrapper/wrapper/handlers"
)
Expand All @@ -26,7 +27,7 @@ func Start(port string) {

httpServer.Handler = mux

log.Printf("Starting server on port %s...", port)
log.Println(fmt.Sprintf("Starting ociswrapper on port %s...", port))

err := httpServer.ListenAndServe()
if err != nil {
Expand Down

0 comments on commit a0a088b

Please sign in to comment.