Progress Updater
Progress Updater
- class progress_updater.updater.ProgressUpdater(task_name: str, uuid: Optional[UUID] = None, suppress_exception: bool = True, verbose: bool = True, write_on_backend: bool = True, settings: Optional[Union[MongoSettings, RedisSettings, SQLSettings]] = None)
Progress Updater. Defines the Progress Updater Class.
Example
>>> from progress_updater import ProgressUpdater >>> from progress_updater.backends import MongoSettings >>> >>> settings = MongoSettings( >>> mongo_connection="mongodb://user:pass@mongo:27017", >>> mongo_db="db", >>> mongo_collection="logs", >>> ) >>> updater = ProgressUpdater(task_name="My Task", settings=settings) >>> >>> with updater(block_name="First part") as updater: >>> # doing things >>> updater.notify("doing first block...") >>> # doing more things >>> >>> with updater(block_name="Second part"): >>> # doing things >>> updater.notify("doing second block...") >>> # doing more things >>> >>> updater.raise_latest_exception()
Backends There are three backends available to save our logs, Mongo, Redis and SQL.
Settings. Different ways to pass settings to the ProgressUpdater.
1. Passing settings as parameters when creating a ProgressUpdater object like in the above example:
>>> from progress_updater import ProgressUpdater >>> from progress_updater import ProgressUpdater >>> from progress_updater.backends import MongoSettings >>> from progress_updater.backends import SQLSettings >>> from progress_updater.backends import RedisSettings >>> >>> mongo_settings = MongoSettings( >>> mongo_connection="mongodb://user:pass@mongo:27017", >>> mongo_db="db", >>> mongo_collection="logs", >>> ) >>> sql_settings = SQLSettings( >>> sql_dsn="postgresql+psycopg2://user:pass@postgres:5432/db", >>> sql_table="logs", >>> ) >>> redis_settings = RedisSettings( >>> redis_host="redis", redis_password="pass" >>> )
2. Environment variables. Set you setting parameters in your environment. The PU__ prefix indicates that belongs to the ProgressUpdater settings. The ProgressUpdater will catch these settings if the option write_on_backend is set to True.
Examples:
SQL:
PU__SQL_DSN='postgresql+psycopg2://user:pass@postgres:5432/db' PU__SQL_TABLE='logs'
Redis:
PU__REDIS_HOST='redis' PU__REDIS_DB='1' PU__REDIS_PASSWORD='pass'
Mongo:
PU__MONGO_CONNECTION='mongodb://user:pass@mongo:27017' PU__MONGO_DB='db' PU__MONGO_COLLECTION='logs'
And then when creating a ProgressUpdater object, the backend will be automatically configured:
>>> from progress_updater import ProgressUpdater >>> updater = ProgressUpdater(task_name="My Task")
- notify(message: str)
Stores a message in the backend. If verbose is set to True, then the message will be printed.
- raise_latest_exception()
Raise latest exception
Decorator
- progress_updater.utils.progress_updater(task_name: Optional[str] = None, verbose: bool = True, suppress_exception: bool = True, raise_latest_exception: bool = False, write_on_backend: bool = True, settings: Optional[Union[MongoSettings, RedisSettings, SQLSettings]] = None)
Progress Updater Decorator. Defines the Progress Updater as decorator
- Basic example:
>>> from progress_updater.utils import progress_updater >>> >>> @progress_updater >>> def task(): >>> return "Hello World" >>> >>> task()
Example passing settings:
>>> from progress_updater.utils import progress_updater >>> from progress_updater.backends import MongoSettings >>> >>> @progress_updater( >>> task_name="My task", >>> suppress_exception=False, >>> settings = MongoSettings( >>> mongo_connection="mongodb://user:pass@mongo:27017", >>> mongo_db="db", >>> mongo_collection="logs", >>> ) >>> ) >>> def task(): >>> return "Hello World" >>> >>> task()