Skip to content

Commit

Permalink
seva-launcher: Replace all fatal logs
Browse files Browse the repository at this point in the history
Replace all fatal log exits with normal log and graceful exit.

Signed-off-by: StaticRocket <[email protected]>
  • Loading branch information
StaticRocket committed Nov 2, 2022
1 parent 6ffd76d commit 31595b1
Showing 1 changed file with 24 additions and 14 deletions.
38 changes: 24 additions & 14 deletions seva-launcher/seva-launcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,13 @@ func echo(w http.ResponseWriter, r *http.Request) {
func setup_working_directory() {
err := os.MkdirAll("/tmp/seva-launcher", os.ModePerm)
if err != nil {
log.Fatal(err)
log.Println(err)
exit(1)
}
err = os.Chdir("/tmp/seva-launcher")
if err != nil {
log.Fatal(err)
log.Println(err)
exit(1)
}
}

Expand Down Expand Up @@ -168,7 +170,8 @@ func docker_run(args ...string) []byte {
log.Printf("|\n%s\n", output)
if err != nil {
log.Println("Failed to start container!")
log.Fatal(err)
log.Println(err)
exit(1)
}
return output
}
Expand All @@ -184,7 +187,8 @@ func start_app() string {
cmd := exec.Command(docker_compose, "-p", "seva-launcher", "up", "-d")
output, err := cmd.CombinedOutput()
if err != nil {
log.Fatal("Failed to start selected app!")
log.Println("Failed to start selected app!")
exit(1)
}
output_s := strings.TrimSpace(string(output))
log.Printf("|\n%s\n", output_s)
Expand All @@ -209,7 +213,8 @@ func get_app() string {
}
content, err := os.ReadFile("metadata.json")
if err != nil {
log.Fatal(err)
log.Println(err)
exit(1)
}
return string(content)
}
Expand All @@ -225,7 +230,8 @@ func load_app(name string) string {
}
err := os.Remove(element)
if err != nil {
log.Fatal("Failed to remove old files")
log.Println("Failed to remove old files")
exit(1)
}
}
g := got.New()
Expand All @@ -234,7 +240,8 @@ func load_app(name string) string {
log.Println("Fetching " + element + " from: " + url)
err := g.Download(url, element)
if err != nil {
log.Fatal(err)
log.Println(err)
exit(1)
}
}
return string("0")
Expand All @@ -245,12 +252,14 @@ func is_running(name string) string {
cmd := exec.Command(docker_compose, "-p", "seva-launcher", "ps", "--format", "json")
output, err := cmd.Output()
if err != nil {
log.Fatal("Failed to check if app is running!")
log.Println("Failed to check if app is running!")
exit(1)
}
var containers Containers
err = json.Unmarshal([]byte(output), &containers)
if err != nil {
log.Fatal("Failed to parse JSON from docker-compose!")
log.Println("Failed to parse JSON from docker-compose!")
exit(1)
}
for _, element := range containers {
if element.Name == name {
Expand All @@ -260,7 +269,7 @@ func is_running(name string) string {
return string("0")
}

func exit() {
func exit(num int) {
log.Println("Stopping non-app containers")
for _, container_id := range container_id_list {
if len(container_id) > 0 {
Expand All @@ -271,15 +280,15 @@ func exit() {
}
}
}
os.Exit(num)
}

func setup_exit_handler() {
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
exit()
os.Exit(0)
exit(0)
}()
}

Expand All @@ -289,10 +298,11 @@ func handle_requests() {
log.Println("Listening for websocket messages at " + *addr + "/ws")
root_content, err := fs.Sub(content, "web")
if err != nil {
log.Fatal("No files to server for web interface!")
log.Println("No files to server for web interface!")
exit(1)
}
router.PathPrefix("/").Handler(http.FileServer(http.FS(root_content)))
log.Fatal(http.ListenAndServe(*addr, router))
log.Println(http.ListenAndServe(*addr, router))
}


Expand Down

0 comments on commit 31595b1

Please sign in to comment.