feat(env_group): add post-sync action to environment and stream configurations #725

This commit is contained in:
Jacky 2025-04-05 10:55:26 +00:00
parent c0f0980e9e
commit 94fcbf6362
No known key found for this signature in database
GPG key ID: 215C21B10DF38B4D
31 changed files with 606 additions and 337 deletions

View file

@ -17,7 +17,7 @@ import (
)
// Save saves a site configuration file
func Save(name string, content string, overwrite bool, syncNodeIds []uint64) (err error) {
func Save(name string, content string, overwrite bool, syncNodeIds []uint64, postAction string) (err error) {
path := nginx.GetConfPath("streams-available", name)
if !overwrite && helper.FileExists(path) {
return ErrDstFileExists
@ -37,10 +37,11 @@ func Save(name string, content string, overwrite bool, syncNodeIds []uint64) (er
return fmt.Errorf("%s", output)
}
output = nginx.Reload()
if nginx.GetLogLevel(output) > nginx.Warn {
return fmt.Errorf("%s", output)
if postAction == model.PostSyncActionReloadNginx {
output = nginx.Reload()
if nginx.GetLogLevel(output) > nginx.Warn {
return fmt.Errorf("%s", output)
}
}
}
@ -60,13 +61,17 @@ func Save(name string, content string, overwrite bool, syncNodeIds []uint64) (er
}
func syncSave(name string, content string) {
nodes := getSyncNodes(name)
nodes, postSyncAction := getSyncData(name)
wg := &sync.WaitGroup{}
wg.Add(len(nodes))
// Map to track successful nodes for potential post-sync action
successfulNodes := make([]*model.Environment, 0)
var nodesMutex sync.Mutex
for _, node := range nodes {
go func() {
go func(node *model.Environment) {
defer func() {
if err := recover(); err != nil {
buf := make([]byte, 1024)
@ -81,8 +86,9 @@ func syncSave(name string, content string) {
resp, err := client.R().
SetHeader("X-Node-Secret", node.Token).
SetBody(map[string]interface{}{
"content": content,
"overwrite": true,
"content": content,
"overwrite": true,
"post_action": postSyncAction,
}).
Post(fmt.Sprintf("/api/streams/%s", name))
if err != nil {
@ -95,12 +101,17 @@ func syncSave(name string, content string) {
}
notification.Success("Save Remote Stream Success", "Save stream %{name} to %{node} successfully", NewSyncResult(node.Name, name, resp))
// Track successful sync for post-sync action
nodesMutex.Lock()
successfulNodes = append(successfulNodes, node)
nodesMutex.Unlock()
// Check if the site is enabled, if so then enable it on the remote node
enabledConfigFilePath := nginx.GetConfPath("streams-enabled", name)
if helper.FileExists(enabledConfigFilePath) {
syncEnable(name)
}
}()
}(node)
}
wg.Wait()