1
Fork 0

Throttle queue notifications

This commit is contained in:
viktorstrate 2020-06-23 16:46:38 +02:00
parent 23461ee707
commit 66a4288ec9
1 changed files with 12 additions and 5 deletions

View File

@ -5,10 +5,12 @@ import (
"fmt" "fmt"
"log" "log"
"sync" "sync"
"time"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/viktorstrate/photoview/api/graphql/models" "github.com/viktorstrate/photoview/api/graphql/models"
"github.com/viktorstrate/photoview/api/graphql/notification" "github.com/viktorstrate/photoview/api/graphql/notification"
"github.com/viktorstrate/photoview/api/utils"
) )
type ScannerJob struct { type ScannerJob struct {
@ -48,6 +50,9 @@ func InitializeScannerQueue(db *sql.DB) {
} }
func (queue *ScannerQueue) startBackgroundWorker() { func (queue *ScannerQueue) startBackgroundWorker() {
notifyThrottle := utils.NewThrottle(500 * time.Millisecond)
for { for {
log.Println("Queue waiting") log.Println("Queue waiting")
<-queue.idle_chan <-queue.idle_chan
@ -95,12 +100,14 @@ func (queue *ScannerQueue) startBackgroundWorker() {
Positive: true, Positive: true,
}) })
} else { } else {
notifyThrottle.Trigger(func() {
notification.BroadcastNotification(&models.Notification{ notification.BroadcastNotification(&models.Notification{
Key: "global-scanner-progress", Key: "global-scanner-progress",
Type: models.NotificationTypeMessage, Type: models.NotificationTypeMessage,
Header: fmt.Sprintf("Scanning photos"), Header: fmt.Sprintf("Scanning photos"),
Content: fmt.Sprintf("%d jobs in progress\n%d jobs waiting", in_progress_length, up_next_length), Content: fmt.Sprintf("%d jobs in progress\n%d jobs waiting", in_progress_length, up_next_length),
}) })
})
} }
} }