feat(controller): hot-reload the notification pipes
Email, ntfy and webhook kept their settings in a plain struct field copied at construction, so editing controllerMethod in config.json wrote the file and replaced the in-memory configuration while the running channels stayed on their boot values. Add Reload to the Controller interface and implement it for the three notification pipes; Manager.ReloadAll fans an update out to every registered controller and is wired into the reload hook from main. Reload runs on the goroutine serving the settings update while the send methods run on the status-change and incoming-webhook goroutines, so storing the new settings in a plain field would be a data race. Each pipe publishes its settings — and the HTTP client derived from them — through atomic pointers, and every send takes one snapshot so a reload landing mid-send cannot split it across two configurations. Only a change to networkUseProxy rebuilds the client; everything else is read from the settings at send time, so a reload that changes nothing relevant leaves the client alone. Email needed one more fix: gomail exposes its dial hook as a package-level variable with no unset, and the constructor only installed the proxy dialer when the flag was set, so turning networkUseProxy off left SMTP tunnelled through a proxy. applyEmailProxy now restores net.DialTimeout, which is gomail's own default. QQ (Napcat) and Telegram implement Reload because the interface requires it, but neither can apply a change in place: the NapCat client is stopped through a sync.Once and the Telegram polling context is created with the controller, so both need to be rebuilt and swapped into the manager. Rather than no-op silently and let an edit look applied, they log a warning while their settings diverge. That rebuild is the next step.
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"nukumizu-backend/config"
|
||||
@@ -15,17 +16,33 @@ import (
|
||||
)
|
||||
|
||||
// NtfyController handles notifications via ntfy.sh or a self-hosted ntfy server.
|
||||
//
|
||||
// Both fields are held behind atomic pointers rather than in plain fields:
|
||||
// Reload replaces them on the settings-update goroutine while the send methods
|
||||
// read them on the status-change and incoming-webhook goroutines.
|
||||
type NtfyController struct {
|
||||
cfg config.NtfyConfig
|
||||
httpClient *http.Client
|
||||
cfg atomic.Pointer[config.NtfyConfig]
|
||||
httpClient atomic.Pointer[http.Client]
|
||||
}
|
||||
|
||||
// NewNtfyController creates a new Ntfy controller.
|
||||
func NewNtfyController(cfg config.NtfyConfig) *NtfyController {
|
||||
return &NtfyController{
|
||||
cfg: cfg,
|
||||
httpClient: netproxy.HTTPClient(cfg.NetworkUseProxy, 10*time.Second),
|
||||
}
|
||||
n := &NtfyController{}
|
||||
n.cfg.Store(&cfg)
|
||||
n.httpClient.Store(netproxy.HTTPClient(cfg.NetworkUseProxy, 10*time.Second))
|
||||
return n
|
||||
}
|
||||
|
||||
// settings returns the configuration currently in effect. The value it points
|
||||
// at is never mutated after being stored, so a caller can hold the pointer for
|
||||
// one whole send without a concurrent Reload disturbing it.
|
||||
func (n *NtfyController) settings() *config.NtfyConfig {
|
||||
return n.cfg.Load()
|
||||
}
|
||||
|
||||
// client returns the HTTP client built for the current proxy setting.
|
||||
func (n *NtfyController) client() *http.Client {
|
||||
return n.httpClient.Load()
|
||||
}
|
||||
|
||||
// Name returns the controller name.
|
||||
@@ -35,7 +52,7 @@ func (n *NtfyController) Name() string {
|
||||
|
||||
// Start initializes the Ntfy controller.
|
||||
func (n *NtfyController) Start() error {
|
||||
if !n.cfg.Enabled {
|
||||
if !n.settings().Enabled {
|
||||
postLog.Info("Ntfy controller is disabled")
|
||||
return nil
|
||||
}
|
||||
@@ -48,26 +65,50 @@ func (n *NtfyController) Stop() {
|
||||
postLog.Info("Ntfy controller stopped")
|
||||
}
|
||||
|
||||
// Reload applies the current configuration. Everything the publisher reads is
|
||||
// taken from the settings at send time, so only a change to the proxy flag
|
||||
// needs more than the swap: that one is baked into the HTTP client's transport
|
||||
// when the client is built.
|
||||
func (n *NtfyController) Reload() {
|
||||
global := config.Current()
|
||||
if global == nil {
|
||||
return
|
||||
}
|
||||
|
||||
current := n.settings()
|
||||
updated := global.ControllerMethod.Ntfy
|
||||
if *current == updated {
|
||||
return
|
||||
}
|
||||
|
||||
if current.NetworkUseProxy != updated.NetworkUseProxy {
|
||||
n.httpClient.Store(netproxy.HTTPClient(updated.NetworkUseProxy, 10*time.Second))
|
||||
}
|
||||
n.cfg.Store(&updated)
|
||||
postLog.Debug("Ntfy controller reloaded")
|
||||
}
|
||||
|
||||
// IsEnabled returns whether the controller is enabled.
|
||||
func (n *NtfyController) IsEnabled() bool {
|
||||
return n.cfg.Enabled
|
||||
return n.settings().Enabled
|
||||
}
|
||||
|
||||
// IsMarkdown returns whether the channel renders Markdown, per its markdown
|
||||
// setting in config.json.
|
||||
func (n *NtfyController) IsMarkdown() bool {
|
||||
return n.cfg.Markdown
|
||||
return n.settings().Markdown
|
||||
}
|
||||
|
||||
// SendStatusChange sends a status change notification via Ntfy.
|
||||
func (n *NtfyController) SendStatusChange(change node.StatusChange) error {
|
||||
if !n.cfg.Enabled {
|
||||
s := n.settings()
|
||||
if !s.Enabled {
|
||||
return nil
|
||||
}
|
||||
|
||||
cfg := config.Current()
|
||||
params := template.BuildParamsFromStatusChange(change)
|
||||
message := template.Render(cfg.ControllerMessage.ServerStatusChanged, params, n.cfg.Markdown)
|
||||
message := template.Render(cfg.ControllerMessage.ServerStatusChanged, params, s.Markdown)
|
||||
|
||||
title := fmt.Sprintf("Server %s: %s", change.Name, change.Event)
|
||||
return n.publish(title, message)
|
||||
@@ -75,26 +116,28 @@ func (n *NtfyController) SendStatusChange(change node.StatusChange) error {
|
||||
|
||||
// SendServerList sends the server list via Ntfy.
|
||||
func (n *NtfyController) SendServerList(onlineServers, offlineServers string) error {
|
||||
if !n.cfg.Enabled {
|
||||
s := n.settings()
|
||||
if !s.Enabled {
|
||||
return nil
|
||||
}
|
||||
|
||||
cfg := config.Current()
|
||||
params := template.BuildParamsFromServerList()
|
||||
message := template.Render(cfg.ControllerMessage.ServerList, params, n.cfg.Markdown)
|
||||
message := template.Render(cfg.ControllerMessage.ServerList, params, s.Markdown)
|
||||
|
||||
return n.publish("Server List", message)
|
||||
}
|
||||
|
||||
// SendExecuteResult sends a command execution result via Ntfy.
|
||||
func (n *NtfyController) SendExecuteResult(serverName, serverUUID, command, result string) error {
|
||||
if !n.cfg.Enabled {
|
||||
s := n.settings()
|
||||
if !s.Enabled {
|
||||
return nil
|
||||
}
|
||||
|
||||
cfg := config.Current()
|
||||
params := template.BuildParamsFromExecResult(serverName, serverUUID, command, result)
|
||||
message := template.Render(cfg.ControllerMessage.ServerExecuteResult, params, n.cfg.Markdown)
|
||||
message := template.Render(cfg.ControllerMessage.ServerExecuteResult, params, s.Markdown)
|
||||
|
||||
title := fmt.Sprintf("Command Result: %s on %s", command, serverName)
|
||||
return n.publish(title, message)
|
||||
@@ -103,21 +146,24 @@ func (n *NtfyController) SendExecuteResult(serverName, serverUUID, command, resu
|
||||
// SendAlert sends an alert submitted through the incoming webhook API to the
|
||||
// configured topic.
|
||||
func (n *NtfyController) SendAlert(alert controller.Alert) error {
|
||||
if !n.cfg.Enabled {
|
||||
s := n.settings()
|
||||
if !s.Enabled {
|
||||
return nil
|
||||
}
|
||||
|
||||
return n.publish(alert.Subject, alert.Render(n.cfg.Markdown))
|
||||
return n.publish(alert.Subject, alert.Render(s.Markdown))
|
||||
}
|
||||
|
||||
func (n *NtfyController) publish(title, message string) error {
|
||||
serverURL := n.cfg.Server
|
||||
s := n.settings()
|
||||
|
||||
serverURL := s.Server
|
||||
if serverURL == "" {
|
||||
serverURL = "https://ntfy.sh"
|
||||
}
|
||||
serverURL = strings.TrimRight(serverURL, "/")
|
||||
|
||||
publishURL := fmt.Sprintf("%s/%s", serverURL, n.cfg.Topic)
|
||||
publishURL := fmt.Sprintf("%s/%s", serverURL, s.Topic)
|
||||
|
||||
req, err := http.NewRequest("POST", publishURL, strings.NewReader(message))
|
||||
if err != nil {
|
||||
@@ -125,14 +171,14 @@ func (n *NtfyController) publish(title, message string) error {
|
||||
}
|
||||
|
||||
req.Header.Set("Title", title)
|
||||
if n.cfg.Priority != "" && n.cfg.Priority != "default" {
|
||||
req.Header.Set("Priority", n.cfg.Priority)
|
||||
if s.Priority != "" && s.Priority != "default" {
|
||||
req.Header.Set("Priority", s.Priority)
|
||||
}
|
||||
if n.cfg.Token != "" {
|
||||
req.Header.Set("Authorization", "Bearer "+n.cfg.Token)
|
||||
if s.Token != "" {
|
||||
req.Header.Set("Authorization", "Bearer "+s.Token)
|
||||
}
|
||||
|
||||
resp, err := n.httpClient.Do(req)
|
||||
resp, err := n.client().Do(req)
|
||||
if err != nil {
|
||||
postLog.Warning("Failed to publish to ntfy: " + err.Error())
|
||||
return err
|
||||
@@ -143,6 +189,6 @@ func (n *NtfyController) publish(title, message string) error {
|
||||
postLog.Warning(fmt.Sprintf("Ntfy publish returned status %d", resp.StatusCode))
|
||||
}
|
||||
|
||||
postLog.Debug("Ntfy notification sent to topic: " + n.cfg.Topic)
|
||||
postLog.Debug("Ntfy notification sent to topic: " + s.Topic)
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user