2024-06-12 02:42:10 +02:00
|
|
|
// Copyright 2024, Command Line Inc.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"runtime"
|
|
|
|
"strconv"
|
2024-06-13 03:17:56 +02:00
|
|
|
"sync"
|
2024-06-12 02:42:10 +02:00
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/wavetermdev/thenextwave/pkg/filestore"
|
|
|
|
"github.com/wavetermdev/thenextwave/pkg/service"
|
|
|
|
"github.com/wavetermdev/thenextwave/pkg/wavebase"
|
|
|
|
"github.com/wavetermdev/thenextwave/pkg/web"
|
|
|
|
"github.com/wavetermdev/thenextwave/pkg/wstore"
|
|
|
|
)
|
|
|
|
|
|
|
|
const ReadySignalPidVarName = "WAVETERM_READY_SIGNAL_PID"
|
|
|
|
|
2024-06-13 03:17:56 +02:00
|
|
|
var shutdownOnce sync.Once
|
|
|
|
|
2024-06-12 02:42:10 +02:00
|
|
|
func doShutdown(reason string) {
|
2024-06-13 03:17:56 +02:00
|
|
|
shutdownOnce.Do(func() {
|
|
|
|
log.Printf("shutting down: %s\n", reason)
|
|
|
|
ctx, cancelFn := context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
|
defer cancelFn()
|
|
|
|
// TODO deal with flush in progress
|
|
|
|
filestore.WFS.FlushCache(ctx)
|
|
|
|
time.Sleep(200 * time.Millisecond)
|
|
|
|
os.Exit(0)
|
|
|
|
})
|
2024-06-12 02:42:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func installShutdownSignalHandlers() {
|
|
|
|
sigCh := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sigCh, syscall.SIGHUP, syscall.SIGTERM, syscall.SIGINT)
|
|
|
|
go func() {
|
|
|
|
for sig := range sigCh {
|
|
|
|
doShutdown(fmt.Sprintf("got signal %v", sig))
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2024-06-13 03:17:56 +02:00
|
|
|
// watch stdin, kill server if stdin is closed
|
|
|
|
func stdinReadWatch() {
|
|
|
|
buf := make([]byte, 1024)
|
|
|
|
for {
|
|
|
|
_, err := os.Stdin.Read(buf)
|
|
|
|
if err != nil {
|
|
|
|
doShutdown(fmt.Sprintf("stdin closed/error (%v)", err))
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-12 02:42:10 +02:00
|
|
|
func main() {
|
2024-06-13 04:33:44 +02:00
|
|
|
log.SetFlags(log.LstdFlags | log.Lmicroseconds)
|
|
|
|
log.SetPrefix("[wavesrv] ")
|
|
|
|
|
2024-06-12 02:42:10 +02:00
|
|
|
err := service.ValidateServiceMap()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error validating service map: %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = wavebase.EnsureWaveHomeDir()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error ensuring wave home dir: %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
waveLock, err := wavebase.AcquireWaveLock()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error acquiring wave lock (another instance of Wave is likely running): %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("wave home dir: %s\n", wavebase.GetWaveHomeDir())
|
|
|
|
err = filestore.InitFilestore()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error initializing filestore: %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = wstore.InitWStore()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error initializing wstore: %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = wstore.EnsureInitialData()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error ensuring initial data: %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
installShutdownSignalHandlers()
|
2024-06-13 03:17:56 +02:00
|
|
|
go stdinReadWatch()
|
2024-06-12 02:42:10 +02:00
|
|
|
|
|
|
|
go web.RunWebSocketServer()
|
|
|
|
go func() {
|
|
|
|
time.Sleep(30 * time.Millisecond)
|
|
|
|
pidStr := os.Getenv(ReadySignalPidVarName)
|
|
|
|
if pidStr != "" {
|
|
|
|
pid, err := strconv.Atoi(pidStr)
|
|
|
|
if err == nil {
|
|
|
|
syscall.Kill(pid, syscall.SIGUSR1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
web.RunWebServer() // blocking
|
|
|
|
runtime.KeepAlive(waveLock)
|
|
|
|
}
|