Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support reloading progress and error for dae reload #470

Merged
merged 3 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions cmd/reload.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,27 @@ import (
"strconv"
"strings"
"syscall"
"time"

"github.com/daeuniverse/dae/cmd/internal"
"github.com/daeuniverse/dae/common/consts"
"github.com/spf13/cobra"
)

func readSignalProgressFile() (code byte, content string, err error) {
b, err := os.ReadFile(SignalProgressFilePath)
if err != nil {
return 0, "", err
}
var firstLine string
firstLine, content, _ = strings.Cut(string(b), "\n")
if len(firstLine) != 1 {
return 0, "", fmt.Errorf("unexpected format: %v", string(b))
}
code = firstLine[0]
return code, content, nil
}

var (
abort bool
reloadCmd = &cobra.Command{
Expand All @@ -41,10 +57,40 @@ var (
f.Close()
}
}
// Read the first line of SignalProgressFilePath.
code, _, err := readSignalProgressFile()
if err == nil && code != consts.ReloadDone && code != consts.ReloadError {
// In progress.
fmt.Printf("%v shows another reload operation is in progress.\n", SignalProgressFilePath)
return
}
// Set the progress as ReloadSend.
os.WriteFile(SignalProgressFilePath, []byte{consts.ReloadSend}, 0644)
// Send signal.
if err = syscall.Kill(pid, syscall.SIGUSR1); err != nil {
fmt.Println(err)
os.Exit(1)
}
time.Sleep(500 * time.Millisecond)
code, _, _ = readSignalProgressFile()
if code == consts.ReloadSend {
// Old version dae is running.
goto fallback
}

for {
time.Sleep(200 * time.Millisecond)
code, content, err := readSignalProgressFile()
if err != nil {
// Unexpecetd case.
goto fallback
}
if code == consts.ReloadDone || code == consts.ReloadError {
fmt.Println(content)
return
}
}
fallback:
fmt.Println("OK")
},
}
Expand Down
9 changes: 8 additions & 1 deletion cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (

"github.com/daeuniverse/dae/cmd/internal"
"github.com/daeuniverse/dae/common"
"github.com/daeuniverse/dae/common/consts"
"github.com/daeuniverse/dae/common/subscription"
"github.com/daeuniverse/dae/config"
"github.com/daeuniverse/dae/control"
Expand All @@ -39,7 +40,8 @@ import (
)

const (
PidFilePath = "/var/run/dae.pid"
PidFilePath = "/var/run/dae.pid"
SignalProgressFilePath = "/var/run/dae.progress"
)

var (
Expand Down Expand Up @@ -134,6 +136,7 @@ func Run(log *logrus.Logger, conf *config.Config, externGeoDataDirs []string) (e
if !disablePidFile {
_ = os.WriteFile(PidFilePath, []byte(strconv.Itoa(os.Getpid())), 0644)
}
_ = os.WriteFile(SignalProgressFilePath, []byte{consts.ReloadDone}, 0644)
}()
control.GetDaeNetns().With(func() error {
if listener, err = c.ListenAndServe(readyChan, conf.Global.TproxyPort); err != nil {
Expand Down Expand Up @@ -167,6 +170,7 @@ loop:
}()
<-readyChan
sdnotify.Ready()
_ = os.WriteFile(SignalProgressFilePath, append([]byte{consts.ReloadDone}, []byte("\nOK")...), 0644)
log.Warnln("[Reload] Finished")
} else {
// Listening error.
Expand All @@ -183,6 +187,7 @@ loop:
log.Warnln("[Reload] Received reload signal; prepare to reload")
}
sdnotify.Reloading()
_ = os.WriteFile(SignalProgressFilePath, []byte{consts.ReloadProcessing}, 0644)

// Load new config.
abortConnections = os.Remove(AbortFile) == nil
Expand All @@ -196,6 +201,7 @@ loop:
"err": err,
}).Errorln("[Reload] Failed to reload")
sdnotify.Ready()
_ = os.WriteFile(SignalProgressFilePath, append([]byte{consts.ReloadError}, []byte("\n"+err.Error())...), 0644)
continue
}
newConf.Global = deepcopy.Copy(conf.Global).(config.Global)
Expand All @@ -210,6 +216,7 @@ loop:
"err": err,
}).Errorln("[Reload] Failed to reload")
sdnotify.Ready()
_ = os.WriteFile(SignalProgressFilePath, append([]byte{consts.ReloadError}, []byte("\n"+err.Error())...), 0644)
continue
}
log.Infof("Include config files: [%v]", strings.Join(includes, ", "))
Expand Down
8 changes: 8 additions & 0 deletions common/consts/reload.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package consts

const (
ReloadSend = '0' + iota
ReloadProcessing
ReloadDone
ReloadError
)
Loading