feat: mutex, graceful shutdown
This commit is contained in:
@@ -57,10 +57,13 @@ type wholeScreenCapturer struct {
|
|||||||
latest string
|
latest string
|
||||||
done chan struct{}
|
done chan struct{}
|
||||||
sync.WaitGroup
|
sync.WaitGroup
|
||||||
|
mu sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get implements ScreenCapturer.
|
// Get implements ScreenCapturer.
|
||||||
func (w *wholeScreenCapturer) Get() (filepath string, err error) {
|
func (w *wholeScreenCapturer) Get() (filepath string, err error) {
|
||||||
|
w.mu.RLock()
|
||||||
|
defer w.mu.Unlock()
|
||||||
if !w.initialized {
|
if !w.initialized {
|
||||||
return "", ErrNotInitialized
|
return "", ErrNotInitialized
|
||||||
}
|
}
|
||||||
@@ -185,6 +188,8 @@ func (w *wholeScreenCapturer) captureAndSave() (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (w *wholeScreenCapturer) addToBuffer(fp string) {
|
func (w *wholeScreenCapturer) addToBuffer(fp string) {
|
||||||
|
w.mu.Lock()
|
||||||
|
defer w.mu.Unlock()
|
||||||
w.files = append(w.files, fp)
|
w.files = append(w.files, fp)
|
||||||
w.latest = fp
|
w.latest = fp
|
||||||
|
|
||||||
@@ -210,8 +215,25 @@ func NewWholeScreenCapturer(lc fx.Lifecycle, config *config.Config, log *logger.
|
|||||||
done: make(chan struct{}),
|
done: make(chan struct{}),
|
||||||
}
|
}
|
||||||
lc.Append(fx.StopHook(func(ctx context.Context) error {
|
lc.Append(fx.StopHook(func(ctx context.Context) error {
|
||||||
log.Debug("Stopping capturer (fx hook)")
|
if (!capturer.initialized) {
|
||||||
return capturer.Stop()
|
log.Debug("wholescreencapturer not initialized, nothing to do")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Debug("stopping wholescreencapturer")
|
||||||
|
|
||||||
|
err := capturer.Stop()
|
||||||
|
if err != nil {
|
||||||
|
log.Error("failed to stop wholescreencapturer gracefully")
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = os.RemoveAll(capturer.tempDirectory)
|
||||||
|
if err != nil {
|
||||||
|
log.Error("failed to remove temp directory")
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}))
|
}))
|
||||||
return capturer
|
return capturer
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user