Skip to content

Commit

Permalink
sync-single-account.py
Browse files Browse the repository at this point in the history
  • Loading branch information
squeaky-pl committed Nov 5, 2024
1 parent 9a87af8 commit 6121f2d
Show file tree
Hide file tree
Showing 5 changed files with 255 additions and 181 deletions.
55 changes: 55 additions & 0 deletions bin/sync-single-account.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/usr/bin/env python

import logging
from threading import BoundedSemaphore

import click

from inbox.logging import configure_logging
from inbox.mailsync.service import get_monitor_classes
from inbox.models.account import Account
from inbox.models.session import global_session_scope


@click.command()
@click.option("--account-id", required=True, type=int, help="Account ID to sync.")
@click.option(
"--folder-name",
default=None,
help="Folder name to sync. If not provided, sync all folders.",
)
def main(account_id: int, folder_name: str) -> None:
"""
Sync a single account.
This script is intended as a debugging tool and can be used to sync
a single account and/or folder. It is useful to sync a single account
to surface issues like memory leaks, high CPU usage, problematic IMAP behavior
or sync halting issues. When syncing multiple accounts in a single process
it might be challenging to attribute these issues to a specific account.
"""
configure_logging(logging.DEBUG)

with global_session_scope() as db_session:
account = db_session.query(Account).get(account_id)
monitor_class = get_monitor_classes()[account.provider]

if folder_name:
folder_sync_engine = monitor_class.sync_engine_class(
account_id,
account.namespace.id,
folder_name,
account.email_address,
account.verbose_provider,
BoundedSemaphore(1),
)
run = folder_sync_engine.run
else:
monitor = monitor_class(account)
run = monitor.run

run()


if __name__ == "__main__":
main()
6 changes: 6 additions & 0 deletions inbox/mailsync/backends/base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import threading
from typing import TYPE_CHECKING, ClassVar

from inbox.config import config
from inbox.interruptible_threading import InterruptibleThread, InterruptibleThreadExit
Expand All @@ -7,6 +8,9 @@
from inbox.util.concurrency import kill_all, retry_with_logging
from inbox.util.debug import bind_context

if TYPE_CHECKING:
from inbox.mailsync.backends.imap.generic import FolderSyncEngine

log = get_logger()

THROTTLE_COUNT = config.get("THROTTLE_COUNT", 200)
Expand All @@ -22,6 +26,8 @@ class MailsyncDone(InterruptibleThreadExit):


class BaseMailSyncMonitor(InterruptibleThread):
sync_engine_class: ClassVar[type["FolderSyncEngine"]]

"""
The SYNC_MONITOR_CLS for all mail sync providers should subclass this.
Expand Down
Loading

0 comments on commit 6121f2d

Please sign in to comment.