Merge lp:~nikwen/account-polld/more-flexible-plugin-initialization into lp:~ubuntu-push-hackers/account-polld/trunk

Proposed by Niklas Wenzel
Status: Needs review
Proposed branch: lp:~nikwen/account-polld/more-flexible-plugin-initialization
Merge into: lp:~ubuntu-push-hackers/account-polld/trunk
Prerequisite: lp:~nikwen/account-polld/more-flexible-account-authentication
Diff against target: 128 lines (+57/-48)
1 file modified
cmd/account-polld/main.go (+57/-48)
To merge this branch: bzr merge lp:~nikwen/account-polld/more-flexible-plugin-initialization
Reviewer Review Type Date Requested Status
Ubuntu Push Hackers Pending
Review via email: mp+287418@code.launchpad.net

Commit message

Make plugin initialization more flexible, easing the addition of new plugins in the future

Description of the change

Make plugin initialization more flexible, easing the addition of new plugins in the future

Required for IMAP support

To post a comment you must log in.
158. By Niklas Wenzel

Fix cut-off comment

Unmerged revisions

158. By Niklas Wenzel

Fix cut-off comment

157. By Niklas Wenzel

Remove old general TODO that I accidentally copied over from the imap-mails branch

156. By Niklas Wenzel

Make plugin initialization more flexible, easing the addition of new plugins in the future

155. By Niklas Wenzel

Fix build errors

154. By Niklas Wenzel

Improve error messages

153. By Niklas Wenzel

Formatting fix

152. By Niklas Wenzel

Make account authentication more flexible, easing the addition of new auth methods

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'cmd/account-polld/main.go'
2--- cmd/account-polld/main.go 2016-02-28 22:41:25 +0000
3+++ cmd/account-polld/main.go 2016-02-28 22:41:25 +0000
4@@ -93,60 +93,20 @@
5 }
6
7 func monitorAccounts(postWatch chan *PostWatch, pollBus *pollbus.PollBus) {
8- // Note: the accounts monitored are all linked to webapps right now
9- watcher := accounts.NewWatcher(SERVICETYPE_WEBAPPS)
10+ // register account watchers
11+ webappWatcher := accounts.NewWatcher(SERVICETYPE_WEBAPPS)
12+
13+ // map: account id -> account manager
14 mgr := make(map[uint]*AccountManager)
15
16 var wg sync.WaitGroup
17
18-L:
19 for {
20 select {
21- case data := <-watcher.C:
22- if account, ok := mgr[data.AccountId]; ok {
23- if data.Enabled {
24- log.Println("New account data for existing account with id", data.AccountId)
25- account.penaltyCount = 0
26- account.updateAuthData(data)
27- wg.Add(1)
28- go func() {
29- defer wg.Done()
30- // Poll() needs to be called asynchronously as otherwise qtcontacs' GetAvatar() will
31- // raise an error: "QSocketNotifier: Can only be used with threads started with QThread"
32- account.Poll(false)
33- }()
34- // No wg.Wait() here as it would break GetAvatar() again.
35- // Instead we have a wg.Wait() before the PollChan polling below.
36- } else {
37- account.Delete()
38- delete(mgr, data.AccountId)
39- }
40- } else if data.Enabled {
41- var plugin plugins.Plugin
42- switch data.ServiceName {
43- case SERVICENAME_GMAIL:
44- log.Println("Creating account with id", data.AccountId, "for", data.ServiceName)
45- plugin = gmail.New(data.AccountId)
46- case SERVICENAME_TWITTER:
47- // This is just stubbed until the plugin exists.
48- log.Println("Creating account with id", data.AccountId, "for", data.ServiceName)
49- plugin = twitter.New(data.AccountId)
50- default:
51- log.Println("Unhandled account with id", data.AccountId, "for", data.ServiceName)
52- continue L
53- }
54- mgr[data.AccountId] = NewAccountManager(watcher, postWatch, plugin)
55- mgr[data.AccountId].updateAuthData(data)
56- wg.Add(1)
57- go func() {
58- defer wg.Done()
59- // Poll() needs to be called asynchronously as otherwise qtcontacs' GetAvatar() will
60- // raise an error: "QSocketNotifier: Can only be used with threads started with QThread"
61- mgr[data.AccountId].Poll(true)
62- }()
63- // No wg.Wait() here as it would break GetAvatar() again.
64- // Instead we have a wg.Wait() before the PollChan polling below.
65- }
66+ // Handle account creation, new account data and account deletion
67+ case data := <-webappWatcher.C:
68+ handleWatcherData(&wg, webappWatcher, postWatch, mgr, data, SERVICETYPE_WEBAPPS)
69+ // Respond to dbus poll requests
70 case <-pollBus.PollChan:
71 wg.Wait() // Finish all running Poll() calls before potentially polling the same accounts again
72 for _, v := range mgr {
73@@ -166,6 +126,55 @@
74 }
75 }
76
77+func handleWatcherData(wg *sync.WaitGroup, watcher *accounts.Watcher, postWatch chan *PostWatch, mgr map[uint]*AccountManager, data accounts.AuthData, serviceType string) {
78+ if account, ok := mgr[data.AccountId]; ok {
79+ if data.Enabled {
80+ log.Println("New account data for existing account with id", data.AccountId)
81+ account.penaltyCount = 0
82+ account.updateAuthData(data)
83+ wg.Add(1)
84+ go func() {
85+ defer wg.Done()
86+ // Poll() needs to be called asynchronously as otherwise qtcontacs' GetAvatar() will
87+ // raise an error: "QSocketNotifier: Can only be used with threads started with QThread"
88+ account.Poll(false)
89+ }()
90+ // No wg.Wait() here as it would break GetAvatar() again.
91+ // Instead we have a wg.Wait() before the PollChan polling below.
92+ } else {
93+ account.Delete()
94+ delete(mgr, data.AccountId)
95+ }
96+ } else if data.Enabled {
97+ var plugin plugins.Plugin = nil
98+ if serviceType == SERVICETYPE_WEBAPPS { // Allows other service types to be added here in the future
99+ switch data.ServiceName {
100+ case SERVICENAME_GMAIL:
101+ log.Println("Creating account with id", data.AccountId, "for", data.ServiceName)
102+ plugin = gmail.New(data.AccountId)
103+ case SERVICENAME_TWITTER:
104+ log.Println("Creating account with id", data.AccountId, "for", data.ServiceName)
105+ plugin = twitter.New(data.AccountId)
106+ default:
107+ log.Println("Unhandled account with id", data.AccountId, "for", data.ServiceName)
108+ }
109+ }
110+ if plugin != nil {
111+ mgr[data.AccountId] = NewAccountManager(watcher, postWatch, plugin)
112+ mgr[data.AccountId].updateAuthData(data)
113+ wg.Add(1)
114+ go func() {
115+ defer wg.Done()
116+ // Poll() needs to be called asynchronously as otherwise qtcontacs' GetAvatar() will
117+ // raise an error: "QSocketNotifier: Can only be used with threads started with QThread"
118+ mgr[data.AccountId].Poll(true)
119+ }()
120+ // No wg.Wait() here as it would break GetAvatar() again.
121+ // Instead we have a wg.Wait() before the PollChan polling below.
122+ }
123+ }
124+}
125+
126 func postOffice(bus *dbus.Connection, postWatch chan *PostWatch) {
127 for post := range postWatch {
128 obj := bus.Object(POSTAL_SERVICE, pushObjectPath(post.appId))

Subscribers

People subscribed via source and target branches