-
Notifications
You must be signed in to change notification settings - Fork 90
Migrate Notifications server to python #198 #526
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
stopnoanime
wants to merge
23
commits into
sio2project:master
Choose a base branch
from
stopnoanime:notifications-python
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 22 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
e3584be
Initial rewrite
stopnoanime 3dd609c
Make notifications actually work
stopnoanime 9d71345
Store the user_id in websocket and fix queue unsubscribe
stopnoanime 237fcc1
Add notifications server auth cache
stopnoanime e9768d6
Make logging use django configuration
stopnoanime b2b18d4
Add necessary dependencies to docker image
stopnoanime dc308c6
Add correct types
stopnoanime 8312d72
Add tests
stopnoanime 4eb057e
Make notifications client recconect on error
stopnoanime 7f9d643
Stop multiple authentications on single websocket
stopnoanime 6d2333a
Update notifications related docs
stopnoanime 92aae1b
Close server with descriptive error on invalid URL config
stopnoanime b37cdb7
Don't clear notifications error status when opening notifications dro…
stopnoanime 3930049
Rewrite server to python websockets
stopnoanime 07c5529
Implement graceful shutdown for notifications server
stopnoanime e84cb45
Add some comments
stopnoanime 1a7b0a1
Add proper getLogger initialization
stopnoanime 5223081
Change notifications authenticate endpoint to use Http status codes t…
stopnoanime a045127
Remove unused packages that were required by socketify.py
stopnoanime 57d3ed7
Update setup.py
stopnoanime 57d21ca
Merge branch 'master' into notifications-python
stopnoanime 28b784c
Fix ruff errors for notifications server
stopnoanime 225bce4
Remove unused imports
stopnoanime File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 8 additions & 26 deletions
34
oioioi/notifications/management/commands/notifications_server.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,36 +1,18 @@ | ||
| import os | ||
| import asyncio | ||
|
|
||
| from django.conf import settings | ||
| from django.core.management.base import BaseCommand | ||
|
|
||
| from oioioi.notifications.server.server import Server | ||
|
|
||
|
|
||
| class Command(BaseCommand): | ||
| help = "Runs the OIOIOI notifications server" | ||
| requires_model_validation = False | ||
|
|
||
| def add_arguments(self, parser): | ||
| parser.add_argument( | ||
| "-i", | ||
| "--install", | ||
| action="store_true", | ||
| help="install dependencies required by the server", | ||
| ) | ||
|
|
||
| def handle(self, *args, **options): | ||
| path = os.path.join(os.path.dirname(__file__), "..", "..", "server") | ||
| os.chdir(path) | ||
| if options["install"]: | ||
| os.execlp("env", "env", "npm", "install") | ||
| else: | ||
| os.execlp( | ||
| "env", | ||
| "env", | ||
| "node", | ||
| "ns-main.js", | ||
| "--port", | ||
| settings.NOTIFICATIONS_SERVER_PORT.__str__(), | ||
| "--url", | ||
| settings.NOTIFICATIONS_OIOIOI_URL, | ||
| "--amqp", | ||
| settings.NOTIFICATIONS_RABBITMQ_URL, | ||
| ) | ||
| server = Server(settings.NOTIFICATIONS_SERVER_PORT, settings.NOTIFICATIONS_RABBITMQ_URL, settings.NOTIFICATIONS_OIOIOI_URL) | ||
| try: | ||
| asyncio.run(server.run()) | ||
| except KeyboardInterrupt: | ||
| pass # Allow graceful shutdown on Ctrl+C |
This file was deleted.
Oops, something went wrong.
Empty file.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import logging | ||
| from typing import Optional | ||
|
|
||
| import aiohttp | ||
| from cachetools import TTLCache | ||
|
|
||
|
|
||
| class Auth: | ||
| AUTH_CACHE_EXPIRATION_SECONDS = 300 | ||
| AUTH_CACHE_MAX_SIZE = 10000 | ||
| URL_AUTHENTICATE_SUFFIX = "notifications/authenticate/" | ||
|
|
||
| def __init__(self, url: str): | ||
| self.auth_url = url + self.URL_AUTHENTICATE_SUFFIX | ||
| self.auth_cache: TTLCache[str, str] = TTLCache(maxsize=self.AUTH_CACHE_MAX_SIZE, ttl=self.AUTH_CACHE_EXPIRATION_SECONDS) | ||
| self.logger = logging.getLogger(__name__) | ||
| self.http_client: aiohttp.ClientSession | None = None | ||
|
|
||
| async def connect(self): | ||
| self.http_client = aiohttp.ClientSession() | ||
|
|
||
| async def close(self): | ||
| if self.http_client is not None: | ||
| await self.http_client.close() | ||
| self.http_client = None | ||
| self.logger.info("HTTP client closed") | ||
|
|
||
| async def authenticate(self, session_id: str) -> str: | ||
| """ | ||
| Authenticate a user with session ID. | ||
|
|
||
| Returns the user ID if authentication is successful. | ||
| Raises RuntimeError if authentication fails. | ||
| """ | ||
| if self.http_client is None: | ||
| raise RuntimeError("Connection not established. Call connect() first.") | ||
|
|
||
| if session_id in self.auth_cache: | ||
| user_id = self.auth_cache[session_id] | ||
| self.logger.debug(f"Cache hit for session ID: {session_id} with user ID: {user_id}") | ||
| return user_id | ||
|
|
||
| async with self.http_client.post(self.auth_url, data={"nsid": session_id}, headers={"Content-Type": "application/x-www-form-urlencoded"}) as response: | ||
| response.raise_for_status() | ||
| result = await response.json() | ||
|
|
||
| user_id = result["user"] | ||
| self.auth_cache[session_id] = user_id | ||
|
|
||
| self.logger.debug(f"Authenticated session ID: {session_id} with user ID: {user_id}") | ||
| return user_id | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.