2 Commits

Author SHA1 Message Date
ae3fa08199 frontend method to refresh image 2025-09-14 17:30:17 +01:00
daa2a292bf refactor: creating image process to handle processing of images
Decoupling this from the DB, it's a good step.

Not yet perfect however.
2025-09-14 17:17:54 +01:00
5 changed files with 27 additions and 21 deletions

View File

@ -12,7 +12,6 @@ import (
"screenmark/screenmark/limits"
"screenmark/screenmark/middleware"
"screenmark/screenmark/models"
"screenmark/screenmark/notifier"
"strconv"
"sync"
"time"
@ -174,7 +173,7 @@ func ListenNewStackEvents(db *sql.DB) {
}
}
func ListenProcessingStackStatus(db *sql.DB, stacks models.ListModel, notifier *notifier.Notifier[imageprocessor.Notification]) {
func ListenProcessingStackStatus(db *sql.DB, stacks models.ListModel, notifier *Notifier[imageprocessor.Notification]) {
listener := pq.NewListener(os.Getenv("DB_CONNECTION"), time.Second, time.Second, func(event pq.ListenerEventType, err error) {
if err != nil {
panic(err)
@ -225,10 +224,10 @@ func ListenProcessingStackStatus(db *sql.DB, stacks models.ListModel, notifier *
*
* What is a reasonable default? Close the channel after 1 minute of inactivity?
*/
func CreateEventsHandler(notifierr *notifier.Notifier[imageprocessor.Notification]) http.HandlerFunc {
func CreateEventsHandler(notifier *Notifier[imageprocessor.Notification]) http.HandlerFunc {
counter := 0
userSplitters := make(map[string]*notifier.ChannelSplitter[imageprocessor.Notification])
userSplitters := make(map[string]*ChannelSplitter[imageprocessor.Notification])
return func(w http.ResponseWriter, r *http.Request) {
_userId := r.Context().Value(middleware.USER_ID).(uuid.UUID)
@ -244,14 +243,14 @@ func CreateEventsHandler(notifierr *notifier.Notifier[imageprocessor.Notificatio
w.Header().Set("Connection", "keep-alive")
// w.(http.Flusher).Flush()
if _, exists := notifierr.Listeners[userId]; !exists {
notifierr.Create(userId)
if _, exists := notifier.Listeners[userId]; !exists {
notifier.Create(userId)
}
userNotifications := notifierr.Listeners[userId]
userNotifications := notifier.Listeners[userId]
if _, exists := userSplitters[userId]; !exists {
splitter := notifier.NewChannelSplitter(userNotifications)
splitter := NewChannelSplitter(userNotifications)
userSplitters[userId] = &splitter
splitter.Listen()

View File

@ -4,7 +4,6 @@ import (
"context"
"fmt"
"screenmark/screenmark/models"
"screenmark/screenmark/notifier"
"github.com/charmbracelet/log"
"github.com/google/uuid"
@ -16,7 +15,7 @@ type DbImageProcessor struct {
incomingImages chan string
notifier *notifier.Notifier[Notification]
outgoing func(userID string, n Notification)
}
func (p *DbImageProcessor) processImage(incomingMsg string) error {
@ -42,7 +41,9 @@ func (p *DbImageProcessor) processImage(incomingMsg string) error {
Status: status,
})
return p.notifier.SendAndCreate(processingImage.UserID.String(), notification)
p.outgoing(processingImage.UserID.String(), notification)
return nil
}
func (p *DbImageProcessor) Work() {
@ -56,11 +57,11 @@ func (p *DbImageProcessor) Work() {
}
}
func NewImageProcessor(logger *log.Logger, imageModel models.ImageModel, incoming chan string, notifier *notifier.Notifier[Notification]) *DbImageProcessor {
func NewImageProcessor(logger *log.Logger, imageModel models.ImageModel, incoming chan string, outgoing func(userID string, n Notification)) *DbImageProcessor {
return &DbImageProcessor{
logger: logger,
images: imageModel,
incomingImages: incoming,
notifier: notifier,
outgoing: outgoing,
}
}

View File

@ -1,4 +1,4 @@
package notifier
package main
import (
"errors"
@ -67,9 +67,12 @@ type ChannelSplitter[TNotification any] struct {
func (s *ChannelSplitter[TNotification]) Listen() {
go func() {
for msg := range s.ch {
for _, v := range s.Listeners {
v <- msg
for {
select {
case msg := <-s.ch:
for _, v := range s.Listeners {
v <- msg
}
}
}
}()

View File

@ -1,4 +1,4 @@
package notifier
package main
import (
"testing"

View File

@ -9,7 +9,6 @@ import (
"screenmark/screenmark/images"
"screenmark/screenmark/limits"
"screenmark/screenmark/models"
"screenmark/screenmark/notifier"
"screenmark/screenmark/stacks"
ourmiddleware "screenmark/screenmark/middleware"
@ -32,7 +31,7 @@ func setupRouter(db *sql.DB) chi.Router {
limitsManager := limits.CreateLimitsManager(db)
notifier := notifier.NewNotifier[imageprocessor.Notification](10)
notifier := NewNotifier[imageprocessor.Notification](10)
// TODO: should extract these into a notification manager
// And actually make them the same code.
@ -43,7 +42,11 @@ func setupRouter(db *sql.DB) chi.Router {
logger := createLogger("Image Processor", os.Stdout)
processingChan := getProcessingImageStatusChannel(db)
imageProcessor := imageprocessor.NewImageProcessor(logger, imageModel, processingChan, &notifier)
imageProcessor := imageprocessor.NewImageProcessor(logger, imageModel, processingChan, func(userID string, n imageprocessor.Notification) {
if err := notifier.SendAndCreate(userID, n); err != nil {
logger.Error("sending notification", "err", err)
}
})
go imageProcessor.Work()