internal/router: wait ntp synced on Synology

This commit is contained in:
Cuong Manh Le authored and Cuong Manh Le committed 2023-08-11 20:28:03 +07:00
1 parent d292e03d1b
commit 32219e7d32
5 files changed
+65 -56

No files matched your search

+22 -2
View File
@@ -1,16 +1,20 @@
package ntp
import (
"bytes"
"context"
"errors"
"fmt"
"os/exec"
"time"
"github.com/Control-D-Inc/ctrld/internal/router/nvram"
"tailscale.com/logtail/backoff"
"github.com/Control-D-Inc/ctrld/internal/router/nvram"
)
func Wait() error {
// WaitNvram waits NTP synced by checking "ntp_ready" value using nvram.
func WaitNvram() error {
// Wait until `ntp_ready=1` set.
b := backoff.NewBackoff("ntp.Wait", func(format string, args ...any) {}, 10*time.Second)
for {
@@ -24,3 +28,19 @@ func Wait() error {
b.BackOff(context.Background(), errors.New("ntp not ready"))
}
}
// WaitUpstart waits NTP synced by checking upstart task "ntpsync" is in "stop/waiting" state.
func WaitUpstart() error {
// Wait until `initctl status ntpsync` returns stop state.
b := backoff.NewBackoff("ntp.WaitUpstart", func(format string, args ...any) {}, 10*time.Second)
for {
out, err := exec.Command("initctl", "status", "ntpsync").CombinedOutput()
if err != nil {
return fmt.Errorf("exec.Command: %w", err)
}
if bytes.Contains(out, []byte("stop/waiting")) {
return nil
}
b.BackOff(context.Background(), errors.New("ntp not ready"))
}
}