[go: nahoru, domu]

Skip to content
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

Logging via websockets #76

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
websocket logging
  • Loading branch information
mariusandra committed Apr 7, 2024
commit 9284a4d95d2192559768f71aed34167895ade9fb
4 changes: 3 additions & 1 deletion backend/app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from flask_login import LoginManager
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flask_sock import Sock
from flask_socketio import SocketIO
from config import Config, get_config
from urllib.parse import urlparse
Expand All @@ -22,6 +23,7 @@
login_manager.login_view = 'api.login'
migrate = Migrate()
socketio = SocketIO(async_mode='gevent')
sock = Sock()

DEFAULT_REDIS_URL = 'redis://localhost:6379/0'

Expand Down Expand Up @@ -52,14 +54,14 @@ def create_app(config: Optional[Config] = None):
config = config or get_config()
app = Flask(__name__, static_folder='../../frontend/dist', static_url_path='/', template_folder='../templates')
app.config.from_object(config)

db.init_app(app)
with app.app_context():
os.makedirs('../db', exist_ok=True)

db.create_all()
login_manager.init_app(app)
migrate.init_app(app, db)
sock.init_app(app)
socketio.init_app(app, cors_allowed_origins="*", message_queue=os.environ.get('REDIS_URL', DEFAULT_REDIS_URL))
initialize_sentry(app)

Expand Down
1 change: 1 addition & 0 deletions backend/app/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@
from .settings import *
from .templates import *
from .misc import *
from .sock import *
7 changes: 7 additions & 0 deletions backend/app/api/sock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from app import sock

@sock.route('/echo')
def echo(ws):
while True:
data = ws.receive()
ws.send(data)
3 changes: 2 additions & 1 deletion backend/requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ pytest
pip-tools
pre-commit
ruff
imagehash
imagehash
flask-sock
9 changes: 9 additions & 0 deletions backend/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ flask==2.3.2
# -r requirements.in
# flask-login
# flask-migrate
# flask-sock
# flask-socketio
# flask-sqlalchemy
# flask-wtf
Expand All @@ -69,6 +70,8 @@ flask-login==0.6.3
# via -r requirements.in
flask-migrate==4.0.4
# via -r requirements.in
flask-sock==0.7.0
# via -r requirements.in
flask-socketio==5.3.5
# via -r requirements.in
flask-sqlalchemy==3.0.5
Expand All @@ -85,6 +88,8 @@ gevent-websocket==0.10.1
# via -r requirements.in
greenlet==2.0.2
# via gevent
h11==0.14.0
# via wsproto
honcho==1.1.0
# via -r requirements.in
huey==2.4.5
Expand Down Expand Up @@ -191,6 +196,8 @@ scp==0.14.5
# via -r requirements.in
sentry-sdk[flask]==1.35.0
# via -r requirements.in
simple-websocket==1.0.0
# via flask-sock
six==1.16.0
# via asttokens
sqlalchemy==2.0.19
Expand Down Expand Up @@ -222,6 +229,8 @@ werkzeug==2.3.6
# flask-login
wheel==0.41.3
# via pip-tools
wsproto==1.2.0
# via simple-websocket
wtforms==3.0.1
# via
# -r requirements.in
Expand Down
19 changes: 17 additions & 2 deletions frameos/src/frameos/logger.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import httpclient, zippy, json, sequtils, os, times, math
import asyncdispatch, asynchttpserver, ws

import frameos/channels
import frameos/types
Expand All @@ -17,7 +18,21 @@ const LOG_FLUSH_SECONDS = 1.0
var threadInitDone = false
var thread: Thread[FrameConfig]

proc run(self: LoggerThread) =
proc run(self: LoggerThread) {.async.} =
let protocol = if self.frameConfig.serverPort mod 1000 == 443: "wss" else: "ws"
let url = protocol & "://" & self.frameConfig.serverHost & ":" & $self.frameConfig.serverPort & "/echo"

echo "1"
var ws = await newWebSocket(url)
echo "2"
# echo await ws.receiveStrPacket()
await ws.send("Hi, how are you?")
echo "3"
echo await ws.receiveStrPacket()
echo "4"
ws.close()
echo "5"

var attempt = 0
while true:
let logCount = (self.logs.len + self.erroredLogs.len)
Expand Down Expand Up @@ -82,7 +97,7 @@ proc createThreadRunner(frameConfig: FrameConfig) {.thread.} =
)
while true:
try:
run(loggerThread)
waitFor run(loggerThread)
except Exception as e:
echo "Error in logger thread: " & $e.msg
sleep(1000)
Expand Down
Loading