1
0
Эх сурвалжийг харах

refactor(supervisor): add an interface for the process

Mustafa Arici 8 жил өмнө
parent
commit
f267dc37fd
2 өөрчлөгдсөн 14 нэмэгдсэн , 10 устгасан
  1. 8 5
      supervisor/supervisor.go
  2. 6 5
      vpn.go

+ 8 - 5
supervisor/supervisor.go

@@ -67,6 +67,14 @@ var tt = []transition{
 	transition{currState: STOPPING, nextState: STOPPING},
 }
 
+// Supervisable is an interface that represents a process.
+type Supervisable interface {
+	Start()
+	Stop()
+	Restart()
+	Status() State
+}
+
 // Process represents a unix process to be supervised.
 type Process struct {
 	lock            *sync.RWMutex
@@ -147,11 +155,6 @@ func (p *Process) Status() State {
 	return p.state
 }
 
-// IsRunning retunrs wether the process is running or not.
-func (p *Process) IsRunning() bool {
-	return p.state == RUNNING
-}
-
 func (p *Process) permittable(state State) bool {
 	p.lock.Lock()
 	defer p.lock.Unlock()

+ 6 - 5
vpn.go

@@ -218,7 +218,7 @@ func GetSystemCA() (*pki.CA, error) {
 }
 
 // vpnProc represents the OpenVPN process that is managed by the ovpm supervisor globally OpenVPN.
-var vpnProc *supervisor.Process
+var vpnProc supervisor.Supervisable
 
 // StartVPNProc starts the OpenVPN process.
 func StartVPNProc() {
@@ -229,7 +229,7 @@ func StartVPNProc() {
 	if vpnProc == nil {
 		panic(fmt.Sprintf("vpnProc is not initialized!"))
 	}
-	if vpnProc.IsRunning() {
+	if vpnProc.Status() == supervisor.RUNNING {
 		logrus.Error("OpenVPN is already started")
 		return
 	}
@@ -254,11 +254,12 @@ func StopVPNProc() {
 	if vpnProc == nil {
 		panic(fmt.Sprintf("vpnProc is not initialized!"))
 	}
-	if !vpnProc.IsRunning() {
-		logrus.Error("OpenVPN is already stopped")
+	if vpnProc.Status() != supervisor.RUNNING {
+		logrus.Error("OpenVPN is already not running")
 		return
 	}
 	vpnProc.Stop()
+
 }
 
 // Emit generates all needed files for the OpenVPN server and dumps them to their corresponding paths defined in the config.
@@ -320,7 +321,7 @@ func Emit() error {
 	logrus.Info("configurations emitted to the filesystem")
 
 	// If the OpenVPN is already running, restart it.
-	if vpnProc.IsRunning() {
+	if vpnProc.Status() == supervisor.RUNNING {
 		logrus.Info("OpenVPN process is restarting")
 		RestartVPNProc()
 	}