Files
aksdb 2bcf2d69a1 🐛 Stop stale entries without overwriting a concurrent stop
The auto-stop listed the running entries and later saved the whole stale
row, so a stop pushed by a client in between was overwritten and then
answered as stale. Each candidate is now loaded again inside its own
transaction and only saved while it is still running and not deleted.
The end of the day uses the timezone effective on the day of the entry,
and entries without a real day_key are skipped with a debug message
instead of an error every minute.
2026-12-01 16:49:15 +01:00

96 lines
2.8 KiB
Go

package tracking
import (
"database/sql"
"errors"
"time"
"github.com/pocketbase/pocketbase/core"
)
// AutoStopStalled stops every running entry whose day lies before today in the
// timezone of its owner. The entry ends at 23:59:59.999 of its own day in the
// timezone effective on that day and is flagged as auto stopped. It returns the
// number of stopped entries. A single entry that cannot be saved is logged and
// skipped.
func AutoStopStalled(app core.App, now time.Time) (int, error) {
entries, err := stalledEntries(app, now)
if err != nil {
return 0, err
}
stopped := 0
for _, entry := range entries {
if stopStalled(app, entry.Id, now) {
stopped++
}
}
return stopped, nil
}
// stalledEntries lists the running entries whose day lies before today in the
// timezone of their owner. Entries whose day_key is not a real date are left
// out: they have no end of day and would otherwise be reported every minute.
func stalledEntries(app core.App, now time.Time) ([]*core.Record, error) {
running, err := app.FindRecordsByFilter("entries", "deleted = false && end_time = 0", "start_time", 0, 0)
if err != nil {
return nil, err
}
today := now.UTC().Format(dayKeyLayout)
locations := map[string]*time.Location{}
var stalled []*core.Record
for _, entry := range running {
dayKey := entry.GetString("day_key")
if !validDayKey(dayKey) {
app.Logger().Debug("auto-stop skipped entry without a real day_key", "id", entry.Id, "day_key", dayKey)
continue
}
owner := entry.GetString("owner")
loc, ok := locations[owner]
if !ok {
loc = location(app, owner, today)
locations[owner] = loc
}
if dayKey < now.In(loc).Format(dayKeyLayout) {
stalled = append(stalled, entry)
}
}
return stalled, nil
}
// stopStalled ends the entry id at the end of its day and reports whether it was
// stopped. The entry is loaded again inside the transaction so that a stop or a
// delete pushed by the client since the entry was listed is not overwritten.
func stopStalled(app core.App, id string, now time.Time) bool {
stopped := false
err := app.RunInTransaction(func(tx core.App) error {
entry, err := tx.FindRecordById("entries", id)
if errors.Is(err, sql.ErrNoRows) {
return nil
}
if err != nil {
return err
}
if entry.GetInt64("end_time") != 0 || entry.GetBool("deleted") {
return nil
}
dayKey := entry.GetString("day_key")
endMs, err := endOfDayMs(dayKey, location(tx, entry.GetString("owner"), dayKey))
if err != nil {
return err
}
entry.Set("end_time", endMs)
entry.Set("auto_stopped", true)
bump(entry, now)
if err := tx.Save(entry); err != nil {
return err
}
stopped = true
return nil
})
if err != nil {
app.Logger().Error("auto-stop failed for entry", "id", id, "error", err)
return false
}
return stopped
}