Skip to content

Commit

Permalink
Bring database_sync_to_async into repo
Browse files Browse the repository at this point in the history
Removes the bulk of having Channels installed when no functionality from the app aside from this command is being used.
  • Loading branch information
smk4664 committed Dec 20, 2024
1 parent 422c0e1 commit 6e65ebd
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 24 deletions.
1 change: 1 addition & 0 deletions NOTICE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Some parts of Nautobot-App-Chatops are based upon `channels`, licensed under BSD-3-Clause by Django Software Foundation.
3 changes: 1 addition & 2 deletions nautobot_chatops/sockets/slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
import json
import shlex

from channels.db import database_sync_to_async
from django.conf import settings
from slack_sdk.socket_mode.aiohttp import SocketModeClient
from slack_sdk.socket_mode.request import SocketModeRequest
from slack_sdk.socket_mode.response import SocketModeResponse
from slack_sdk.web.async_client import AsyncWebClient

from nautobot_chatops.dispatchers.slack import SlackDispatcher
from nautobot_chatops.utils import socket_check_and_enqueue_command
from nautobot_chatops.utils import database_sync_to_async, socket_check_and_enqueue_command
from nautobot_chatops.workers import commands_help, get_commands_registry, parse_command_string


Expand Down
20 changes: 19 additions & 1 deletion nautobot_chatops/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
import sys
from datetime import datetime, timezone

from channels.db import database_sync_to_async
from asgiref.sync import SyncToAsync
from django.conf import settings
from django.db import close_old_connections
from django.db.models import Q
from django.http import HttpResponse, JsonResponse
from nautobot.core.celery import nautobot_task
Expand All @@ -18,6 +19,23 @@
logger = logging.getLogger(__name__)


class DatabaseSyncToAsync(SyncToAsync):
"""
SyncToAsync version that cleans up old database connections when it exits.
Sourced from Channels, see NOTICE file for license information.
"""

def thread_handler(self, loop, *args, **kwargs):
close_old_connections()
try:
return super().thread_handler(loop, *args, **kwargs)
finally:
close_old_connections()


# The class is TitleCased, but we want to encourage use as a callable/decorator
database_sync_to_async = DatabaseSyncToAsync

def get_app_config_part(prefix: str) -> dict:
"""Get part of the app config.
Expand Down
21 changes: 1 addition & 20 deletions poetry.lock

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

1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ aiodns = "^1.0"
aiohttp = "^3.7.3"
asgiref = "^3.4.1"
certifi = { version = ">=2021.5.30", optional = true }
channels = "^4.2.0"
cloudvision = { version = "^1.1", optional = true }
cvprac = { version = "^1.0.6", optional = true }
defusedxml = { version = "^0.7.1", optional = true }
Expand Down

0 comments on commit 6e65ebd

Please sign in to comment.