Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
32 changed files
with
1,508 additions
and
623 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package immortal | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
/* Test Helpers */ | ||
func expect(t *testing.T, a interface{}, b interface{}) { | ||
if a != b { | ||
t.Errorf("Expected: %v (type %v) Got: %v (type %v)", a, reflect.TypeOf(a), b, reflect.TypeOf(b)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,124 +1,33 @@ | ||
package immortal | ||
|
||
import ( | ||
"gopkg.in/yaml.v2" | ||
"io/ioutil" | ||
"log" | ||
"os" | ||
"os/user" | ||
"path/filepath" | ||
"sync" | ||
) | ||
|
||
type Daemon struct { | ||
sync.Mutex | ||
type Config struct { | ||
Cmd string `yaml:"cmd" json:"cmd"` | ||
Cwd string `yaml:",omitempty" json:",omitempty"` | ||
Env map[string]string `yaml:",omitempty" json:",omitempty"` | ||
Log `yaml:",omitempty" json:",omitempty"` | ||
Logger string `yaml:",omitempty" json:",omitempty"` | ||
Pid `yaml:",omitempty" json:",omitempty"` | ||
User string `yaml:",omitempty" json:",omitempty"` | ||
Wait int `yaml:",omitempty"` | ||
command []string | ||
count uint32 | ||
ctrl Ctrl | ||
ctrl bool | ||
log bool | ||
logger *log.Logger | ||
owner *user.User | ||
pid int | ||
run Run | ||
user *user.User | ||
} | ||
|
||
type Run struct { | ||
Command string | ||
Ctrl bool | ||
Cwd string | ||
Env map[string]string | ||
Logfile string | ||
Logger string | ||
Signals map[string]string | ||
User string | ||
ParentPid string | ||
ChildPid string | ||
FollowPid string | ||
type Pid struct { | ||
Follow string `yaml:",omitempty"` | ||
Parent string `yaml:",omitempty"` | ||
Child string `yaml:",omitempty"` | ||
} | ||
|
||
type Ctrl struct { | ||
fifo chan Return | ||
quit chan struct{} | ||
state chan error | ||
control_fifo *os.File | ||
status_fifo *os.File | ||
} | ||
|
||
type Return struct { | ||
err error | ||
msg string | ||
} | ||
|
||
// New return a instances of Daemon | ||
// u - usr | ||
// c - config | ||
// d - working dir | ||
// f - follow pid | ||
// l - log file | ||
// logger - command to pipe stdout/stderr | ||
// P - parent pidfile | ||
// p - child pidfile | ||
// cmd - command to supervice | ||
// ctrl - create supervise dir | ||
func New(u *user.User, c, d, f, l, logger, p, P *string, cmd []string, ctrl *bool) (*Daemon, error) { | ||
if *c != "" { | ||
yml_file, err := ioutil.ReadFile(*c) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var D Daemon | ||
|
||
if err := yaml.Unmarshal(yml_file, &D); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &D, nil | ||
} | ||
|
||
daemon := &Daemon{ | ||
owner: u, | ||
command: cmd, | ||
run: Run{ | ||
Cwd: *d, | ||
FollowPid: *f, | ||
Logfile: *l, | ||
Logger: *logger, | ||
ParentPid: *P, | ||
ChildPid: *p, | ||
Ctrl: *ctrl, | ||
}, | ||
ctrl: Ctrl{ | ||
fifo: make(chan Return), | ||
quit: make(chan struct{}), | ||
state: make(chan error), | ||
}, | ||
} | ||
|
||
if *ctrl { | ||
wd, err := os.Getwd() | ||
if err != nil { | ||
return nil, err | ||
} | ||
wd = filepath.Join(wd, "supervise") | ||
if err := os.MkdirAll(wd, 0700); err != nil { | ||
return nil, err | ||
} | ||
// create control pipe | ||
daemon.ctrl.control_fifo, err = MakeFIFO(filepath.Join(wd, "control")) | ||
if err != nil { | ||
return nil, err | ||
} | ||
// create status pipe | ||
daemon.ctrl.status_fifo, err = MakeFIFO(filepath.Join(wd, "status")) | ||
if err != nil { | ||
return nil, err | ||
} | ||
// create lock | ||
if err = Lock(filepath.Join(wd, "lock")); err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
return daemon, nil | ||
type Log struct { | ||
File string `yaml:",omitempty"` | ||
Age int `yaml:",omitempty"` | ||
Num int `yaml:",omitempty"` | ||
Size int `yaml:",omitempty"` | ||
} |
Oops, something went wrong.