def start_sync(self, account_id): """ Starts a sync for the account with the given account_id. If that account doesn't exist, does nothing. """ with self.semaphore, session_scope(account_id) as db_session: acc = db_session.query(Account).get(account_id) if acc is None: self.log.error('no such account', account_id=account_id) return self.log.info('starting sync', account_id=acc.id, email_address=acc.email_address) if acc.id not in self.syncing_accounts: try: acc.sync_host = self.process_identifier if acc.sync_email: monitor = self.monitor_cls_for[acc.provider](acc) self.email_sync_monitors[acc.id] = monitor monitor.start() info = acc.provider_info if info.get('contacts', None) and acc.sync_contacts: contact_sync = ContactSync(acc.email_address, acc.verbose_provider, acc.id, acc.namespace.id) self.contact_sync_monitors[acc.id] = contact_sync contact_sync.start() if info.get('events', None) and acc.sync_events: if (USE_GOOGLE_PUSH_NOTIFICATIONS and acc.provider == 'gmail'): event_sync = GoogleEventSync( acc.email_address, acc.verbose_provider, acc.id, acc.namespace.id) else: event_sync = EventSync(acc.email_address, acc.verbose_provider, acc.id, acc.namespace.id) self.event_sync_monitors[acc.id] = event_sync event_sync.start() acc.sync_started() self.syncing_accounts.add(acc.id) db_session.commit() self.log.info('Sync started', account_id=account_id, sync_host=acc.sync_host) except Exception: self.log.error('Error starting sync', exc_info=True, account_id=account_id) else: self.log.info('sync already started', account_id=account_id)
def start_sync(self, account_id): """ Starts a sync for the account with the given account_id. If that account doesn't exist, does nothing. """ with self.semaphore, session_scope(account_id) as db_session: acc = db_session.query(Account).with_for_update().get(account_id) if acc is None: self.log.error("no such account", account_id=account_id) return False if not acc.sync_should_run: return False if (acc.desired_sync_host is not None and acc.desired_sync_host != self.process_identifier): return False if acc.sync_host is not None and acc.sync_host != self.process_identifier: return False self.log.info("starting sync", account_id=acc.id, email_address=acc.email_address) if acc.id in self.syncing_accounts: self.log.info("sync already started", account_id=account_id) return False try: acc.sync_host = self.process_identifier if acc.sync_email: monitor = self.monitor_cls_for[acc.provider](acc) self.email_sync_monitors[acc.id] = monitor monitor.start() info = acc.provider_info if info.get("contacts", None) and acc.sync_contacts: contact_sync = ContactSync( acc.email_address, acc.verbose_provider, acc.id, acc.namespace.id, ) self.contact_sync_monitors[acc.id] = contact_sync contact_sync.start() if info.get("events", None) and acc.sync_events: if USE_GOOGLE_PUSH_NOTIFICATIONS and acc.provider == "gmail": event_sync = GoogleEventSync( acc.email_address, acc.verbose_provider, acc.id, acc.namespace.id, ) else: event_sync = EventSync( acc.email_address, acc.verbose_provider, acc.id, acc.namespace.id, ) self.event_sync_monitors[acc.id] = event_sync event_sync.start() acc.sync_started() self.syncing_accounts.add(acc.id) # TODO (mark): Uncomment this after we've transitioned to from statsd to brubeck # statsd_client.gauge('mailsync.sync_hosts_counts.{}'.format(acc.id), 1, delta=True) db_session.commit() self.log.info("Sync started", account_id=account_id, sync_host=acc.sync_host) except Exception: self.log.error("Error starting sync", exc_info=True, account_id=account_id) return False return True
def start_sync(self, account_id): """ Starts a sync for the account with the given account_id. If that account doesn't exist, does nothing. """ with session_scope() as db_session: acc = db_session.query(Account).get(account_id) if acc is None: self.log.error('no such account', account_id=account_id) return fqdn = platform.node() self.log.info('starting sync', account_id=acc.id, email_address=acc.email_address) if acc.sync_host is not None and acc.sync_host != fqdn: self.log.error( 'Sync Host Mismatch', message='account is syncing on another host {}'.format( acc.sync_host), account_id=account_id) elif acc.id not in self.monitors: try: if acc.is_sync_locked and acc.is_killed: acc.sync_unlock() acc.sync_lock() monitor = self.monitor_cls_for[acc.provider](acc) self.monitors[acc.id] = monitor monitor.start() info = acc.provider_info if info.get('contacts', None) and acc.sync_contacts: contact_sync = ContactSync(acc.email_address, acc.provider, acc.id, acc.namespace.id) self.contact_sync_monitors[acc.id] = contact_sync contact_sync.start() if info.get('events', None) and acc.sync_events: if (USE_GOOGLE_PUSH_NOTIFICATIONS and acc.provider == 'gmail'): event_sync = GoogleEventSync( acc.email_address, acc.provider, acc.id, acc.namespace.id) else: event_sync = EventSync(acc.email_address, acc.provider, acc.id, acc.namespace.id) self.event_sync_monitors[acc.id] = event_sync event_sync.start() acc.sync_started() db_session.add(acc) db_session.commit() self.log.info('Sync started', account_id=account_id, sync_host=fqdn) except Exception as e: self.log.error('sync_error', message=str(e.message), account_id=account_id) else: self.log.info('sync already started', account_id=account_id)