service

package module
v0.0.0-...-1869c36 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 4, 2016 License: MIT Imports: 5 Imported by: 0

README

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.

Documentation

Overview

Package service provides Start, Status and Stop functions

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ControlType

type ControlType int

ControlType represents the service control type

const (

	// LaunchCtl - Mac OS implementation (https://developer.applh.com/library/mac/documentation/Darwin/Reference/ManPages/man1/launchctl.1.html)
	LaunchCtl ControlType
	// Upstart implementation (http://upstart.ubuntu.com/)
	Upstart
	// SystemD is systemd implementation (https://fedoraproject.org/wiki/Systemd, https://github.com/systemd/systemd)
	SystemD
)

type Handler

type Handler struct {
	Sudo        bool
	ServiceName string
}

Handler represents a service instance for execution operation

func NewHandler

func NewHandler(serviceName string) *Handler

NewHandler constructs a handler with a given name. In linux with sudo true and Mac sudo false

func (*Handler) GetStatus

func (h *Handler) GetStatus() (Status, error)

GetStatus show the status for a service

func (*Handler) Start

func (h *Handler) Start() (Status, error)

Start starts a service

func (*Handler) StartAndWait

func (h *Handler) StartAndWait(timeout time.Duration) (Status, error)

StartAndWait starts a service and wait it starts

func (*Handler) Stop

func (h *Handler) Stop() (Status, error)

Stop stops a service

func (*Handler) StopAndWait

func (h *Handler) StopAndWait(timeout time.Duration) (Status, error)

StopAndWait stops a service and wait it stops

type Status

type Status struct {
	Running bool
	PID     int
}

Status represents a service status

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL