55 "context"
66 "errors"
77 "fmt"
8- "github.com/cakturk/go-netstat/netstat"
9- "github.com/f1bonacc1/process-compose/src/types"
108 "io"
119 "math/rand"
1210 "os"
@@ -17,6 +15,9 @@ import (
1715 "syscall"
1816 "time"
1917
18+ "github.com/cakturk/go-netstat/netstat"
19+ "github.com/f1bonacc1/process-compose/src/types"
20+
2021 "github.com/f1bonacc1/process-compose/src/command"
2122 "github.com/f1bonacc1/process-compose/src/health"
2223 "github.com/f1bonacc1/process-compose/src/pclog"
@@ -65,6 +66,13 @@ type Process struct {
6566 isMain bool
6667 extraArgs []string
6768 isStopped atomic.Bool
69+ isDevRestart atomic.Bool
70+ devWatchers []devWatch
71+ }
72+
73+ type devWatch struct {
74+ sub * WatchmanSub
75+ config * types.Watch
6876}
6977
7078func NewProcess (
@@ -77,6 +85,7 @@ func NewProcess(
7785 printLogs bool ,
7886 isMain bool ,
7987 extraArgs []string ,
88+ watchman * Watchman ,
8089) * Process {
8190 colNumeric := rand .Intn (int (color .FgHiWhite )- int (color .FgHiBlack )) + int (color .FgHiBlack )
8291
@@ -104,6 +113,7 @@ func NewProcess(
104113 proc .setUpProbes ()
105114 proc .procCond = * sync .NewCond (proc )
106115 proc .procStartedCond = * sync .NewCond (proc )
116+ proc .setUpWatchman (watchman )
107117 return proc
108118}
109119
@@ -139,9 +149,9 @@ func (p *Process) run() int {
139149
140150 p .startProbes ()
141151
142- //Wait should wait for I/O consumption, but if the execution is too fast
143- //e.g. echo 'hello world' the output will not reach the pipe
144- //TODO Fix this
152+ // Wait should wait for I/O consumption, but if the execution is too fast
153+ // e.g. echo 'hello world' the output will not reach the pipe
154+ // TODO Fix this
145155 time .Sleep (50 * time .Millisecond )
146156 _ = p .command .Wait ()
147157 p .Lock ()
@@ -212,7 +222,6 @@ func (p *Process) getCommander() command.Commander {
212222 p .mergeExtraArgs (),
213223 )
214224 }
215-
216225}
217226
218227func (p * Process ) mergeExtraArgs () []string {
@@ -254,6 +263,11 @@ func (p *Process) isRestartable() bool {
254263 p .Lock ()
255264 exitCode := p .getExitCode ()
256265 p .Unlock ()
266+
267+ if p .isDevRestart .Swap (false ) {
268+ return true
269+ }
270+
257271 if p .isStopped .Swap (false ) {
258272 return false
259273 }
@@ -381,16 +395,35 @@ func (p *Process) isRunning() bool {
381395
382396func (p * Process ) prepareForShutDown () {
383397 // prevent restart during global shutdown or scale down
384- //p.procConf.RestartPolicy.Restart = types.RestartPolicyNo
398+ // p.procConf.RestartPolicy.Restart = types.RestartPolicyNo
385399 p .isStopped .Store (true )
386-
387400}
388401
389402func (p * Process ) onProcessStart () {
390403 if isStringDefined (p .procConf .LogLocation ) {
391404 p .logger .Open (p .getLogPath (), p .procConf .LoggerConfig )
392405 }
393406
407+ for _ , watch := range p .devWatchers {
408+ go func () {
409+ for {
410+ files , ok := watch .sub .Recv ()
411+ if ! ok {
412+ log .
413+ Debug ().
414+ Msg ("watchman sub closed, exiting" )
415+ return
416+ }
417+
418+ if len (files ) == 0 {
419+ continue
420+ }
421+
422+ p .isDevRestart .Store (true )
423+ }
424+ }()
425+ }
426+
394427 p .Lock ()
395428 p .started = true
396429 p .Unlock ()
@@ -457,8 +490,8 @@ func (p *Process) updateProcState() {
457490 p .procState .Name = p .getName ()
458491 }
459492 p .procState .IsRunning = isRunning
460-
461493}
494+
462495func (p * Process ) setStartTime (startTime time.Time ) {
463496 p .timeMutex .Lock ()
464497 defer p .timeMutex .Unlock ()
@@ -663,6 +696,13 @@ func (p *Process) onReadinessCheckEnd(isOk, isFatal bool, err string) {
663696 }
664697}
665698
699+ func (p * Process ) setUpWatchman (watchman * Watchman ) {
700+ for _ , config := range p .procConf .Watch {
701+ recv := watchman .Subscribe (config )
702+ p .devWatchers = append (p .devWatchers , devWatch {recv , config })
703+ }
704+ }
705+
666706func (p * Process ) validateProcess () error {
667707 if isStringDefined (p .procConf .WorkingDir ) {
668708 stat , err := os .Stat (p .procConf .WorkingDir )
0 commit comments