Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add gonit daemon options to ConfigManager Settings

Change-Id: Ie16a3f3bb3c59de22ca66fa205a02ed88a0b2552
  • Loading branch information...
commit 21ff275ba3de4a955007df37a87eabc2cb26dcfb 1 parent 13d0ffd
@dougm dougm authored
View
27 configmanager.go
@@ -26,6 +26,9 @@ type ConfigManager struct {
type Settings struct {
AlertTransport string
SocketFile string
+ RpcServerUrl string
+ PollInterval int
+ Daemon *Process
}
type ProcessGroup struct {
@@ -166,7 +169,7 @@ func (c *ConfigManager) parseDir(dirPath string) error {
}
// Applies default global settings if some options haven't been specified.
-func (c *ConfigManager) applyDefaultSettings() {
+func (c *ConfigManager) ApplyDefaultSettings() {
if c.Settings == nil {
c.Settings = &Settings{}
}
@@ -174,6 +177,26 @@ func (c *ConfigManager) applyDefaultSettings() {
if settings.AlertTransport == "" {
settings.AlertTransport = DEFAULT_ALERT_TRANSPORT
}
+ if settings.Daemon == nil {
+ settings.Daemon = &Process{}
+ }
+ daemon := settings.Daemon
+ if daemon.Dir == "" {
+ daemon.Dir = os.Getenv("HOME")
+ }
+
+ if daemon.Name == "" {
+ daemon.Name = filepath.Base(os.Args[0])
+ }
+ if daemon.Pidfile == "" {
+ defaultPath := "." + daemon.Name + ".pid"
+ daemon.Pidfile = filepath.Join(daemon.Dir, defaultPath)
+ }
+
+ if settings.RpcServerUrl == "" {
+ defaultPath := "." + daemon.Name + ".sock"
+ settings.RpcServerUrl = filepath.Join(daemon.Dir, defaultPath)
+ }
}
// Parses a file.
@@ -217,7 +240,7 @@ func (c *ConfigManager) Parse(paths ...string) error {
if c.Settings == nil {
log.Printf("No settings found, using defaults.")
}
- c.applyDefaultSettings()
+ c.ApplyDefaultSettings()
c.applyDefaultConfigOpts()
if err := c.validate(); err != nil {
return err
View
4 configmanager_test.go
@@ -27,6 +27,10 @@ func assertFileParsed(t *testing.T, configManager *ConfigManager) {
assert.Equal(t, (*Event)(nil), pg.EventByName("blah"))
assert.Equal(t, "none", configManager.Settings.AlertTransport)
+ assert.NotEqual(t, "", configManager.Settings.RpcServerUrl)
+ assert.Equal(t, 0, configManager.Settings.PollInterval)
+ assert.NotEqual(t, nil, configManager.Settings.Daemon)
+ assert.Equal(t, "lolnit", configManager.Settings.Daemon.Name)
}
func TestGetPid(t *testing.T) {
View
59 gonit/main.go
@@ -14,7 +14,6 @@ import (
"os/signal"
"path/filepath"
"syscall"
- "time"
)
var (
@@ -22,21 +21,16 @@ var (
config string
pidfile string
rpcUrl string
- polltime time.Duration
+ poll int
group bool
foreground bool
version bool
- // defaults
- home = os.Getenv("HOME")
- name = filepath.Base(os.Args[0]) // gonit
- defaultPidFile = filepath.Join(home, "."+name+".pid")
- defaultRpcUrl = filepath.Join(home, "."+name+".sock")
-
// internal
api *gonit.API
rpcServer *gonit.RpcServer
eventMonitor *gonit.EventMonitor
+ settings *gonit.Settings
)
func main() {
@@ -54,12 +48,17 @@ func main() {
if err != nil {
log.Fatal(err)
}
+ } else {
+ configManager.ApplyDefaultSettings()
}
+ settings = configManager.Settings
+ applySettings()
+
api = gonit.NewAPI(configManager)
args := flag.Args()
if len(args) == 0 {
- if polltime != 0 {
+ if settings.PollInterval != 0 {
runDaemon(api.Control, configManager)
} else {
log.Fatal("Nothing todo (yet)")
@@ -75,15 +74,26 @@ func main() {
}
}
+func applySettings() {
+ if pidfile != "" {
+ settings.Daemon.Pidfile = pidfile
+ }
+ if rpcUrl != "" {
+ settings.RpcServerUrl = rpcUrl
+ }
+ if poll != 0 {
+ settings.PollInterval = poll
+ }
+}
+
func parseFlags() {
flag.BoolVar(&version, "V", false, "Print version number")
flag.BoolVar(&group, "g", false, "Use process group")
flag.BoolVar(&foreground, "I", false, "Do not run in background")
flag.StringVar(&config, "c", "", "Config path")
- // XXX should be able to use gonit.yml for the following opts
- flag.StringVar(&pidfile, "p", defaultPidFile, "Pid file path")
- flag.StringVar(&rpcUrl, "s", defaultRpcUrl, "RPC server URL")
- flag.DurationVar(&polltime, "d", 0, "Run as a daemon with duration")
+ flag.StringVar(&pidfile, "p", "", "Pid file path")
+ flag.StringVar(&rpcUrl, "s", "", "RPC server URL")
+ flag.IntVar(&poll, "d", 0, "Run as a daemon with duration")
const named = "the named process or group"
const all = "all processes"
@@ -109,6 +119,7 @@ func parseFlags() {
}
flag.Usage = func() {
+ name := filepath.Base(os.Args[0]) // gonit
fmt.Println("Usage:", name, "[options] {arguments}")
fmt.Println("Options are as follows:")
@@ -125,7 +136,7 @@ func parseFlags() {
}
func rpcClient() *rpc.Client {
- url, err := url.Parse(rpcUrl)
+ url, err := url.Parse(settings.RpcServerUrl)
if err != nil {
log.Fatal(err)
}
@@ -153,7 +164,7 @@ func rpcClient() *rpc.Client {
func runCommand(cmd, arg string) {
var client gonit.CliClient
- if isRunning() {
+ if settings.Daemon.IsRunning() {
rpc := rpcClient()
defer rpc.Close()
client = gonit.NewRemoteClient(rpc, api)
@@ -197,7 +208,7 @@ func shutdown() {
func start() {
var err error
- rpcServer, err = gonit.NewRpcServer(rpcUrl)
+ rpcServer, err = gonit.NewRpcServer(settings.RpcServerUrl)
if err != nil {
log.Fatal(err)
}
@@ -231,27 +242,21 @@ func loop() {
}
}
-func isRunning() bool {
- pid, err := gonit.ReadPidFile(pidfile)
-
- return err == nil && syscall.Kill(pid, 0) == nil
-}
-
func runDaemon(control *gonit.Control, configManager *gonit.ConfigManager) {
- if isRunning() {
- log.Fatalf("%s daemon is already running", name)
+ daemon := settings.Daemon
+ if daemon.IsRunning() {
+ log.Fatalf("%s daemon is already running", daemon.Name)
}
if !foreground {
log.Print("daemonize - not yet supported")
}
- log.Printf("Saving %s daemon pid to file=%s", name, pidfile)
- err := gonit.WritePidFile(os.Getpid(), pidfile)
+ err := daemon.SavePid(os.Getpid())
if err != nil {
log.Fatal(err)
}
- defer os.Remove(pidfile)
+ defer os.Remove(daemon.Pidfile)
createEventMonitor(control, configManager)
start()
loop()
View
2  process.go
@@ -9,6 +9,7 @@ import (
"errors"
"io"
"io/ioutil"
+ "log"
"os"
"os/exec"
"os/user"
@@ -205,6 +206,7 @@ func WritePidFile(pid int, path string) error {
// Write pid to Pidfile
func (p *Process) SavePid(pid int) error {
+ log.Printf("Saving %q pid to file=%s", p.Name, p.Pidfile)
return WritePidFile(pid, p.Pidfile)
}
View
3  test/config/gonit.yml
@@ -1,2 +1,5 @@
---
alerttransport: none
+daemon:
+ name: lolnit
+ pidfile: /tmp/lolnit.pid

0 comments on commit 21ff275

Please sign in to comment.
Something went wrong with that request. Please try again.