aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJason Gunthorpe <jgg@mellanox.com>2020-06-19 12:05:46 -0300
committerJason Gunthorpe <jgg@nvidia.com>2020-06-22 20:24:18 -0300
commit6c7dbe902d8679570ca10f39672d844fa5cb6c50 (patch)
tree739423332633aaf0159bc4c799685d8f6e717596
parentdc8cf1fb338b6f072692c34a4e22a24551486b35 (diff)
downloadcloud_mdir_sync-6c7dbe902d8679570ca10f39672d844fa5cb6c50.tar.gz
cloud_mdir_sync-6c7dbe902d8679570ca10f39672d844fa5cb6c50.tar.bz2
cloud_mdir_sync-6c7dbe902d8679570ca10f39672d844fa5cb6c50.zip
GMail: Instead of polling each Label, poll the profile
The global history_id covers all labels, if we see it change then do a delta query to all the labels to figure things out. This reduces the amount of server traffic if there are multiple labels. Signed-off-by: Jason Gunthorpe <jgg@mellanox.com>
-rw-r--r--cloud_mdir_sync/gmail.py23
1 files changed, 14 insertions, 9 deletions
diff --git a/cloud_mdir_sync/gmail.py b/cloud_mdir_sync/gmail.py
index b621577..dc70361 100644
--- a/cloud_mdir_sync/gmail.py
+++ b/cloud_mdir_sync/gmail.py
@@ -77,6 +77,7 @@ class GmailAPI(oauth.Account):
def __init__(self, cfg: config.Config, user: str):
super().__init__(cfg, user)
self.domain_id = f"gmail-{user}"
+ self.mailboxes = []
async def go(self):
cfg = self.cfg
@@ -100,6 +101,18 @@ class GmailAPI(oauth.Account):
token=self.api_token)
await self._do_authenticate()
+ asyncio.create_task(self._poll_for_changes())
+
+ async def _poll_for_changes(self):
+ while True:
+ await asyncio.sleep(60)
+ profile = await self.get_json("v1","/users/me/profile")
+ history_id = int(profile["historyId"])
+ for mbox in self.mailboxes:
+ if (mbox.history_delta is not None
+ and int(mbox.history_delta[1]) < history_id):
+ mbox.need_update = True
+ mbox.changed_event.set()
def _set_token(self, api_token):
# Only store the refresh token, access tokens are more dangerous to
@@ -314,7 +327,6 @@ class GMailMailbox(mailbox.Mailbox):
supported_flags = (messages.Message.FLAG_READ
| messages.Message.FLAG_FLAGGED
| messages.Message.FLAG_DELETED)
- timer = None
gmail: GmailAPI
gmail_messages: Dict[str, GMailMessage]
history_delta = None
@@ -326,6 +338,7 @@ class GMailMailbox(mailbox.Mailbox):
self.gmail = gmail
self.gmail_messages = {}
self.max_fetches = asyncio.Semaphore(10)
+ gmail.mailboxes.append(self)
def __repr__(self):
return f"<GMailMailbox at {id(self):x} for {self.gmail.domain_id} {self.label_name}>"
@@ -504,14 +517,6 @@ class GMailMailbox(mailbox.Mailbox):
for msg in self.history_delta[0] if msg.content_hash is not None
}
self.need_update = False
- if self.timer:
- self.timer.cancel()
- self.timer = None
- self.timer = self.cfg.loop.call_later(60, self._timer)
-
- def _timer(self):
- self.need_update = True
- self.changed_event.set()
def force_content(self, msgs):
raise RuntimeError("Cannot move messages into the Cloud")