Allow RebootNow() to wait for disconnection

This commit is contained in:
wpetit 2018-09-20 11:20:35 +02:00
parent f24f697a3d
commit 964e1d713e
2 changed files with 21 additions and 2 deletions

View File

@ -1,6 +1,9 @@
package reach
import (
"sync"
"forge.cadoles.com/Pyxis/golang-socketio"
"github.com/pkg/errors"
)
@ -9,9 +12,21 @@ const (
)
// RebootNow asks the ReachRS module to reboot now
func (u *Updater) RebootNow() error {
func (u *Updater) RebootNow(waitDisconnect bool) error {
var err error
var wg sync.WaitGroup
if waitDisconnect {
wg.Add(1)
err = u.conn.On(gosocketio.OnDisconnection, func(h *gosocketio.Channel) {
u.conn.Off(gosocketio.OnDisconnection)
wg.Done()
})
if err != nil {
return errors.Wrapf(err, "error while binding to '%s' event", gosocketio.OnDisconnection)
}
}
u.logf("sending '%s' event", eventReboot)
if err = u.conn.Emit(eventReboot, nil); err != nil {
@ -19,6 +34,10 @@ func (u *Updater) RebootNow() error {
}
u.logf("'%s' event sent", eventReboot)
if waitDisconnect {
wg.Wait()
}
return err
}

View File

@ -18,7 +18,7 @@ func TestClientReboutNow(t *testing.T) {
t.Fatal(err)
}
if err := client.RebootNow(); err != nil {
if err := client.RebootNow(true); err != nil {
t.Error(err)
}