Skip to content

Commit

Permalink
Fix non-updating config to api
Browse files Browse the repository at this point in the history
  • Loading branch information
dtcooper committed Nov 13, 2023
1 parent 842f5d1 commit 79a5a08
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 16 deletions.
16 changes: 13 additions & 3 deletions server/api/server_messages.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import asyncio
import json
import logging
import math

import psycopg
import redis.asyncio as redis
Expand All @@ -17,6 +18,8 @@


logger = logging.getLogger(__name__)
DB_QUEUE_DEDUPE_TIMEOUT = 0.2
DB_QUEUE_NUM_DEDUPE_TIMEOUTS_WITH_NO_MESSAGES_BEFORE_REFRESH = math.floor(90 / DB_QUEUE_DEDUPE_TIMEOUT) # 90 seconds


class ServerMessages(MessagesBase):
Expand Down Expand Up @@ -90,20 +93,27 @@ async def consume_redis_notifications(self):
redis_message = await pubsub.get_message(ignore_subscribe_messages=True)
if redis_message:
message = json.loads(redis_message["data"])
await self.process(message_type=message["type"], message=message["data"])
await self.process(message["type"], message["data"])

@task
async def consume_db_notifications_debouncer(self):
messages = []
num_timeouts_with_no_messages = 0

while True:
try:
messages.append(await asyncio.wait_for(self.pending_db_changes.get(), timeout=0.2))
messages.append(await asyncio.wait_for(self.pending_db_changes.get(), timeout=DB_QUEUE_DEDUPE_TIMEOUT))
except asyncio.TimeoutError:
# When we have received at least one db change for the timeout, then process them (debounce)
if messages:
await self.process(message_type=self.Types.DB_CHANGES, message=messages)
await self.process(self.Types.DB_CHANGES, messages)
messages = []
num_timeouts_with_no_messages = 0
elif num_timeouts_with_no_messages >= DB_QUEUE_NUM_DEDUPE_TIMEOUTS_WITH_NO_MESSAGES_BEFORE_REFRESH:
await self.process(self.Types.DB_CHANGES, [{"table": "forced/update", "op": "update"}])
num_timeouts_with_no_messages = 0
else:
num_timeouts_with_no_messages += 1

@task
async def consume_db_notifications(self):
Expand Down
11 changes: 0 additions & 11 deletions server/tomato/management/commands/constance.py

This file was deleted.

3 changes: 1 addition & 2 deletions server/tomato/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"django.contrib.messages",
"django.contrib.staticfiles",
# Third-party
"constance",
"django_file_form",
"huey.contrib.djhuey",
"pgtrigger",
Expand All @@ -82,8 +83,6 @@
INSTALLED_APPS.extend([
# Local
"tomato",
# Third-party, constance needs to come after tomato, since we override a management command
"constance",
])

AUTH_USER_MODEL = "tomato.User"
Expand Down

0 comments on commit 79a5a08

Please sign in to comment.