Skip to content

Commit

Permalink
Async SQLAlchemy
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrunner committed Dec 30, 2024
1 parent f0cd231 commit e168dc2
Show file tree
Hide file tree
Showing 11 changed files with 260 additions and 190 deletions.
4 changes: 4 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ repos:
- --tool=ruff
- --die-on-tool-error
- --output-format=pylint
exclude: |
(?x)^(
tilecloud_chain/tests/.*
)$
additional_dependencies:
- prospector-profile-duplicated==1.9.0 # pypi
- prospector-profile-utils==1.14.1 # pypi
Expand Down
4 changes: 2 additions & 2 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ services:
<<: *app-env
TEST_USER: Test
SQL_LOG_LEVEL: DEBUG
TILECLOUD_CHAIN_SQLALCHEMY_URL: postgresql+psycopg2://postgresql:postgresql@db:5432/tests
TILECLOUD_CHAIN_SQLALCHEMY_URL: postgresql+pyscopg://postgresql:postgresql@db:5432/tests
volumes:
- ./example/tilegeneration/config-postgresql.yaml:/etc/tilegeneration/config.yaml:ro
ports:
Expand Down Expand Up @@ -105,7 +105,7 @@ services:
TILECLOUD_LOG_LEVEL: DEBUG
TILECLOUD_CHAIN_LOG_LEVEL: DEBUG
TILECLOUD_CHAIN_SESSION_SALT: a-long-secret-a-long-secret
TILECLOUD_CHAIN_SQLALCHEMY_URL: postgresql+psycopg2://postgresql:postgresql@db:5432/tests
TILECLOUD_CHAIN_SQLALCHEMY_URL: postgresql+psycopg://postgresql:postgresql@db:5432/tests
command:
- sleep
- infinity
Expand Down
22 changes: 20 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ psutil = "6.1.0"
pyproj = "3.7.0"
psycopg = { version = "3.2.3", extras = ["binary"] }
aiohttp = "3.11.11"
sqlalchemy = { version = "2.0.36", extras = ["asyncio"] }
pytest-asyncio = "0.25.0"

[tool.poetry.group.dev.dependencies]
prospector = { extras = ["with_mypy", "with_bandit", "with_pyroma", "with_ruff"], version = "1.13.3" }
Expand Down
4 changes: 2 additions & 2 deletions tilecloud_chain/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1888,7 +1888,7 @@ def _await_message(_: Any) -> bool:
raise StopAsyncIteration # pylint: disable=raise-missing-from


def get_queue_store(config: DatedConfig, daemon: bool) -> TimedTileStoreWrapper:
async def get_queue_store(config: DatedConfig, daemon: bool) -> TimedTileStoreWrapper:
"""Get the quue tile store."""
queue_store = config.config.get("queue_store", configuration.QUEUE_STORE_DEFAULT)

Expand All @@ -1899,7 +1899,7 @@ def get_queue_store(config: DatedConfig, daemon: bool) -> TimedTileStoreWrapper:
)

return TimedTileStoreWrapper(
get_postgresql_queue_store(config),
await get_postgresql_queue_store(config),
store_name="postgresql",
)
elif queue_store == "redis":
Expand Down
15 changes: 10 additions & 5 deletions tilecloud_chain/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@


def main(args: list[str] | None = None, out: IO[str] | None = None) -> None:
"""Generate the contextual file like the legends."""
asyncio.run(_async_main(args, out))


async def _async_main(args: list[str] | None = None, out: IO[str] | None = None) -> None:
"""Generate the contextual file like the legends."""
try:
parser = ArgumentParser(
Expand Down Expand Up @@ -76,7 +81,7 @@ def main(args: list[str] | None = None, out: IO[str] | None = None) -> None:
config = gene.get_config(gene.config_file)

if options.status:
status(gene)
await status(gene)
sys.exit(0)

if options.cache is None:
Expand Down Expand Up @@ -525,15 +530,15 @@ def _generate_legend_images(gene: TileGeneration, out: IO[str] | None = None) ->
)


def status(gene: TileGeneration) -> None:
async def status(gene: TileGeneration) -> None:
"""Print th tilegeneration status."""
print("\n".join(get_status(gene)))
print("\n".join(await get_status(gene)))


def get_status(gene: TileGeneration) -> list[str]:
async def get_status(gene: TileGeneration) -> list[str]:
"""Get the tile generation status."""
config = gene.get_main_config()
store = get_queue_store(config, False)
store = await get_queue_store(config, False)
type_: Literal["redis"] | Literal["sqs"] = "redis" if "redis" in config.config else "sqs"
conf = config.config[type_]
with _GET_STATUS_SUMMARY.labels(type_, conf.get("queue", configuration.REDIS_QUEUE_DEFAULT)).time():
Expand Down
6 changes: 3 additions & 3 deletions tilecloud_chain/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def __init__(

async def init(self, server: bool = False) -> None:
"""Initialize the generation."""
self._generate_init()
await self._generate_init()
if self._options.role != "master" and not server:
await self._generate_tiles()

Expand Down Expand Up @@ -122,14 +122,14 @@ async def gene(self, layer_name: str | None = None) -> None:
await self.generate_consume()
self.generate_resume(layer_name)

def _generate_init(self) -> None:
async def _generate_init(self) -> None:
if self._options.role != "server":
self._count_metatiles_dropped = Count()
self._count_tiles = Count()
self._count_tiles_dropped = Count()

if self._options.role in ("master", "slave") and not self._options.tiles:
self._queue_tilestore = get_queue_store(self._gene.get_main_config(), self._options.daemon)
self._queue_tilestore = await get_queue_store(self._gene.get_main_config(), self._options.daemon)

if self._options.role in ("local", "master"):
self._gene.add_geom_filter()
Expand Down
Loading

0 comments on commit e168dc2

Please sign in to comment.