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
|
|
|
|
2021-09-19 11:59:26 +02:00
|
|
|
"ewintr.nl/gte/internal/storage"
|
|
|
|
"ewintr.nl/gte/internal/task"
|
2021-05-13 08:15:14 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrRecurProcess = errors.New("could not generate tasks from recurrer")
|
2021-05-15 11:46:03 +02:00
|
|
|
|
|
|
|
recurLock sync.Mutex
|
2021-05-13 08:15:14 +02:00
|
|
|
)
|
|
|
|
|
2021-06-25 09:14:27 +02:00
|
|
|
// Recur generates new tasks from a recurring task for a given day
|
2021-05-13 08:15:14 +02:00
|
|
|
type Recur struct {
|
2021-06-25 09:14:27 +02:00
|
|
|
taskRepo *storage.RemoteRepository
|
|
|
|
taskDispatcher *storage.Dispatcher
|
2021-05-13 08:15:14 +02:00
|
|
|
daysAhead int
|
|
|
|
}
|
|
|
|
|
|
|
|
type RecurResult 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 09:14:27 +02:00
|
|
|
func NewRecur(repo *storage.RemoteRepository, disp *storage.Dispatcher, daysAhead int) *Recur {
|
2021-05-13 08:15:14 +02:00
|
|
|
return &Recur{
|
|
|
|
taskRepo: repo,
|
|
|
|
taskDispatcher: disp,
|
|
|
|
daysAhead: daysAhead,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (recur *Recur) Process() (*RecurResult, error) {
|
2021-05-15 11:46:03 +02:00
|
|
|
recurLock.Lock()
|
|
|
|
defer recurLock.Unlock()
|
|
|
|
|
|
|
|
start := time.Now()
|
|
|
|
|
2021-05-13 08:15:14 +02:00
|
|
|
tasks, err := recur.taskRepo.FindAll(task.FOLDER_RECURRING)
|
|
|
|
if err != nil {
|
|
|
|
return &RecurResult{}, fmt.Errorf("%w: %v", ErrRecurProcess, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rDate := task.Today.AddDays(recur.daysAhead)
|
|
|
|
var count int
|
|
|
|
for _, t := range tasks {
|
|
|
|
if t.RecursOn(rDate) {
|
|
|
|
newTask, err := t.GenerateFromRecurrer(rDate)
|
|
|
|
if err != nil {
|
|
|
|
return &RecurResult{}, fmt.Errorf("%w: %v", ErrRecurProcess, err)
|
|
|
|
}
|
|
|
|
if err := recur.taskDispatcher.Dispatch(newTask); err != nil {
|
|
|
|
return &RecurResult{}, fmt.Errorf("%w: %v", ErrRecurProcess, err)
|
|
|
|
}
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &RecurResult{
|
2021-05-15 11:46:03 +02:00
|
|
|
Duration: time.Since(start).String(),
|
|
|
|
Count: count,
|
2021-05-13 08:15:14 +02:00
|
|
|
}, nil
|
|
|
|
}
|