GitXplorerGitXplorer
f

go-spfc

public
2 stars
0 forks
0 issues

Commits

List of commits on branch master.
Unverified
1293e6ea4bbdd1a38bc85514bacd03fc4b7f7a5b

Branch for docker

ffranciscocpg committed 9 years ago
Unverified
70b37534f46241ac79d7d0af6875e022b5a70b76

Update README.md

ffranciscocpg committed 9 years ago
Unverified
bdd938599acaf8861ea7cad17411f062b55c630e

Merge from dev

ffranciscocpg committed 9 years ago
Unverified
9865eed48d1986976bf5da281e18112bbf082cd6

Branch for docker

ffranciscocpg committed 9 years ago
Unverified
8d0917cd6aad28cf8931bb4a3a10104074eade34

Docker ibn

ffranciscocpg committed 9 years ago
Unverified
48aa59046074187cd8a449da3b488cce5a83fcfd

Adjust test for ubuntu

ffranciscocpg committed 9 years ago

README

The README file for this repository.

go-spfc

Doc

GoDoc

Build

master

Build Status codecov.io

dev

Build Status codecov.io

A lib for wrapping services in Linux (Upstart or systemd) and MacOS (launchd).

Installing

  1. With go get: go get github.com/franciscocpg/go-spfc
  2. Or, the best way, using some dependency manager like glide

Using

Supposing you have a mongodb service installed in a CentOS (running systemd) or Ubuntu (running systemd or upstart) or MacOS (running launchd) machine you can instantiate a new handler this way:

 package main

import (
	"fmt"
	"github.com/franciscocpg/go-spfc"
)

func main() {
	h := service.NewHandler("mongod")
	st, err := h.Start()
	errorHandler(err)
	printStatus(st)
	st, err = h.GetStatus()
	errorHandler(err)
	printStatus(st)
	st, err = h.Stop()
	errorHandler(err)
	printStatus(st)
}

func printStatus(st service.Status) {
	fmt.Printf("Running: %t, st.PID: %d\n", st.Running, st.PID)
}

func errorHandler(err error) {
	if err != nil {
		panic(err)
	}
}

result

Running: true, st.PID: 3765
Running: true, st.PID: 3765
Running: false, st.PID: 0

Wait service to start or stop

Especially when you are using a Mac the previous code can result something like this

Running: false, st.PID: 0
Running: false, st.PID: 0
Running: false, st.PID: 0

What happens? The problem is that launchd runs commands in a asynchronous way, so when we get the status, the service was not started (or stopped) yet. To workaround this just wait the service start or stop this way

package main

import (
	"fmt"t
	"github.com/franciscocpg/go-spfc"
	"time"
)

func main() {
	timeout := 10 * time.Second
	h := service.NewHandler("mongod")
	st, err := h.StartAndWait(timeout)
	errorHandler(err)
	printStatus(st)
	st, err = h.GetStatus()
	errorHandler(err)
	printStatus(st)
	st, err = h.StopAndWait(timeout)
	errorHandler(err)
	printStatus(st)
}

func printStatus(st service.Status) {
	fmt.Printf("Running: %t, st.PID: %d\n", st.Running, st.PID)
}

func errorHandler(err error) {
	if err != nil {
		panic(err)
	}
}

If someting goes wrong, the err is populate with a timeout message.

TODO

  • Implement the systemd communication using DBUS (go-systemd) instead of a process call to systemctl.