|
| 1 | +# This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +# file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 4 | +from contextvars import copy_context |
| 5 | + |
| 6 | +import asyncio |
| 7 | +from concurrent.futures import Executor |
| 8 | + |
| 9 | +import structlog |
| 10 | +from functools import partial |
| 11 | +from typing import Optional |
| 12 | + |
| 13 | +import flowmachine |
| 14 | +from flowmachine.core import Connection |
| 15 | +from flowmachine.core.cache import shrink_below_size |
| 16 | +from flowmachine.core.context import get_db, get_executor |
| 17 | +from flowmachine.core.server.server_config import get_server_config |
| 18 | + |
| 19 | +logger = structlog.get_logger("flowmachine.debug", submodule=__name__) |
| 20 | + |
| 21 | + |
| 22 | +async def watch_and_shrink_cache( |
| 23 | + *, |
| 24 | + flowdb_connection: "Connection", |
| 25 | + pool: Executor, |
| 26 | + sleep_time: int = 86400, |
| 27 | + timeout: Optional[int] = 600, |
| 28 | + loop: bool = True, |
| 29 | + size_threshold: int = None, |
| 30 | + dry_run: bool = False, |
| 31 | + protected_period: Optional[int] = None, |
| 32 | +) -> None: |
| 33 | + """ |
| 34 | + Background task to periodically trigger a shrink of the cache. |
| 35 | +
|
| 36 | + Parameters |
| 37 | + ---------- |
| 38 | + flowdb_connection : Connection |
| 39 | + Flowdb connection to check dates on |
| 40 | + pool : Executor |
| 41 | + Executor to run the date check with |
| 42 | + sleep_time : int, default 86400 |
| 43 | + Number of seconds to sleep for between checks |
| 44 | + timeout : int or None, default 600 |
| 45 | + Seconds to wait for a cache shrink to complete before cancelling it |
| 46 | + loop : bool, default True |
| 47 | + Set to false to return after the first check |
| 48 | + size_threshold : int, default None |
| 49 | + Optionally override the maximum cache size set in flowdb. |
| 50 | + dry_run : bool, default False |
| 51 | + Set to true to just report the objects that would be removed and not remove them |
| 52 | + protected_period : int, default None |
| 53 | + Optionally specify a number of seconds within which cache entries are excluded. If None, |
| 54 | + the value stored in cache.cache_config will be used.Set to a negative number to ignore cache protection |
| 55 | + completely. |
| 56 | +
|
| 57 | + Returns |
| 58 | + ------- |
| 59 | + None |
| 60 | +
|
| 61 | + """ |
| 62 | + shrink_func = partial( |
| 63 | + shrink_below_size, |
| 64 | + connection=flowdb_connection, |
| 65 | + size_threshold=size_threshold, |
| 66 | + dry_run=dry_run, |
| 67 | + protected_period=protected_period, |
| 68 | + ) |
| 69 | + while True: |
| 70 | + logger.debug("Checking if cache should be shrunk.") |
| 71 | + |
| 72 | + try: # Set the shrink function running with a copy of the current execution context (db conn etc) in background thread |
| 73 | + await asyncio.wait_for( |
| 74 | + asyncio.get_running_loop().run_in_executor( |
| 75 | + pool, copy_context().run, shrink_func |
| 76 | + ), |
| 77 | + timeout=timeout, |
| 78 | + ) |
| 79 | + except (TimeoutError, asyncio.exceptions.TimeoutError): |
| 80 | + logger.error( |
| 81 | + f"Failed to complete cache shrink within {timeout}s. Trying again in {sleep_time}s." |
| 82 | + ) |
| 83 | + if not loop: |
| 84 | + break |
| 85 | + await asyncio.sleep(sleep_time) |
| 86 | + |
| 87 | + |
| 88 | +def main(): |
| 89 | + # Read config options from environment variables |
| 90 | + config = get_server_config() |
| 91 | + # Connect to flowdb |
| 92 | + flowmachine.connect() |
| 93 | + |
| 94 | + if config.debug_mode: |
| 95 | + logger.info( |
| 96 | + "Enabling asyncio's debugging mode.", |
| 97 | + sleep_time=config.cache_pruning_frequency, |
| 98 | + timeout=config.cache_pruning_timeout, |
| 99 | + ) |
| 100 | + |
| 101 | + logger.info( |
| 102 | + "Starting cache cleanup service.", |
| 103 | + ) |
| 104 | + # Run loop which periodically checks if the cache can/should be resized |
| 105 | + asyncio.run( |
| 106 | + watch_and_shrink_cache( |
| 107 | + flowdb_connection=get_db(), |
| 108 | + pool=get_executor(), |
| 109 | + sleep_time=config.cache_pruning_frequency, |
| 110 | + timeout=config.cache_pruning_timeout, |
| 111 | + ), |
| 112 | + debug=config.debug_mode, |
| 113 | + ) # note: asyncio.run() requires Python 3.7+ |
| 114 | + |
| 115 | + |
| 116 | +if __name__ == "__main__": |
| 117 | + main() |
0 commit comments