Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/false positive app err logs #1268

Merged
merged 17 commits into from
Jun 29, 2022
19 changes: 15 additions & 4 deletions cmd/apps/skychat/skychat.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"net"
"net/http"
"os"
"os/signal"
"runtime"
"sync"
"time"
Expand All @@ -24,7 +25,7 @@ import (
"github.com/skycoin/skywire-utilities/pkg/netutil"
"github.com/skycoin/skywire/pkg/app"
"github.com/skycoin/skywire/pkg/app/appnet"
"github.com/skycoin/skywire/pkg/app/launcher"
"github.com/skycoin/skywire/pkg/app/appserver"
"github.com/skycoin/skywire/pkg/routing"
"github.com/skycoin/skywire/pkg/skyenv"
)
Expand Down Expand Up @@ -83,15 +84,25 @@ func main() {
http.HandleFunc("/sse", sseHandler)

fmt.Println("Serving HTTP on", *addr)
if runtime.GOOS != "windows" {
termCh := make(chan os.Signal, 1)
signal.Notify(termCh, os.Interrupt)

setAppStatus(appCl, launcher.AppDetailedStatusRunning)
go func() {
<-termCh
setAppStatus(appCl, appserver.AppDetailedStatusStopped)
os.Exit(1)
}()
}
setAppStatus(appCl, appserver.AppDetailedStatusRunning)

err := http.ListenAndServe(*addr, nil)
if err != nil {
print(err)
setAppError(appCl, err)
os.Exit(1)
}

}

