GitXplorerGitXplorer
f

go-spfc

public
2 stars
0 forks
0 issues

Commits

List of commits on branch master.
Unverified
1869c3671070548f1481d97007ea204cdb3bfa8a

Merge pull request #2 from franciscocpg/dev

ffranciscocpg committed 9 years ago
Unverified
3df32f033fc14b48a079cb7ac3d54ad002250830

[ci skip] Update README.md

ffranciscocpg committed 9 years ago
Unverified
c87512ff790322223a2c8679be1b9e9eab8fd1dc

Update README.md

ffranciscocpg committed 9 years ago
Unverified
128ab57e229562c3e4101528948a16e8551c862d

Ops

ffranciscocpg committed 9 years ago
Unverified
43049acdec53b3f30e978b385297b1d5f7c5a7c5

Trying to guess mac OS test coverage

ffranciscocpg committed 9 years ago
Unverified
9db794105870638270b6c76edc3be0e7149d35bc

Testing docker branch and trying to make upstart tests stable

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.