fix sync
This commit is contained in:
parent
5bbea05305
commit
72d2ceab98
|
@ -1,7 +1,6 @@
|
||||||
package component
|
package component
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"sort"
|
"sort"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -66,26 +65,27 @@ func (t *Tasks) Today() (map[string]string, error) {
|
||||||
return tasks, nil
|
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()
|
countDisp, err := process.NewSend(t.local, t.disp).Process()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, 0, err
|
return 0, 0, err
|
||||||
}
|
}
|
||||||
logger.Log("finished dispatch")
|
|
||||||
|
|
||||||
latestFetch, err := t.local.LatestSync()
|
latestFetch, err := t.local.LatestSync()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, 0, err
|
return 0, 0, err
|
||||||
}
|
}
|
||||||
logger.Log(fmt.Sprintf("latest fetch time, was at %s", latestFetch.Format(time.Stamp)))
|
// use unix timestamp for time comparison, because time.Before and
|
||||||
if time.Now().Before(latestFetch.Add(15 * time.Minute)) {
|
// 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
|
return countDisp, 0, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
res, err := process.NewFetch(t.remote, t.local).Process()
|
res, err := process.NewFetch(t.remote, t.local).Process()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return countDisp, 0, err
|
return countDisp, 0, err
|
||||||
}
|
}
|
||||||
logger.Log("fininshed actual fetch")
|
|
||||||
return countDisp, res.Count, nil
|
return countDisp, res.Count, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -73,7 +73,6 @@ func (r *Runner) Run() {
|
||||||
|
|
||||||
func (r *Runner) processRequest() {
|
func (r *Runner) processRequest() {
|
||||||
for req := range r.requests {
|
for req := range r.requests {
|
||||||
r.logger.Log(fmt.Sprintf("processing request %T", req))
|
|
||||||
switch v := req.(type) {
|
switch v := req.(type) {
|
||||||
case screen.SaveConfigRequest:
|
case screen.SaveConfigRequest:
|
||||||
r.status = "saving..."
|
r.status = "saving..."
|
||||||
|
@ -81,33 +80,28 @@ func (r *Runner) processRequest() {
|
||||||
for k, val := range v.Fields {
|
for k, val := range v.Fields {
|
||||||
r.conf.Set(k, val)
|
r.conf.Set(k, val)
|
||||||
}
|
}
|
||||||
r.logger.Log("new config saved")
|
|
||||||
r.status = "ready"
|
r.status = "ready"
|
||||||
|
r.logger.Log("config saved")
|
||||||
case screen.SyncTasksRequest:
|
case screen.SyncTasksRequest:
|
||||||
r.logger.Log("starting sync request")
|
|
||||||
r.status = "syncing..."
|
r.status = "syncing..."
|
||||||
r.refresh <- true
|
r.refresh <- true
|
||||||
countDisp, countFetch, err := r.tasks.Sync(r.logger)
|
countDisp, countFetch, err := r.tasks.Sync()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
r.logger.Log(err.Error())
|
r.logger.Log(err.Error())
|
||||||
}
|
}
|
||||||
//if countDisp > 0 || countFetch > 0 {
|
if countDisp > 0 || countFetch > 0 {
|
||||||
r.logger.Log(fmt.Sprintf("task sync: dispatched: %d, fetched: %d", countDisp, countFetch))
|
r.logger.Log(fmt.Sprintf("task sync: dispatched: %d, fetched: %d", countDisp, countFetch))
|
||||||
//}
|
}
|
||||||
r.status = "ready"
|
r.status = "ready"
|
||||||
|
|
||||||
r.logger.Log("fetching all")
|
|
||||||
all, err := r.tasks.All()
|
all, err := r.tasks.All()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
r.logger.Log(err.Error())
|
r.logger.Log(err.Error())
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
r.logger.Log("saving all")
|
|
||||||
if err := save(r.fileURI, all); err != nil {
|
if err := save(r.fileURI, all); err != nil {
|
||||||
r.logger.Log(err.Error())
|
r.logger.Log(err.Error())
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
r.logger.Log("sync request done")
|
|
||||||
case screen.MarkTaskDoneRequest:
|
case screen.MarkTaskDoneRequest:
|
||||||
if err := r.tasks.MarkDone(v.ID); err != nil {
|
if err := r.tasks.MarkDone(v.ID); err != nil {
|
||||||
r.logger.Log(err.Error())
|
r.logger.Log(err.Error())
|
||||||
|
@ -117,19 +111,15 @@ func (r *Runner) processRequest() {
|
||||||
r.logger.Log("request unknown")
|
r.logger.Log("request unknown")
|
||||||
}
|
}
|
||||||
r.refresh <- true
|
r.refresh <- true
|
||||||
r.logger.Log("processing request done")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Runner) refresher() {
|
func (r *Runner) refresher() {
|
||||||
for <-r.refresh {
|
for <-r.refresh {
|
||||||
r.logger.Log("start refresh")
|
|
||||||
tasks, err := r.tasks.Today()
|
tasks, err := r.tasks.Today()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
r.logger.Log(err.Error())
|
r.logger.Log(err.Error())
|
||||||
}
|
}
|
||||||
r.logger.Log(fmt.Sprintf("found %d tasks", len(tasks)))
|
|
||||||
sTasks := []screen.Task{}
|
sTasks := []screen.Task{}
|
||||||
for id, action := range tasks {
|
for id, action := range tasks {
|
||||||
sTasks = append(sTasks, screen.Task{
|
sTasks = append(sTasks, screen.Task{
|
||||||
|
|
Loading…
Reference in New Issue