func listenLoop() {
Expand All @@ -106,7 +117,7 @@ func listenLoop() {
fmt.Println("Accepting skychat conn...")
conn, err := l.Accept()
if err != nil {
print(fmt.Sprintf("Failed to accept conn: %v", err))
print(fmt.Sprintf("Failed to accept conn: %v\n", err))
return
}
fmt.Println("Accepted skychat conn")
Expand Down Expand Up @@ -253,7 +264,7 @@ func handleIPCSignal(client *ipc.Client) {
os.Exit(0)
}

func setAppStatus(appCl *app.Client, status launcher.AppDetailedStatus) {
func setAppStatus(appCl *app.Client, status appserver.AppDetailedStatus) {
if err := appCl.SetDetailedStatus(string(status)); err != nil {
print(fmt.Sprintf("Failed to set status %v: %v\n", status, err))
}
Expand Down
16 changes: 8 additions & 8 deletions cmd/apps/skysocks-client/skysocks-client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"github.com/skycoin/skywire/internal/skysocks"
"github.com/skycoin/skywire/pkg/app"
"github.com/skycoin/skywire/pkg/app/appnet"
"github.com/skycoin/skywire/pkg/app/launcher"
"github.com/skycoin/skywire/pkg/app/appserver"
"github.com/skycoin/skywire/pkg/routing"
"github.com/skycoin/skywire/pkg/skyenv"
)
Expand Down Expand Up @@ -66,9 +66,9 @@ func main() {

if *serverPK == "" {
err := errors.New("Empty server PubKey. Exiting")
print(err)
print(fmt.Sprintf("%v\n", err))
setAppErr(appCl, err)
return
os.Exit(1)
}

pk := cipher.PubKey{}
Expand All @@ -77,7 +77,7 @@ func main() {
setAppErr(appCl, err)
os.Exit(1)
}

defer setAppStatus(appCl, appserver.AppDetailedStatusStopped)
for {
conn, err := dialServer(ctx, appCl, pk)
if err != nil {
Expand Down Expand Up @@ -107,18 +107,18 @@ func main() {
}

fmt.Println("Reconnecting to skysocks server")
setAppStatus(appCl, launcher.AppDetailedStatusReconnecting)
setAppStatus(appCl, appserver.AppDetailedStatusReconnecting)
}
}

func setAppErr(appCl *app.Client, err error) {
if appErr := appCl.SetError(err.Error()); appErr != nil {
fmt.Printf("Failed to set error %v: %v\n", err, appErr)
print(fmt.Sprintf("Failed to set error %v: %v\n", err, appErr))
}
}

func setAppStatus(appCl *app.Client, status launcher.AppDetailedStatus) {
func setAppStatus(appCl *app.Client, status appserver.AppDetailedStatus) {
if err := appCl.SetDetailedStatus(string(status)); err != nil {
fmt.Printf("Failed to set status %v: %v\n", status, err)
print(fmt.Sprintf("Failed to set status %v: %v\n", status, err))
}
}
33 changes: 25 additions & 8 deletions cmd/apps/skysocks/skysocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package main
import (
"flag"
"fmt"
"log"
"os"
"os/signal"
"runtime"
Expand All @@ -17,6 +16,7 @@ import (
"github.com/skycoin/skywire/internal/skysocks"
"github.com/skycoin/skywire/pkg/app"
"github.com/skycoin/skywire/pkg/app/appnet"
"github.com/skycoin/skywire/pkg/app/appserver"
"github.com/skycoin/skywire/pkg/routing"
"github.com/skycoin/skywire/pkg/skyenv"
)
Expand All @@ -31,28 +31,33 @@ func main() {
defer appCl.Close()

if _, err := buildinfo.Get().WriteTo(os.Stdout); err != nil {
fmt.Printf("Failed to output build info: %v", err)
print(fmt.Sprintf("Failed to output build info: %v", err))
}

var passcode = flag.String("passcode", "", "Authorize user against this passcode")
flag.Parse()

srv, err := skysocks.NewServer(*passcode, appCl)
if err != nil {
log.Fatal("Failed to create a new server: ", err)
setAppError(appCl, err)
print(fmt.Sprintf("Failed to create a new server: %v\n", err))
os.Exit(1)
}

l, err := appCl.Listen(netType, port)
if err != nil {
log.Fatalf("Error listening network %v on port %d: %v\n", netType, port, err)
setAppError(appCl, err)
print(fmt.Sprintf("Error listening network %v on port %d: %v\n", netType, port, err))
os.Exit(1)
}

fmt.Println("Starting serving proxy server")

if runtime.GOOS == "windows" {
ipcClient, err := ipc.StartClient(skyenv.VPNClientName, nil)
if err != nil {
fmt.Printf("Error creating ipc server for VPN client: %v\n", err)
setAppError(appCl, err)
print(fmt.Sprintf("Error creating ipc server for VPN client: %v\n", err))
os.Exit(1)
}
go srv.ListenIPC(ipcClient)
Expand All @@ -64,15 +69,27 @@ func main() {
<-termCh

if err := srv.Close(); err != nil {
fmt.Println(err)
print(fmt.Sprintf("%v\n", err))
os.Exit(1)
}
}()

}
defer setAppStatus(appCl, appserver.AppDetailedStatusStopped)

if err := srv.Serve(l); err != nil {
fmt.Println(err)
print(fmt.Sprintf("%v\n", err))
os.Exit(1)
}
}

func setAppStatus(appCl *app.Client, status appserver.AppDetailedStatus) {
if err := appCl.SetDetailedStatus(string(status)); err != nil {
print(fmt.Sprintf("Failed to set status %v: %v\n", status, err))
}
}

func setAppError(appCl *app.Client, appErr error) {
if err := appCl.SetError(appErr.Error()); err != nil {
print(fmt.Sprintf("Failed to set error %v: %v\n", appErr, err))
}
}
15 changes: 12 additions & 3 deletions cmd/apps/vpn-client/vpn-client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/skycoin/skywire/internal/vpn"
"github.com/skycoin/skywire/pkg/app"
"github.com/skycoin/skywire/pkg/app/appevent"
"github.com/skycoin/skywire/pkg/app/appserver"
"github.com/skycoin/skywire/pkg/skyenv"
)

Expand Down Expand Up @@ -74,7 +75,7 @@ func main() {
// TODO(darkrengarius): fix args passage for Windows
//*serverPKStr = "03e9019b3caa021dbee1c23e6295c6034ab4623aec50802fcfdd19764568e2958d"
err := errors.New("VPN server pub key is missing")
print(err)
print(fmt.Sprintf("%v\n", err))
setAppErr(appCl, err)
os.Exit(1)
}
Expand Down Expand Up @@ -170,13 +171,21 @@ func main() {
go vpnClient.ListenIPC(ipcClient)
}

defer setAppStatus(appCl, appserver.AppDetailedStatusStopped)

if err := vpnClient.Serve(); err != nil {
print(fmt.Sprintf("Failed to serve VPN: %v", err))
print(fmt.Sprintf("Failed to serve VPN: %v\n", err))
}
}

func setAppErr(appCl *app.Client, err error) {
if appErr := appCl.SetError(err.Error()); appErr != nil {
fmt.Printf("Failed to set error %v: %v\n", err, appErr)
print(fmt.Sprintf("Failed to set error %v: %v\n", err, appErr))
}
}

func setAppStatus(appCl *app.Client, status appserver.AppDetailedStatus) {
if err := appCl.SetDetailedStatus(string(status)); err != nil {
print(fmt.Sprintf("Failed to set status %v: %v\n", status, err))
}
}
23 changes: 16 additions & 7 deletions cmd/apps/vpn-server/vpn-server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/skycoin/skywire/internal/vpn"
"github.com/skycoin/skywire/pkg/app"
"github.com/skycoin/skywire/pkg/app/appnet"
"github.com/skycoin/skywire/pkg/app/appserver"
"github.com/skycoin/skywire/pkg/routing"
"github.com/skycoin/skywire/pkg/skyenv"
)
Expand Down Expand Up @@ -52,7 +53,7 @@ func main() {
localPK := cipher.PubKey{}
if *localPKStr != "" {
if err := localPK.UnmarshalText([]byte(*localPKStr)); err != nil {
print(fmt.Sprintf("Invalid local PK: %v", err))
print(fmt.Sprintf("Invalid local PK: %v\n", err))
setAppErr(appCl, err)
os.Exit(1)
}
Expand All @@ -61,7 +62,7 @@ func main() {
localSK := cipher.SecKey{}
if *localSKStr != "" {
if err := localSK.UnmarshalText([]byte(*localSKStr)); err != nil {
print(fmt.Sprintf("Invalid local SK: %v", err))
print(fmt.Sprintf("Invalid local SK: %v\n", err))
setAppErr(appCl, err)
os.Exit(1)
}
Expand All @@ -76,7 +77,7 @@ func main() {

l, err := appCl.Listen(netType, vpnPort)
if err != nil {
print(fmt.Sprintf("Error listening network %v on port %d: %v", netType, vpnPort, err))
print(fmt.Sprintf("Error listening network %v on port %d: %v\n", netType, vpnPort, err))
setAppErr(appCl, err)
os.Exit(1)
}
Expand All @@ -90,13 +91,13 @@ func main() {
}
srv, err := vpn.NewServer(srvCfg, appCl)
if err != nil {
print(fmt.Sprintf("Error creating VPN server: %v", err))
print(fmt.Sprintf("Error creating VPN server: %v\n", err))
setAppErr(appCl, err)
os.Exit(1)
}
defer func() {
if err := srv.Close(); err != nil {
print(fmt.Sprintf("Error closing server: %v", err))
print(fmt.Sprintf("Error closing server: %v\n", err))
}
}()

Expand All @@ -109,15 +110,23 @@ func main() {
close(errCh)
}()

defer setAppStatus(appCl, appserver.AppDetailedStatusStopped)

select {
case <-osSigs:
case err := <-errCh:
print(fmt.Sprintf("Error serving: %v", err))
print(fmt.Sprintf("Error serving: %v\n", err))
}
}

func setAppErr(appCl *app.Client, err error) {
if appErr := appCl.SetError(err.Error()); appErr != nil {
fmt.Printf("Failed to set error %v: %v\n", err, appErr)
print(fmt.Sprintf("Failed to set error %v: %v\n", err, appErr))
}
}

func setAppStatus(appCl *app.Client, status appserver.AppDetailedStatus) {
if err := appCl.SetDetailedStatus(string(status)); err != nil {
print(fmt.Sprintf("Failed to set status %v: %v\n", status, err))
}
}
4 changes: 2 additions & 2 deletions cmd/skywire-cli/commands/config/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/skycoin/skywire-utilities/pkg/logging"
"github.com/skycoin/skywire-utilities/pkg/netutil"
utilenv "github.com/skycoin/skywire-utilities/pkg/skyenv"
"github.com/skycoin/skywire/pkg/app/launcher"
"github.com/skycoin/skywire/pkg/app/appserver"
"github.com/skycoin/skywire/pkg/skyenv"
"github.com/skycoin/skywire/pkg/visor/visorconfig"
)
Expand Down Expand Up @@ -353,7 +353,7 @@ var genConfigCmd = &cobra.Command{
for _, app := range apps {
appsSlice[app] = true
}
var newConfLauncherApps []launcher.AppConfig
var newConfLauncherApps []appserver.AppConfig
for _, app := range conf.Launcher.Apps {
if _, ok := appsSlice[app.Name]; !ok {
newConfLauncherApps = append(newConfLauncherApps, app)
Expand Down
6 changes: 3 additions & 3 deletions cmd/skywire-cli/commands/visor/vapps/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/spf13/cobra"

"github.com/skycoin/skywire/cmd/skywire-cli/internal"
"github.com/skycoin/skywire/pkg/app/launcher"
"github.com/skycoin/skywire/pkg/app/appserver"
)

func init() {
Expand All @@ -37,10 +37,10 @@ var lsAppsCmd = &cobra.Command{

for _, state := range states {
status := "stopped"
if state.Status == launcher.AppStatusRunning {
if state.Status == appserver.AppStatusRunning {
status = "running"
}
if state.Status == launcher.AppStatusErrored {
if state.Status == appserver.AppStatusErrored {
status = "errored"
}
_, err = fmt.Fprintf(w, "%s\t%s\t%t\t%s\n", state.Name, strconv.Itoa(int(state.Port)), state.AutoStart, status)
Expand Down
Loading