refactor: system processsing status pub/sub

This commit is contained in:
Jacky 2025-04-27 09:26:46 +00:00
parent b5ee43b2b0
commit 9ee84dd138
No known key found for this signature in database
GPG key ID: 215C21B10DF38B4D
50 changed files with 2239 additions and 2306 deletions

66
api/system/processing.go Normal file
View file

@ -0,0 +1,66 @@
package system
import (
"time"
"io"
"github.com/0xJacky/Nginx-UI/api"
"github.com/0xJacky/Nginx-UI/internal/cache"
"github.com/0xJacky/Nginx-UI/internal/cert"
"github.com/gin-gonic/gin"
)
type ProcessingStatus struct {
IndexScanning bool `json:"index_scanning"`
AutoCertProcessing bool `json:"auto_cert_processing"`
}
// GetProcessingStatus is an SSE endpoint that sends real-time processing status updates
func GetProcessingStatus(c *gin.Context) {
api.SetSSEHeaders(c)
notify := c.Writer.CloseNotify()
indexScanning := cache.SubscribeScanningStatus()
defer cache.UnsubscribeScanningStatus(indexScanning)
autoCert := cert.SubscribeProcessingStatus()
defer cert.UnsubscribeProcessingStatus(autoCert)
// Track current status
status := ProcessingStatus{
IndexScanning: false,
AutoCertProcessing: false,
}
sendStatus := func() {
c.Stream(func(w io.Writer) bool {
c.SSEvent("message", status)
return false
})
}
for {
select {
case indexStatus, ok := <-indexScanning:
if !ok {
return
}
status.IndexScanning = indexStatus
sendStatus()
case certStatus, ok := <-autoCert:
if !ok {
return
}
status.AutoCertProcessing = certStatus
sendStatus()
case <-time.After(30 * time.Second):
c.Stream(func(w io.Writer) bool {
c.SSEvent("heartbeat", "")
return false
})
case <-notify:
// Client disconnected
return
}
}
}