forked from AliyunContainerService/log-pilot
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[release-1.2-starbucks]chore(*): cherrypick stability fix from master (…
…#49) * fix(*): monitor subprocess status (#43) * refactor subprocess start and stop * refactor watch /config/filebeat-output.yml * fix bug * fix bug (cherry picked from commit cae2680) * chore(*): add mount propagation (#45) (cherry picked from commit f0b561a)
- Loading branch information
1 parent
931a1de
commit 354326a
Showing
4 changed files
with
206 additions
and
41 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package main | ||
|
||
import ( | ||
"os/exec" | ||
"syscall" | ||
"time" | ||
|
||
"github.com/caicloud/nirvana/log" | ||
) | ||
|
||
const ( | ||
waitingTime = 60 | ||
) | ||
|
||
type AsyncCmd struct { | ||
cmd *exec.Cmd | ||
waitDone chan struct{} | ||
finished bool | ||
} | ||
|
||
func WrapCmd(cmd *exec.Cmd) *AsyncCmd { | ||
return &AsyncCmd{ | ||
cmd: cmd, | ||
waitDone: make(chan struct{}), | ||
finished: false, | ||
} | ||
} | ||
|
||
func (ac *AsyncCmd) Start() error { | ||
if err := ac.cmd.Start(); err != nil { | ||
return err | ||
} | ||
|
||
go func(ac *AsyncCmd) { | ||
ac.cmd.Wait() | ||
close(ac.waitDone) | ||
ac.finished = true | ||
}(ac) | ||
|
||
return nil | ||
} | ||
|
||
func (ac *AsyncCmd) Stop() error { | ||
log.Infoln("Send TERM signal") | ||
if err := ac.cmd.Process.Signal(syscall.SIGTERM); err != nil { | ||
return err | ||
} | ||
|
||
select { | ||
case <-ac.waitDone: | ||
return nil | ||
case <-time.After(waitingTime * time.Second): | ||
log.Infoln("Kill Process") | ||
if err := ac.cmd.Process.Kill(); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
<-ac.waitDone | ||
return nil | ||
} | ||
|
||
func (ac *AsyncCmd) Exited() bool { | ||
return ac.finished | ||
} |
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,43 @@ | ||
package main | ||
|
||
import ( | ||
"path/filepath" | ||
|
||
"github.com/caicloud/nirvana/log" | ||
"gopkg.in/fsnotify/fsnotify.v1" | ||
) | ||
|
||
const ( | ||
dataDirName = "..data" | ||
) | ||
|
||
// ref_link [https://github.com/jimmidyson/configmap-reload/issues/6#issuecomment-355203620] | ||
// ConfigMap volumes use an atomic writer. You could familarize yourself with | ||
// the mechanic how atomic writes are implemented. In the end you could check | ||
// if the actual change you do in your ConfigMap results in the rename of the | ||
// ..data-symlink (step 9). | ||
// ref_link [https://github.com/kubernetes/kubernetes/blob/6d98cdbbfb055757a9846dee97dafd4177d9a222/pkg/volume/util/atomic_writer.go#L56] | ||
func watchConfigMapUpdate(path string, update func()) error { | ||
w, err := fsnotify.NewWatcher() | ||
if err != nil { | ||
return err | ||
} | ||
if err := w.Add(path); err != nil { | ||
return err | ||
} | ||
|
||
for { | ||
select { | ||
case ev := <-w.Events: | ||
log.Infoln("Event:", ev.String()) | ||
if ev.Op&fsnotify.Create == fsnotify.Create { | ||
if filepath.Base(ev.Name) == dataDirName { | ||
log.Infoln("Configmap updated") | ||
update() | ||
} | ||
} | ||
case err := <-w.Errors: | ||
log.Errorf("Watch error: %v", err) | ||
} | ||
} | ||
} |
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