本文整理汇总了Python中inbox.heartbeat.store.HeartbeatStatusProxy.publish方法的典型用法代码示例。如果您正苦于以下问题:Python HeartbeatStatusProxy.publish方法的具体用法?Python HeartbeatStatusProxy.publish怎么用?Python HeartbeatStatusProxy.publish使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类inbox.heartbeat.store.HeartbeatStatusProxy
的用法示例。
在下文中一共展示了HeartbeatStatusProxy.publish方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: FolderSyncEngine
# 需要导入模块: from inbox.heartbeat.store import HeartbeatStatusProxy [as 别名]
# 或者: from inbox.heartbeat.store.HeartbeatStatusProxy import publish [as 别名]
class FolderSyncEngine(Greenlet):
"""Base class for a per-folder IMAP sync engine."""
def __init__(self, account_id, folder_name, folder_id, email_address,
provider_name, poll_frequency, syncmanager_lock,
refresh_flags_max, retry_fail_classes):
bind_context(self, 'foldersyncengine', account_id, folder_id)
self.account_id = account_id
self.folder_name = folder_name
self.folder_id = folder_id
self.poll_frequency = poll_frequency
self.syncmanager_lock = syncmanager_lock
self.refresh_flags_max = refresh_flags_max
self.retry_fail_classes = retry_fail_classes
self.state = None
self.provider_name = provider_name
self.is_initial_sync = False
self.is_first_sync = False
with mailsync_session_scope() as db_session:
account = db_session.query(Account).get(self.account_id)
self.throttled = account.throttled
self.namespace_id = account.namespace.id
assert self.namespace_id is not None, "namespace_id is None"
folder = db_session.query(Folder).get(self.folder_id)
if folder:
self.is_initial_sync = folder.initial_sync_end is None
self.is_first_sync = folder.initial_sync_start is None
self.state_handlers = {
'initial': self.initial_sync,
'initial uidinvalid': self.resync_uids,
'poll': self.poll,
'poll uidinvalid': self.resync_uids,
'finish': lambda self: 'finish',
}
Greenlet.__init__(self)
self.heartbeat_status = HeartbeatStatusProxy(self.account_id,
self.folder_id,
self.folder_name,
email_address,
self.provider_name)
def _run(self):
# Bind greenlet-local logging context.
self.log = log.new(account_id=self.account_id, folder=self.folder_name)
# eagerly signal the sync status
self.heartbeat_status.publish()
return retry_and_report_killed(self._run_impl,
account_id=self.account_id,
folder_name=self.folder_name,
logger=log,
fail_classes=self.retry_fail_classes)
def _run_impl(self):
# We defer initializing the pool to here so that we'll retry if there
# are any errors (remote server 503s or similar) when initializing it.
self.conn_pool = connection_pool(self.account_id)
try:
saved_folder_status = self._load_state()
except IntegrityError:
# The state insert failed because the folder ID ForeignKey
# was no longer valid, ie. the folder for this engine was deleted
# while we were starting up.
# Exit the sync and let the monitor sort things out.
log.info("Folder state loading failed due to IntegrityError",
folder_id=self.folder_id, account_id=self.account_id)
raise MailsyncDone()
# NOTE: The parent ImapSyncMonitor handler could kill us at any
# time if it receives a shutdown command. The shutdown command is
# equivalent to ctrl-c.
while True:
old_state = self.state
try:
self.state = self.state_handlers[old_state]()
self.heartbeat_status.publish(state=self.state)
except UidInvalid:
self.state = self.state + ' uidinvalid'
self.heartbeat_status.publish(state=self.state)
except FolderMissingError:
# Folder was deleted by monitor while its sync was running.
# TODO: Monitor should handle shutting down the folder engine.
log.info('Folder disappeared: {}. Stopping sync.'.format(
self.folder_name), account_id=self.account_id,
folder_id=self.folder_id)
raise MailsyncDone()
except ValidationError:
log.error('Error authenticating; stopping sync', exc_info=True,
account_id=self.account_id, folder_id=self.folder_id)
raise MailsyncDone()
# State handlers are idempotent, so it's okay if we're
# killed between the end of the handler and the commit.
if self.state != old_state:
# Don't need to re-query, will auto refresh on re-associate.
with mailsync_session_scope() as db_session:
#.........这里部分代码省略.........
示例2: FolderSyncEngine
# 需要导入模块: from inbox.heartbeat.store import HeartbeatStatusProxy [as 别名]
# 或者: from inbox.heartbeat.store.HeartbeatStatusProxy import publish [as 别名]
class FolderSyncEngine(Greenlet):
"""Base class for a per-folder IMAP sync engine."""
def __init__(self, account_id, namespace_id, folder_name, folder_id,
email_address, provider_name, syncmanager_lock):
bind_context(self, 'foldersyncengine', account_id, folder_id)
self.account_id = account_id
self.namespace_id = namespace_id
self.folder_name = folder_name
self.folder_id = folder_id
if self.folder_name.lower() == 'inbox':
self.poll_frequency = INBOX_POLL_FREQUENCY
else:
self.poll_frequency = DEFAULT_POLL_FREQUENCY
self.syncmanager_lock = syncmanager_lock
self.state = None
self.provider_name = provider_name
self.last_fast_refresh = None
self.conn_pool = connection_pool(self.account_id)
# Metric flags for sync performance
self.is_initial_sync = False
self.is_first_sync = False
self.is_first_message = False
with session_scope(self.namespace_id) as db_session:
folder = Folder.get(self.folder_id, db_session)
if folder:
self.is_initial_sync = folder.initial_sync_end is None
self.is_first_sync = folder.initial_sync_start is None
self.is_first_message = self.is_first_sync
self.state_handlers = {
'initial': self.initial_sync,
'initial uidinvalid': self.resync_uids,
'poll': self.poll,
'poll uidinvalid': self.resync_uids,
}
Greenlet.__init__(self)
self.heartbeat_status = HeartbeatStatusProxy(self.account_id,
self.folder_id,
self.folder_name,
email_address,
self.provider_name)
def _run(self):
# Bind greenlet-local logging context.
self.log = log.new(account_id=self.account_id, folder=self.folder_name,
provider=self.provider_name)
# eagerly signal the sync status
self.heartbeat_status.publish()
return retry_with_logging(self._run_impl, account_id=self.account_id,
provider=self.provider_name, logger=log)
def _run_impl(self):
try:
saved_folder_status = self._load_state()
except IntegrityError:
# The state insert failed because the folder ID ForeignKey
# was no longer valid, ie. the folder for this engine was deleted
# while we were starting up.
# Exit the sync and let the monitor sort things out.
log.info("Folder state loading failed due to IntegrityError",
folder_id=self.folder_id, account_id=self.account_id)
raise MailsyncDone()
# NOTE: The parent ImapSyncMonitor handler could kill us at any
# time if it receives a shutdown command. The shutdown command is
# equivalent to ctrl-c.
while True:
old_state = self.state
try:
self.state = self.state_handlers[old_state]()
self.heartbeat_status.publish(state=self.state)
except UidInvalid:
self.state = self.state + ' uidinvalid'
self.heartbeat_status.publish(state=self.state)
except FolderMissingError:
# Folder was deleted by monitor while its sync was running.
# TODO: Monitor should handle shutting down the folder engine.
log.info('Folder disappeared. Stopping sync.',
account_id=self.account_id,
folder_name=self.folder_name,
folder_id=self.folder_id)
raise MailsyncDone()
except ValidationError as exc:
log.error('Error authenticating; stopping sync', exc_info=True,
account_id=self.account_id, folder_id=self.folder_id,
logstash_tag='mark_invalid')
with session_scope(self.namespace_id) as db_session:
account = db_session.query(Account).get(self.account_id)
account.mark_invalid()
account.update_sync_error(str(exc))
raise MailsyncDone()
# State handlers are idempotent, so it's okay if we're
# killed between the end of the handler and the commit.
if self.state != old_state:
#.........这里部分代码省略.........
示例3: BaseSyncMonitor
# 需要导入模块: from inbox.heartbeat.store import HeartbeatStatusProxy [as 别名]
# 或者: from inbox.heartbeat.store.HeartbeatStatusProxy import publish [as 别名]
class BaseSyncMonitor(Greenlet):
"""
Abstracted sync monitor, based on BaseMailSyncMonitor but not mail-specific
Subclasses should run
bind_context(self, 'mailsyncmonitor', account.id)
poll_frequency : int
How often to check for commands.
retry_fail_classes : list
Exceptions to *not* retry on.
"""
def __init__(self, account_id, namespace_id, email_address, folder_id,
folder_name, provider_name, poll_frequency=1,
scope=None):
self.account_id = account_id
self.namespace_id = namespace_id
self.poll_frequency = poll_frequency
self.scope = scope
self.log = logger.new(account_id=account_id)
self.shutdown = event.Event()
self.heartbeat_status = HeartbeatStatusProxy(self.account_id,
folder_id,
folder_name,
email_address,
provider_name)
Greenlet.__init__(self)
def _run(self):
# Bind greenlet-local logging context.
self.log = self.log.new(account_id=self.account_id)
return retry_with_logging(self._run_impl, account_id=self.account_id,
logger=self.log)
def _run_impl(self):
# Return true/false based on whether the greenlet succeeds or throws
# and error. Note this is not how the mailsync monitor works
try:
while True:
# Check to see if this greenlet should exit
if self.shutdown.is_set():
self._cleanup()
return False
try:
self.sync()
self.heartbeat_status.publish(state='poll')
# If we get a connection or API permissions error, then sleep
# 2x poll frequency.
except ConnectionError:
self.log.error('Error while polling', exc_info=True)
self.heartbeat_status.publish(state='poll error')
sleep(self.poll_frequency)
sleep(self.poll_frequency)
except ValidationError:
# Bad account credentials; exit.
self.log.error('Error while establishing the connection',
account_id=self.account_id,
exc_info=True, logstash_tag='mark_invalid')
self._cleanup()
with session_scope(self.namespace_id) as db_session:
account = db_session.query(Account).get(self.account_id)
account.mark_invalid(scope=self.scope)
return False
def sync(self):
""" Subclasses should override this to do work """
raise NotImplementedError
def _cleanup(self):
self.heartbeat_status.clear()