Skip to content

Commit

Permalink
working, but make test not passing
Browse files Browse the repository at this point in the history
  • Loading branch information
ivcosla committed Jun 11, 2019
1 parent 6ca95ae commit 14b725c
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 18 deletions.
26 changes: 19 additions & 7 deletions pkg/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,21 @@ import (
"context"
"errors"
"fmt"
"github.com/skycoin/skywire/pkg/util/pathutil"
"io"
"net"
"net/rpc"
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"
"sync"
"syscall"
"time"

"github.com/skycoin/skywire/pkg/util/pathutil"

"github.com/skycoin/skycoin/src/util/logging"

"github.com/skycoin/skywire/internal/noise"
Expand Down Expand Up @@ -98,6 +100,8 @@ type Node struct {
startedMu sync.RWMutex
startedApps map[string]*appBind

pidMu sync.Mutex

rpcListener net.Listener
rpcDialers []*noise.RPCClientDialer
}
Expand Down Expand Up @@ -155,7 +159,6 @@ func NewNode(config *Config) (*Node, error) {
RoutingTable: node.rt,
RouteFinder: routeFinder.NewHTTP(config.Routing.RouteFinder, time.Duration(config.Routing.RouteFinderTimeout)),
SetupNodes: config.Routing.SetupNodes,

}
r := router.New(rConfig)
node.router = r
Expand Down Expand Up @@ -251,7 +254,7 @@ func (node *Node) dir() string {
}

func (node *Node) pidFile() *os.File {
f, err := os.OpenFile(filepath.Join(node.dir(),"apps.pid"), os.O_RDWR|os.O_CREATE, 0755)
f, err := os.OpenFile(filepath.Join(node.dir(), "apps.pid"), os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
panic(err)
}
Expand All @@ -260,6 +263,8 @@ func (node *Node) pidFile() *os.File {
}

func (node *Node) closePreviousApps() {
node.logger.Info("killing previously ran apps if any...")

pids := node.pidFile()
defer pids.Close() // nocheck: err

Expand All @@ -285,13 +290,14 @@ func (node *Node) closePreviousApps() {
func (node *Node) stopUnhandledApp(name string, pid int) {
p, err := os.FindProcess(pid)
if err != nil {
node.logger.Infof("Previous app %s ran by this node with pid: %d not found", name, pid)
if runtime.GOOS != "windows" {
node.logger.Infof("Previous app %s ran by this node with pid: %d not found", name, pid)
}
return
}

err = p.Signal(syscall.SIGKILL)
if err != nil {
node.logger.Warnf("Found hanged app %s with pid %d previously ran by this node, but unable to kill it: %s", name, pid, err)
return
}

Expand Down Expand Up @@ -415,9 +421,12 @@ func (node *Node) SpawnApp(config *AppConfig, startCh chan<- struct{}) error {
node.startedMu.Lock()
bind.pid = pid
node.startedMu.Unlock()
appCh <- node.executer.Wait(cmd)

node.pidMu.Lock()
node.logger.Infof("storing app %s pid %d", config.App, pid)
node.persistPID(config.App, pid)
node.pidMu.Unlock()
appCh <- node.executer.Wait(cmd)
}()

srvCh := make(chan error)
Expand Down Expand Up @@ -452,7 +461,10 @@ func (node *Node) SpawnApp(config *AppConfig, startCh chan<- struct{}) error {

func (node *Node) persistPID(name string, pid int) {
pidF := node.pidFile()
pathutil.AtomicAppendToFile(pidF.Name(), []byte(fmt.Sprintf("%s %d\n", name, pid)))
pidFName := pidF.Name()
pidF.Close()

pathutil.AtomicAppendToFile(pidFName, []byte(fmt.Sprintf("%s %d\n", name, pid)))
}

// StopApp stops running App.
Expand Down
8 changes: 7 additions & 1 deletion pkg/node/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"errors"
"github.com/skycoin/skywire/pkg/util/pathutil"
"net"
"os"
"os/exec"
Expand Down Expand Up @@ -97,11 +98,16 @@ func TestNodeStartClose(t *testing.T) {
}

func TestNodeSpawnApp(t *testing.T) {
pk, _ := cipher.GenerateKeyPair()
r := new(mockRouter)
executer := &MockExecuter{}
defer os.RemoveAll("skychat")
apps := []AppConfig{{App: "skychat", Version: "1.0", AutoStart: false, Port: 10, Args: []string{"foo"}}}
node := &Node{router: r, executer: executer, appsConf: apps, startedApps: map[string]*appBind{}, logger: logging.MustGetLogger("test")}
node := &Node{router: r, executer: executer, appsConf: apps, startedApps: map[string]*appBind{}, logger: logging.MustGetLogger("test"),
config: &Config{}}
node.config.Node.StaticPubKey = pk
pathutil.EnsureDir(node.dir())
defer os.RemoveAll(node.dir())

require.NoError(t, node.StartApp("skychat"))
time.Sleep(100 * time.Millisecond)
Expand Down
10 changes: 9 additions & 1 deletion pkg/node/rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package node
import (
"context"
"encoding/json"
"github.com/skycoin/skywire/pkg/cipher"
"github.com/skycoin/skywire/pkg/util/pathutil"
"net"
"net/rpc"
"os"
Expand Down Expand Up @@ -47,12 +49,16 @@ func TestListApps(t *testing.T) {
}

func TestStartStopApp(t *testing.T) {
pk, _ := cipher.GenerateKeyPair()
router := new(mockRouter)
executer := new(MockExecuter)
defer os.RemoveAll("skychat")

apps := []AppConfig{{App: "foo", Version: "1.0", AutoStart: false, Port: 10}}
node := &Node{router: router, executer: executer, appsConf: apps, startedApps: map[string]*appBind{}, logger: logging.MustGetLogger("test")}
node := &Node{router: router, executer: executer, appsConf: apps, startedApps: map[string]*appBind{}, logger: logging.MustGetLogger("test"), config: &Config{}}
node.config.Node.StaticPubKey = pk
pathutil.EnsureDir(node.dir())
defer os.RemoveAll(node.dir())

rpc := &RPC{node: node}
unknownApp := "bar"
Expand Down Expand Up @@ -119,6 +125,8 @@ func TestRPC(t *testing.T) {
startedApps: map[string]*appBind{},
logger: logging.MustGetLogger("test"),
}
pathutil.EnsureDir(node.dir())
defer os.RemoveAll(node.dir())

require.NoError(t, node.StartApp("foo"))
require.NoError(t, node.StartApp("bar"))
Expand Down
15 changes: 6 additions & 9 deletions pkg/util/pathutil/homedir.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package pathutil

import (
"fmt"
"github.com/skycoin/skywire/pkg/cipher"
"io/ioutil"
"os"
"path"
"path/filepath"
"runtime"

"github.com/skycoin/skywire/pkg/cipher"
)

// HomeDir obtains the path to the user's home directory via ENVs.
Expand All @@ -25,13 +25,13 @@ func HomeDir() string {

// NodeDir returns a path to a directory used to store specific node configuration. Such dir is ~/.skywire/{PK}
func NodeDir(pk cipher.PubKey) string {
return filepath.Join(HomeDir(),".skycoin","skywire",pk.String())
return filepath.Join(HomeDir(), ".skycoin", "skywire", pk.String())
}

// EnsureDir attempts to create given directory, panics if it fails to do so
func EnsureDir(path string) {
if _, err := os.Stat(path); os.IsNotExist(err) {
err := os.MkdirAll(path, 0644)
err := os.MkdirAll(path, 0755)
if err != nil {
panic(err)
}
Expand All @@ -41,7 +41,6 @@ func EnsureDir(path string) {
// AtomicWriteFile creates a temp file in which to write data, then calls syscall.Rename to swap it and write it on
// filename for an atomic write. On failure temp file is removed and panics.
func AtomicWriteFile(filename string, data []byte) {
fmt.Println("got filename: ", filename)
dir, name := path.Split(filename)
f, err := ioutil.TempFile(dir, name)
if err != nil {
Expand All @@ -63,19 +62,17 @@ func AtomicWriteFile(filename string, data []byte) {
}

if err != nil {
os.Remove(f.Name())
os.Remove(f.Name()) // nolint: errcheck
panic(err)
}
panic(err)
}

// AtomicAppendToFile calls AtomicWriteFile but appends new data to destiny file
func AtomicAppendToFile(filename string, data []byte) {
fmt.Println("got filename: ", filename)
oldFile, err := ioutil.ReadFile(filename)
if err != nil {
panic(err)
}

AtomicWriteFile(filename, append(oldFile, data...))
}

0 comments on commit 14b725c

Please sign in to comment.