Home

Awesome

goproc

NOTE: versions before v1.4502.2114 are deleted because it have goroutine leaks.

Simple process manager helper library, features:

Versioning

versioning using this format 1.(M+(YEAR-2021)*12)DD.HMM, so for example v1.213.1549 means it was released at 2021-02-13 15:49

Example


daemon := goproc.New()

cmdId := daemon.AddCommand(&goproc.Cmd{
    Program: `sleep`, // program to run
	WorkDir: `/tmp`, // directory to run the program (optional)
    Parameters: []string{`2`}, // command line arguments
    MaxRestart: goproc.RestartForever, // default: NoRestart=0
    OnStderr: func(cmd *goproc.Cmd, line string) error { // optional
        fmt.Println(`OnStderr: `+line)
        return nil
    },
    OnStdout: func(cmd *goproc.Cmd, line string) error { // optional
        fmt.Println(`OnStdout: `+line)
        return nil
    },
})

daemon.Start(cmdId) // use "go" keyword if you need non-blocking version
// ^ by default will only run next command if previous command exited
// if you want to run them in parralel, use daemon.StartParallel().Wait()

daemon.CommandString(cmdId) // returns "sleep 2"

Alternatively there's shortcut with Run1 returns string stdout and stderr, error, and exitCode. Also there's RunLines returns []string instead of string.

FAQ

Q: Why not just channel? why callback?

A: Because channel requires a consumer, or it would stuck/block if channel is full, while callback doesn't (this is why I add flag to activate the channel API). To the Percona reviewer that rejected me because I didn't use channel at the first time "the whole thing is written in JavaScript translated to Go, not in Go. Technical task does not adhere to Go best practices which is what would expect from the candidate. One example was that the code is written in JS-like style (e.g. callback). Go code does not use callback like node.js for example", well, jokes on you even net/http.HandleFunc uses callback XD

Q: How to ignore error being printed?

A: assign Goproc.HasErrFunc with goproc.DiscardHasErr, other option are: L.IsError (default), goproc.LogHasErr (uses log), goproc.PrintHasErr (uses fmt), or you can always create your own.

TODO