Skip to content

Commit

Permalink
Merge f9c6898 into 959693a
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Lo-A-Foe committed Apr 22, 2018
2 parents 959693a + f9c6898 commit c091376
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 18 deletions.
20 changes: 15 additions & 5 deletions cmd/immortal/main.go
Expand Up @@ -7,7 +7,6 @@ import (
"log/syslog"
"os"
"os/user"
"path/filepath"
"strings"

"github.com/immortal/immortal"
Expand Down Expand Up @@ -49,11 +48,22 @@ func main() {
if len(cfg.Require) > 0 {
down := []string{}
ctl := &immortal.Controller{}
services, _ := ctl.FindServices(immortal.GetSdir())
if userServices, err := ctl.FindServices(
immortal.GetUserdir(),
); err == nil {
services = append(services, userServices...)
}
for _, r := range cfg.Require {
socket := filepath.Join(immortal.GetSdir(), r, "immortal.sock")
if status, err := ctl.GetStatus(socket); err != nil {
down = append(down, r)
} else if status.Up == "" {
isDown := true
for _, s := range services {
s.Status, err = ctl.GetStatus(s.Socket)
if err == nil && (r == s.Status.Name || r == s.Name) {
isDown = false
break
}
}
if isDown {
down = append(down, r)
}
}
Expand Down
17 changes: 4 additions & 13 deletions cmd/immortalctl/main.go
Expand Up @@ -4,8 +4,6 @@ import (
"flag"
"fmt"
"os"
"os/user"
"path/filepath"
"sync"
"time"

Expand Down Expand Up @@ -145,18 +143,8 @@ func main() {
// get status for all services
services, _ := ctl.FindServices(sdir)

// get user $HOME/.immortal services
home := os.Getenv("HOME")
if home == "" {
usr, err := user.Current()
if err != nil {
fmt.Fprintf(os.Stderr, "error getting user home: %s\n", err)
os.Exit(1)
}
home = usr.HomeDir
}
if userServices, err := ctl.FindServices(
filepath.Join(home, ".immortal"),
immortal.GetUserdir(),
); err == nil {
services = append(services, userServices...)
}
Expand Down Expand Up @@ -197,6 +185,9 @@ func main() {
} else {
s.Status = status
s.SignalResponse = res
if len(s.Status.Name) > 0 { // Override name if provided
s.Name = s.Status.Name
}
queue <- &Pad{
pid: len(fmt.Sprintf("%d", status.Pid)),
up: len(status.Up),
Expand Down
1 change: 1 addition & 0 deletions config.go
Expand Up @@ -8,6 +8,7 @@ import (
type Config struct {
Cmd string `yaml:"cmd" json:"cmd"`
Cwd string `yaml:",omitempty" json:",omitempty"`
Name string `yaml:"name,omitempty" json:"name,omitempty"`
Env map[string]string `yaml:",omitempty" json:",omitempty"`
Log Log `yaml:",omitempty" json:",omitempty"`
Stderr Log `yaml:",omitempty" json:",omitempty"`
Expand Down
16 changes: 16 additions & 0 deletions funcs.go
Expand Up @@ -9,6 +9,8 @@ import (
"net"
"net/http"
"os"
"os/user"
"path/filepath"
"time"
)

Expand Down Expand Up @@ -85,6 +87,20 @@ func inSlice(s []string, item string) bool {
return false
}

// GetUserdir returns the $HOME/.immortal
func GetUserdir() string {
home := os.Getenv("HOME")
if home == "" {
usr, err := user.Current()
if err != nil {
fmt.Fprintf(os.Stderr, "error getting user home: %s\n", err)
os.Exit(1)
}
home = usr.HomeDir
}
return filepath.Join(home, ".immortal")
}

// GetSdir return the main supervise directory, defaults to /var/run/immortal
func GetSdir() string {
// if IMMORTAL_SDIR env is set, use it as default sdir
Expand Down
13 changes: 13 additions & 0 deletions funcs_test.go
Expand Up @@ -3,6 +3,7 @@ package immortal
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"time"
)
Expand Down Expand Up @@ -61,6 +62,18 @@ func TestInSlice(t *testing.T) {
}
}

func TestGetUserdir(t *testing.T) {
oldHome := os.Getenv("HOME")

os.Setenv("HOME", "/tmp/foo")
expect(t, GetUserdir(), filepath.Join("/tmp/foo", ".immortal"))

os.Setenv("HOME", "")
expect(t, GetUserdir(), filepath.Join(oldHome, ".immortal"))

os.Setenv("HOME", oldHome)
}

func TestIsDir(t *testing.T) {
expect(t, false, isDir("/dev/null"))
expect(t, true, isDir("/"))
Expand Down
2 changes: 2 additions & 0 deletions parser.go
Expand Up @@ -188,6 +188,8 @@ func ParseArgs(p Parser, fs *flag.FlagSet) (cfg *Config, err error) {
return
}
}
serviceFile := filepath.Base(flags.Configfile)
cfg.Name = strings.TrimSuffix(serviceFile, filepath.Ext(serviceFile))
cfg.ctl = sdir
return
}
Expand Down
4 changes: 4 additions & 0 deletions socket.go
Expand Up @@ -23,6 +23,7 @@ type Status struct {
Fpid bool `json:"fpid"`
Count uint32 `json:"count"`
Status string `json:"status,omitempty"`
Name string `json:"name,omitempty"`
}

// Listen creates a unix socket used for control the daemon
Expand Down Expand Up @@ -59,6 +60,9 @@ func (d *Daemon) HandleStatus(w http.ResponseWriter, r *http.Request) {
startin := d.process.sTime.Add(time.Duration(d.cfg.Wait) * time.Second)
status.Status = fmt.Sprintf("Starting in %0.1f seconds", startin.Sub(time.Now()).Seconds())
}
if len(d.cfg.Name) > 0 {
status.Name = d.cfg.Name
}

// return status in json
w.Header().Set("Content-Type", "application/json")
Expand Down

0 comments on commit c091376

Please sign in to comment.