gte/internal/process/inbox.go

62 lines
1.1 KiB
Go
Raw Normal View History

2021-05-13 08:15:14 +02:00
package process
import (
"errors"
"fmt"
2021-05-15 11:46:03 +02:00
"sync"
"time"
2021-05-13 08:15:14 +02:00
"git.ewintr.nl/gte/internal/task"
)
var (
ErrInboxProcess = errors.New("could not process inbox")
2021-05-15 11:46:03 +02:00
inboxLock sync.Mutex
2021-05-13 08:15:14 +02:00
)
type Inbox struct {
2021-06-25 07:48:58 +02:00
taskRepo *task.RemoteRepository
2021-05-13 08:15:14 +02:00
}
type InboxResult struct {
2021-05-15 11:46:03 +02:00
Duration string `json:"duration"`
Count int `json:"count"`
2021-05-13 08:15:14 +02:00
}
2021-06-25 07:48:58 +02:00
func NewInbox(repo *task.RemoteRepository) *Inbox {
2021-05-13 08:15:14 +02:00
return &Inbox{
taskRepo: repo,
}
}
func (inbox *Inbox) Process() (*InboxResult, error) {
2021-05-15 11:46:03 +02:00
inboxLock.Lock()
defer inboxLock.Unlock()
start := time.Now()
2021-05-13 08:15:14 +02:00
tasks, err := inbox.taskRepo.FindAll(task.FOLDER_INBOX)
if err != nil {
return &InboxResult{}, fmt.Errorf("%w: %v", ErrInboxProcess, err)
}
var cleanupNeeded bool
for _, t := range tasks {
2021-06-04 10:45:56 +02:00
if err := inbox.taskRepo.Update(t); err != nil {
return &InboxResult{}, fmt.Errorf("%w: %v", ErrInboxProcess, err)
2021-05-13 08:15:14 +02:00
}
2021-06-04 10:45:56 +02:00
cleanupNeeded = true
2021-05-13 08:15:14 +02:00
}
if cleanupNeeded {
if err := inbox.taskRepo.CleanUp(); err != nil {
return &InboxResult{}, fmt.Errorf("%w: %v", ErrInboxProcess, err)
}
}
return &InboxResult{
2021-05-15 11:46:03 +02:00
Duration: time.Since(start).String(),
Count: len(tasks),
2021-05-13 08:15:14 +02:00
}, nil
}