def sync(self): """ Start per-folder syncs. Only have one per-folder sync in the 'initial' state at a time. """ with session_scope() as db_session: saved_states = dict( (saved_state.folder_name, saved_state.state) for saved_state in db_session.query(FolderSync).filter_by(account_id=self.account_id) ) crispin_client = new_crispin(self.account_id, self.provider) with crispin_client.pool.get() as c: sync_folders = crispin_client.sync_folders(c) imapaccount = db_session.query(ImapAccount).get(self.account_id) folder_names = crispin_client.folder_names(c) save_folder_names(self.log, imapaccount, folder_names, db_session) for folder in sync_folders: if saved_states.get(folder) != "finish": self.log.info("Initializing folder sync for {0}".format(folder)) thread = ImapFolderSyncMonitor( self.account_id, folder, self.email_address, self.provider, self.shared_state, self.folder_state_handlers, ) thread.start() self.folder_monitors.add(thread) while not self._thread_polling(thread) and not self._thread_finished(thread): sleep(self.heartbeat) # Allow individual folder sync monitors to shut themselves down # after completing the initial sync. if self._thread_finished(thread): self.log.info("Folder sync for {} is done.".format(folder)) # NOTE: Greenlet is automatically removed from the group # after finishing. self.folder_monitors.join()
def sync(self): """ Start per-folder syncs. Only have one per-folder sync in the 'initial' state at a time. """ with session_scope() as db_session: saved_states = dict((saved_state.folder_name, saved_state.state) \ for saved_state in db_session.query(FolderSync).filter_by( imapaccount_id=self.account_id)) crispin_client = new_crispin(self.account_id, self.provider) with crispin_client.pool.get() as c: sync_folders = crispin_client.sync_folders(c) imapaccount = db_session.query(ImapAccount).get(self.account_id) folder_names = crispin_client.folder_names(c) save_folder_names(self.log, imapaccount, folder_names, db_session) for folder in sync_folders: if saved_states.get(folder) != 'finish': self.log.info("Initializing folder sync for {0}".format(folder)) thread = ImapFolderSyncMonitor(self.account_id, folder, self.email_address, self.provider, self.shared_state, self.folder_state_handlers) thread.start() self.folder_monitors.append(thread) while not self._thread_polling(thread) and \ not self._thread_finished(thread): sleep(self.heartbeat) # Allow individual folder sync monitors to shut themselves down # after completing the initial sync. if self._thread_finished(thread): self.log.info("Folder sync for {0} is done.".format(folder)) self.folder_monitors.pop() # Just hang out. We don't want to block, but we don't want to return # either, since that will let the threads go out of scope. while True: sleep(self.heartbeat)