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
=== modified file 'cmd/account-polld/main.go'
--- cmd/account-polld/main.go 2016-02-28 22:41:25 +0000
+++ cmd/account-polld/main.go 2016-02-28 22:41:25 +0000
@@ -93,60 +93,20 @@
93}93}
9494
95func monitorAccounts(postWatch chan *PostWatch, pollBus *pollbus.PollBus) {95func monitorAccounts(postWatch chan *PostWatch, pollBus *pollbus.PollBus) {
96 // Note: the accounts monitored are all linked to webapps right now96 // register account watchers
97 watcher := accounts.NewWatcher(SERVICETYPE_WEBAPPS)97 webappWatcher := accounts.NewWatcher(SERVICETYPE_WEBAPPS)
98
99 // map: account id -> account manager
98 mgr := make(map[uint]*AccountManager)100 mgr := make(map[uint]*AccountManager)
99101
100 var wg sync.WaitGroup102 var wg sync.WaitGroup
101103
102L:
103 for {104 for {
104 select {105 select {
105 case data := <-watcher.C:106 // Handle account creation, new account data and account deletion
106 if account, ok := mgr[data.AccountId]; ok {107 case data := <-webappWatcher.C:
107 if data.Enabled {108 handleWatcherData(&wg, webappWatcher, postWatch, mgr, data, SERVICETYPE_WEBAPPS)
108 log.Println("New account data for existing account with id", data.AccountId)109 // Respond to dbus poll requests
109 account.penaltyCount = 0
110 account.updateAuthData(data)
111 wg.Add(1)
112 go func() {
113 defer wg.Done()
114 // Poll() needs to be called asynchronously as otherwise qtcontacs' GetAvatar() will
115 // raise an error: "QSocketNotifier: Can only be used with threads started with QThread"
116 account.Poll(false)
117 }()
118 // No wg.Wait() here as it would break GetAvatar() again.
119 // Instead we have a wg.Wait() before the PollChan polling below.
120 } else {
121 account.Delete()
122 delete(mgr, data.AccountId)
123 }
124 } else if data.Enabled {
125 var plugin plugins.Plugin
126 switch data.ServiceName {
127 case SERVICENAME_GMAIL:
128 log.Println("Creating account with id", data.AccountId, "for", data.ServiceName)
129 plugin = gmail.New(data.AccountId)
130 case SERVICENAME_TWITTER:
131 // This is just stubbed until the plugin exists.
132 log.Println("Creating account with id", data.AccountId, "for", data.ServiceName)
133 plugin = twitter.New(data.AccountId)
134 default:
135 log.Println("Unhandled account with id", data.AccountId, "for", data.ServiceName)
136 continue L
137 }
138 mgr[data.AccountId] = NewAccountManager(watcher, postWatch, plugin)
139 mgr[data.AccountId].updateAuthData(data)
140 wg.Add(1)
141 go func() {
142 defer wg.Done()
143 // Poll() needs to be called asynchronously as otherwise qtcontacs' GetAvatar() will
144 // raise an error: "QSocketNotifier: Can only be used with threads started with QThread"
145 mgr[data.AccountId].Poll(true)
146 }()
147 // No wg.Wait() here as it would break GetAvatar() again.
148 // Instead we have a wg.Wait() before the PollChan polling below.
149 }
150 case <-pollBus.PollChan:110 case <-pollBus.PollChan:
151 wg.Wait() // Finish all running Poll() calls before potentially polling the same accounts again111 wg.Wait() // Finish all running Poll() calls before potentially polling the same accounts again
152 for _, v := range mgr {112 for _, v := range mgr {
@@ -166,6 +126,55 @@
166 }126 }
167}127}
168128
129func handleWatcherData(wg *sync.WaitGroup, watcher *accounts.Watcher, postWatch chan *PostWatch, mgr map[uint]*AccountManager, data accounts.AuthData, serviceType string) {
130 if account, ok := mgr[data.AccountId]; ok {
131 if data.Enabled {
132 log.Println("New account data for existing account with id", data.AccountId)
133 account.penaltyCount = 0
134 account.updateAuthData(data)
135 wg.Add(1)
136 go func() {
137 defer wg.Done()
138 // Poll() needs to be called asynchronously as otherwise qtcontacs' GetAvatar() will
139 // raise an error: "QSocketNotifier: Can only be used with threads started with QThread"
140 account.Poll(false)
141 }()
142 // No wg.Wait() here as it would break GetAvatar() again.
143 // Instead we have a wg.Wait() before the PollChan polling below.
144 } else {
145 account.Delete()
146 delete(mgr, data.AccountId)
147 }
148 } else if data.Enabled {
149 var plugin plugins.Plugin = nil
150 if serviceType == SERVICETYPE_WEBAPPS { // Allows other service types to be added here in the future
151 switch data.ServiceName {
152 case SERVICENAME_GMAIL:
153 log.Println("Creating account with id", data.AccountId, "for", data.ServiceName)
154 plugin = gmail.New(data.AccountId)
155 case SERVICENAME_TWITTER:
156 log.Println("Creating account with id", data.AccountId, "for", data.ServiceName)
157 plugin = twitter.New(data.AccountId)
158 default:
159 log.Println("Unhandled account with id", data.AccountId, "for", data.ServiceName)
160 }
161 }
162 if plugin != nil {
163 mgr[data.AccountId] = NewAccountManager(watcher, postWatch, plugin)
164 mgr[data.AccountId].updateAuthData(data)
165 wg.Add(1)
166 go func() {
167 defer wg.Done()
168 // Poll() needs to be called asynchronously as otherwise qtcontacs' GetAvatar() will
169 // raise an error: "QSocketNotifier: Can only be used with threads started with QThread"
170 mgr[data.AccountId].Poll(true)
171 }()
172 // No wg.Wait() here as it would break GetAvatar() again.
173 // Instead we have a wg.Wait() before the PollChan polling below.
174 }
175 }
176}
177
169func postOffice(bus *dbus.Connection, postWatch chan *PostWatch) {178func postOffice(bus *dbus.Connection, postWatch chan *PostWatch) {
170 for post := range postWatch {179 for post := range postWatch {
171 obj := bus.Object(POSTAL_SERVICE, pushObjectPath(post.appId))180 obj := bus.Object(POSTAL_SERVICE, pushObjectPath(post.appId))

Subscribers

People subscribed via source and target branches