From 72d2ceab9802a504f70ecf1dbd62d07f8fa52fe3 Mon Sep 17 00:00:00 2001 From: Erik Winter Date: Sun, 23 Oct 2022 16:46:37 +0200 Subject: [PATCH] fix sync --- cmd/android-app/component/tasks.go | 12 ++++++------ cmd/android-app/runner/runner.go | 20 +++++--------------- 2 files changed, 11 insertions(+), 21 deletions(-) diff --git a/cmd/android-app/component/tasks.go b/cmd/android-app/component/tasks.go index 082839f..c8e8ae8 100644 --- a/cmd/android-app/component/tasks.go +++ b/cmd/android-app/component/tasks.go @@ -1,7 +1,6 @@ package component import ( - "fmt" "sort" "time" @@ -66,26 +65,27 @@ func (t *Tasks) Today() (map[string]string, error) { return tasks, nil } -func (t *Tasks) Sync(logger *Logger) (int, int, error) { +func (t *Tasks) Sync() (int, int, error) { countDisp, err := process.NewSend(t.local, t.disp).Process() if err != nil { return 0, 0, err } - logger.Log("finished dispatch") latestFetch, err := t.local.LatestSync() if err != nil { return 0, 0, err } - logger.Log(fmt.Sprintf("latest fetch time, was at %s", latestFetch.Format(time.Stamp))) - if time.Now().Before(latestFetch.Add(15 * time.Minute)) { + // use unix timestamp for time comparison, because time.Before and + // time.After depend on a monotonic clock and in Android the + // monotonic clock stops ticking if the phone is in suspended sleep + if latestFetch.Add(15*time.Minute).Unix() > time.Now().Unix() { return countDisp, 0, nil } + res, err := process.NewFetch(t.remote, t.local).Process() if err != nil { return countDisp, 0, err } - logger.Log("fininshed actual fetch") return countDisp, res.Count, nil } diff --git a/cmd/android-app/runner/runner.go b/cmd/android-app/runner/runner.go index 5fe22f6..779d2b9 100644 --- a/cmd/android-app/runner/runner.go +++ b/cmd/android-app/runner/runner.go @@ -73,7 +73,6 @@ func (r *Runner) Run() { func (r *Runner) processRequest() { for req := range r.requests { - r.logger.Log(fmt.Sprintf("processing request %T", req)) switch v := req.(type) { case screen.SaveConfigRequest: r.status = "saving..." @@ -81,33 +80,28 @@ func (r *Runner) processRequest() { for k, val := range v.Fields { r.conf.Set(k, val) } - r.logger.Log("new config saved") r.status = "ready" + r.logger.Log("config saved") case screen.SyncTasksRequest: - r.logger.Log("starting sync request") r.status = "syncing..." r.refresh <- true - countDisp, countFetch, err := r.tasks.Sync(r.logger) + countDisp, countFetch, err := r.tasks.Sync() if err != nil { r.logger.Log(err.Error()) } - //if countDisp > 0 || countFetch > 0 { - r.logger.Log(fmt.Sprintf("task sync: dispatched: %d, fetched: %d", countDisp, countFetch)) - //} + if countDisp > 0 || countFetch > 0 { + r.logger.Log(fmt.Sprintf("task sync: dispatched: %d, fetched: %d", countDisp, countFetch)) + } r.status = "ready" - - r.logger.Log("fetching all") all, err := r.tasks.All() if err != nil { r.logger.Log(err.Error()) break } - r.logger.Log("saving all") if err := save(r.fileURI, all); err != nil { r.logger.Log(err.Error()) break } - r.logger.Log("sync request done") case screen.MarkTaskDoneRequest: if err := r.tasks.MarkDone(v.ID); err != nil { r.logger.Log(err.Error()) @@ -117,19 +111,15 @@ func (r *Runner) processRequest() { r.logger.Log("request unknown") } r.refresh <- true - r.logger.Log("processing request done") } - } func (r *Runner) refresher() { for <-r.refresh { - r.logger.Log("start refresh") tasks, err := r.tasks.Today() if err != nil { r.logger.Log(err.Error()) } - r.logger.Log(fmt.Sprintf("found %d tasks", len(tasks))) sTasks := []screen.Task{} for id, action := range tasks { sTasks = append(sTasks, screen.Task{