Skip to content

Commit

Permalink
Merge pull request #287 from pankona/sync-periodically
Browse files Browse the repository at this point in the history
sync periodically
  • Loading branch information
pankona authored Oct 11, 2021
2 parents d203123 + 2f98b81 commit b112970
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
14 changes: 12 additions & 2 deletions cmd/hashira-cui/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os"
"os/user"
"path/filepath"
"time"

"github.com/pankona/gocui"
hashirac "github.com/pankona/hashira/client"
Expand Down Expand Up @@ -88,17 +89,26 @@ func main() {
log.Printf("HASHIRA_ACCESSTOKEN is invalid. Synchronization is not started: %v", err)
}
m.SetAccessToken(accesstoken)

if err := m.SyncOnNotify(context.Background()); err != nil {
log.Printf("sync on notify finished: %v", err)
}

// sync on launch
m.NotifySync()
}()

go func() {
// start polling
for {
m.NotifySync()
<-time.After(2 * time.Minute)
}
}()
}

// prepare controller
ps := &PubSub{}
m.SetPublisher(ps)

c := &Ctrl{
m: m,
pub: ps,
Expand Down
23 changes: 23 additions & 0 deletions cmd/hashira-cui/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ type Model struct {
// TODO: remove if accesstoken is held by syncclient
accesstoken string
syncChan chan struct{}

pub Publisher
}

func NewModel(hc *hashirac.Client, sc *syncutil.Client) *Model {
Expand Down Expand Up @@ -82,6 +84,10 @@ func (m *Model) SetAccessToken(accesstoken string) {
m.accesstoken = accesstoken
}

func (m *Model) SetPublisher(p Publisher) {
m.pub = p
}

func (m *Model) NotifySync() {
select {
case m.syncChan <- struct{}{}:
Expand Down Expand Up @@ -122,6 +128,23 @@ func (m *Model) SyncOnNotify(ctx context.Context) error {
log.Printf("failed to download tasks: %v", err)
}

// publish to notify update of tasks and priorities
tasks, err := m.List(ctx)
if err != nil {
log.Printf("failed to retrieve tasks: %v", err)
}

priorities, err := m.RetrievePriority(ctx)
if err != nil {
log.Printf("failed to retrieve priorities: %v", err)
}

ktasks := make(map[string]*KeyedTask)
for k, v := range tasks {
ktasks[k] = (*KeyedTask)(v)
}

m.pub.Publish("update", ktasks, priorities)
}
}(ctx)
}
Expand Down
2 changes: 1 addition & 1 deletion sync/syncutil/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (c *Client) Upload(accesstoken string, uploadTarget UploadTarget) error {

if len(ur.Tasks) == 0 && !isPriorityDirty(allPriorities) {
// there's no task to upload
return fmt.Errorf("there's no dirty task. no task to upload")
return nil
}

sc := sync.NewClient()
Expand Down

0 comments on commit b112970

Please sign in to comment.