-
-
Notifications
You must be signed in to change notification settings - Fork 54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Watchman integration #188
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,8 +5,6 @@ import ( | |
"context" | ||
"errors" | ||
"fmt" | ||
"github.com/cakturk/go-netstat/netstat" | ||
"github.com/f1bonacc1/process-compose/src/types" | ||
"io" | ||
"math/rand" | ||
"os" | ||
|
@@ -17,6 +15,9 @@ import ( | |
"syscall" | ||
"time" | ||
|
||
"github.com/cakturk/go-netstat/netstat" | ||
"github.com/f1bonacc1/process-compose/src/types" | ||
|
||
"github.com/f1bonacc1/process-compose/src/command" | ||
"github.com/f1bonacc1/process-compose/src/health" | ||
"github.com/f1bonacc1/process-compose/src/pclog" | ||
|
@@ -65,6 +66,13 @@ type Process struct { | |
isMain bool | ||
extraArgs []string | ||
isStopped atomic.Bool | ||
isDevRestart atomic.Bool | ||
devWatchers []devWatch | ||
} | ||
|
||
type devWatch struct { | ||
sub *WatchmanSub | ||
config *types.Watch | ||
} | ||
|
||
func NewProcess( | ||
|
@@ -77,6 +85,7 @@ func NewProcess( | |
printLogs bool, | ||
isMain bool, | ||
extraArgs []string, | ||
watchman *Watchman, | ||
) *Process { | ||
colNumeric := rand.Intn(int(color.FgHiWhite)-int(color.FgHiBlack)) + int(color.FgHiBlack) | ||
|
||
|
@@ -104,6 +113,7 @@ func NewProcess( | |
proc.setUpProbes() | ||
proc.procCond = *sync.NewCond(proc) | ||
proc.procStartedCond = *sync.NewCond(proc) | ||
proc.setUpWatchman(watchman) | ||
return proc | ||
} | ||
|
||
|
@@ -139,9 +149,9 @@ func (p *Process) run() int { | |
|
||
p.startProbes() | ||
|
||
//Wait should wait for I/O consumption, but if the execution is too fast | ||
//e.g. echo 'hello world' the output will not reach the pipe | ||
//TODO Fix this | ||
// Wait should wait for I/O consumption, but if the execution is too fast | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sorry i have |
||
// e.g. echo 'hello world' the output will not reach the pipe | ||
// TODO Fix this | ||
time.Sleep(50 * time.Millisecond) | ||
_ = p.command.Wait() | ||
p.Lock() | ||
|
@@ -212,7 +222,6 @@ func (p *Process) getCommander() command.Commander { | |
p.mergeExtraArgs(), | ||
) | ||
} | ||
|
||
} | ||
|
||
func (p *Process) mergeExtraArgs() []string { | ||
|
@@ -254,6 +263,11 @@ func (p *Process) isRestartable() bool { | |
p.Lock() | ||
exitCode := p.getExitCode() | ||
p.Unlock() | ||
|
||
if p.isDevRestart.Swap(false) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i needed a new signal for when the restart is associated with a watch. i thought about changing
this is probably still the better solution (especially since |
||
return true | ||
} | ||
|
||
if p.isStopped.Swap(false) { | ||
return false | ||
} | ||
|
@@ -381,16 +395,35 @@ func (p *Process) isRunning() bool { | |
|
||
func (p *Process) prepareForShutDown() { | ||
// prevent restart during global shutdown or scale down | ||
//p.procConf.RestartPolicy.Restart = types.RestartPolicyNo | ||
// p.procConf.RestartPolicy.Restart = types.RestartPolicyNo | ||
p.isStopped.Store(true) | ||
|
||
} | ||
|
||
func (p *Process) onProcessStart() { | ||
if isStringDefined(p.procConf.LogLocation) { | ||
p.logger.Open(p.getLogPath(), p.procConf.LoggerConfig) | ||
} | ||
|
||
for _, watch := range p.devWatchers { | ||
go func() { | ||
for { | ||
files, ok := watch.sub.Recv() | ||
if !ok { | ||
log. | ||
Debug(). | ||
Msg("watchman sub closed, exiting") | ||
return | ||
} | ||
|
||
if len(files) == 0 { | ||
continue | ||
} | ||
|
||
p.isDevRestart.Store(true) | ||
} | ||
}() | ||
} | ||
|
||
p.Lock() | ||
p.started = true | ||
p.Unlock() | ||
|
@@ -457,8 +490,8 @@ func (p *Process) updateProcState() { | |
p.procState.Name = p.getName() | ||
} | ||
p.procState.IsRunning = isRunning | ||
|
||
} | ||
|
||
func (p *Process) setStartTime(startTime time.Time) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i should use this value to reset the watchman subscription clock |
||
p.timeMutex.Lock() | ||
defer p.timeMutex.Unlock() | ||
|
@@ -663,6 +696,13 @@ func (p *Process) onReadinessCheckEnd(isOk, isFatal bool, err string) { | |
} | ||
} | ||
|
||
func (p *Process) setUpWatchman(watchman *Watchman) { | ||
for _, config := range p.procConf.Watch { | ||
recv := watchman.Subscribe(config) | ||
p.devWatchers = append(p.devWatchers, devWatch{recv, config}) | ||
} | ||
} | ||
|
||
func (p *Process) validateProcess() error { | ||
if isStringDefined(p.procConf.WorkingDir) { | ||
stat, err := os.Stat(p.procConf.WorkingDir) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this won't be pinned to a revision by the time i mark this pr as ready. once i finish this integration and clean up the library's api and do some more tests i'll cut a 0.3 release