-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
177 additions
and
249 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package app2 | ||
package appserver | ||
|
||
import "github.com/skycoin/dmsg/cipher" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package appserver | ||
|
||
import ( | ||
"fmt" | ||
"os/exec" | ||
"path/filepath" | ||
|
||
"github.com/skycoin/skycoin/src/util/logging" | ||
) | ||
|
||
// Proc is a wrapper for a skywire app. Encapsulates | ||
// the running proccess itself and the RPC server for | ||
// app/visor communication. | ||
type Proc struct { | ||
key Key | ||
config Config | ||
log *logging.Logger | ||
rpcS *Server | ||
cmd *exec.Cmd | ||
} | ||
|
||
// NewProc constructs `Proc`. | ||
func NewProc(log *logging.Logger, c Config, args []string) (*Proc, error) { | ||
key := GenerateAppKey() | ||
|
||
binaryPath := getBinaryPath(c.BinaryDir, c.Name, c.Version) | ||
|
||
const ( | ||
appKeyEnvFormat = "APP_KEY=%s" | ||
sockFileEnvFormat = "SW_UNIX=%s" | ||
) | ||
|
||
env := make([]string, 0, 2) | ||
env = append(env, fmt.Sprintf(appKeyEnvFormat, key)) | ||
env = append(env, fmt.Sprintf(sockFileEnvFormat, c.SockFile)) | ||
|
||
cmd := exec.Command(binaryPath, args...) // nolint:gosec | ||
|
||
cmd.Env = env | ||
cmd.Dir = c.WorkDir | ||
|
||
rpcS, err := New(logging.MustGetLogger(fmt.Sprintf("app_rpc_server_%s", key)), | ||
c.SockFile, key) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &Proc{ | ||
key: key, | ||
config: c, | ||
log: log, | ||
cmd: cmd, | ||
rpcS: rpcS, | ||
}, nil | ||
} | ||
|
||
// Run runs the application. It starts the process and runs the | ||
// RPC communication server. | ||
func (p *Proc) Run() error { | ||
go func() { | ||
if err := p.rpcS.ListenAndServe(); err != nil { | ||
p.log.WithError(err).Error("error serving RPC") | ||
} | ||
}() | ||
|
||
if err := p.cmd.Run(); err != nil { | ||
p.closeRPCServer() | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// Stop stops the applicacation. It stops the process and | ||
// shuts down the RPC server. | ||
func (p *Proc) Stop() error { | ||
p.closeRPCServer() | ||
return p.cmd.Process.Kill() | ||
} | ||
|
||
// Wait shuts down the RPC server and waits for the | ||
// application cmd to exit. | ||
func (p *Proc) Wait() error { | ||
p.closeRPCServer() | ||
return p.cmd.Wait() | ||
} | ||
|
||
// closeRPCServer closes RPC server and logs error if any. | ||
func (p *Proc) closeRPCServer() { | ||
if err := p.rpcS.Close(); err != nil { | ||
p.log.WithError(err).Error("error closing RPC server") | ||
} | ||
} | ||
|
||
// getBinaryPath formats binary path using app dir, name and version. | ||
func getBinaryPath(dir, name, ver string) string { | ||
const binaryNameFormat = "%s.v%s" | ||
return filepath.Join(dir, fmt.Sprintf(binaryNameFormat, name, ver)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package appserver | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/pkg/errors" | ||
|
||
"github.com/skycoin/skycoin/src/util/logging" | ||
) | ||
|
||
// ProcManager allows to manage skywire applications. | ||
type ProcManager struct { | ||
procs map[string]*Proc | ||
mx sync.RWMutex | ||
} | ||
|
||
// NewProcManager constructs `ProcManager`. | ||
func NewProcManager() *ProcManager { | ||
return &ProcManager{ | ||
procs: make(map[string]*Proc), | ||
} | ||
} | ||
|
||
// Run runs the application according to its config and additional args. | ||
func (m *ProcManager) Run(log *logging.Logger, c Config, args []string) error { | ||
// TODO: pass another logging instance? | ||
p, err := NewProc(log, c, args) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := p.Run(); err != nil { | ||
return err | ||
} | ||
|
||
m.mx.Lock() | ||
m.procs[c.Name] = p | ||
m.mx.Unlock() | ||
|
||
return nil | ||
} | ||
|
||
// Stop stops the application. | ||
func (m *ProcManager) Stop(name string) error { | ||
p, err := m.pop(name) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return p.Stop() | ||
} | ||
|
||
// Wait waits for the application to exit. | ||
func (m *ProcManager) Wait(name string) error { | ||
p, err := m.pop(name) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return p.Wait() | ||
} | ||
|
||
func (m *ProcManager) pop(name string) (*Proc, error) { | ||
m.mx.Lock() | ||
p, ok := m.procs[name] | ||
if !ok { | ||
m.mx.Unlock() | ||
return nil, errors.New("no such app") | ||
} | ||
delete(m.procs, name) | ||
m.mx.Unlock() | ||
|
||
return p, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.