refactor: creating image process to handle processing of images
Decoupling this from the DB, it's a good step. Not yet perfect however.
This commit is contained in:
65
backend/imageprocessor/notification.go
Normal file
65
backend/imageprocessor/notification.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package imageprocessor
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
const (
|
||||
IMAGE_TYPE = "image"
|
||||
LIST_TYPE = "list"
|
||||
)
|
||||
|
||||
type ImageNotification struct {
|
||||
Type string
|
||||
|
||||
ImageID uuid.UUID
|
||||
ImageName string
|
||||
|
||||
Status string
|
||||
}
|
||||
|
||||
func getImageNotification(image ImageNotification) Notification {
|
||||
return Notification{
|
||||
image: &image,
|
||||
}
|
||||
}
|
||||
|
||||
type ListNotification struct {
|
||||
Type string
|
||||
|
||||
ListID uuid.UUID
|
||||
Name string
|
||||
|
||||
Status string
|
||||
}
|
||||
|
||||
type Notification struct {
|
||||
image *ImageNotification
|
||||
list *ListNotification
|
||||
}
|
||||
|
||||
func GetListNotification(list ListNotification) Notification {
|
||||
return Notification{
|
||||
list: &list,
|
||||
}
|
||||
}
|
||||
|
||||
func (n Notification) MarshalJSON() ([]byte, error) {
|
||||
if n.image != nil {
|
||||
return json.Marshal(n.image)
|
||||
}
|
||||
|
||||
if n.list != nil {
|
||||
return json.Marshal(n.list)
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("no image or list present")
|
||||
}
|
||||
|
||||
func (n *Notification) UnmarshalJSON(data []byte) error {
|
||||
return fmt.Errorf("unimplemented")
|
||||
}
|
||||
|
||||
66
backend/imageprocessor/processor.go
Normal file
66
backend/imageprocessor/processor.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package imageprocessor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"screenmark/screenmark/models"
|
||||
"screenmark/screenmark/notifier"
|
||||
|
||||
"github.com/charmbracelet/log"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type DbImageProcessor struct {
|
||||
logger *log.Logger
|
||||
images models.ImageModel
|
||||
|
||||
incomingImages chan string
|
||||
|
||||
notifier *notifier.Notifier[Notification]
|
||||
}
|
||||
|
||||
func (p *DbImageProcessor) processImage(incomingMsg string) error {
|
||||
imageStringUUID := incomingMsg[0:36]
|
||||
status := incomingMsg[36:]
|
||||
|
||||
imageUUID, err := uuid.Parse(imageStringUUID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("parsing: %w", err)
|
||||
}
|
||||
|
||||
processingImage, err := p.images.GetToProcess(context.Background(), imageUUID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("get to processes: %w", err)
|
||||
}
|
||||
|
||||
p.logger.Info("Update", "id", imageStringUUID, "status", status)
|
||||
|
||||
notification := getImageNotification(ImageNotification{
|
||||
Type: IMAGE_TYPE,
|
||||
ImageID: processingImage.ImageID,
|
||||
ImageName: processingImage.Image.ImageName,
|
||||
Status: status,
|
||||
})
|
||||
|
||||
return p.notifier.SendAndCreate(processingImage.UserID.String(), notification)
|
||||
}
|
||||
|
||||
func (p *DbImageProcessor) Work() {
|
||||
for incomingMsg := range p.incomingImages {
|
||||
go func() {
|
||||
err := p.processImage(incomingMsg)
|
||||
if err != nil {
|
||||
p.logger.Error("processing image", "err", err)
|
||||
}
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
||||
func NewImageProcessor(logger *log.Logger, imageModel models.ImageModel, incoming chan string, notifier *notifier.Notifier[Notification]) *DbImageProcessor {
|
||||
return &DbImageProcessor{
|
||||
logger: logger,
|
||||
images: imageModel,
|
||||
incomingImages: incoming,
|
||||
notifier: notifier,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user