Skip to content
Open
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
34 changes: 34 additions & 0 deletions devops/nginx-static-loadbalance.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Define upstream servers
upstream extension_server { server localhost:8888; }
upstream dashboard_server { server localhost:8889; }

map $arg_source $upstream_server {
default dashboard_server; # default if no match or query param is absent
"extension" extension_server;
"dashboard" dashboard_server;
}

server {
listen 80;

location / {
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";

# Pass original host to the upstream server
proxy_set_header Host $host;

# Other WebSocket headers
proxy_http_version 1.1;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;

# Route the request to the selected upstream server
proxy_pass http://$upstream_server;
proxy_read_timeout 3600;
proxy_send_timeout 3600;
}
}


17 changes: 17 additions & 0 deletions docs/nginx.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# NGINX Setup and Config

This document contains basic info related to using nginx in the context of writing observer.

## Static Routing Use Case Config

In order to improve event processing efficiency, we will use nginx to route requests to different processes based on whether they are incoming data events (from the extension), or requests for data (from the dashboard). The associated config can be found at `/devops/nginx-static-loadbalance.conf` in this repository.

At the top of that file, the `upstream`s indicate the servers that should be used for each type of request, `dashboard` or `extension`.

The next section, which includes the `map` directive, tells nginx to look for a url query parameter called source (`$arg_source`: `$arg` indicates that it is a url query param, and `source` gives the name). If `source=dashboard`, then it routes to the dashboard server, if `source=extension`, it routes to the extension server, and if no `source` is provided, then it defaults to the dashboard server.

Below, in the `server` block, are general instructions for handling proxying.

## Usage

To deploy nginx, take the appropriate `.conf` file and put it in `/etc/nginx/sites-enabled`. Then, you can use `sudo nginx -t` to validate the config and ensure it is correct--this will print any detected syntax errors in the config files. Finally, restart nginx to apply the new config (for example, with `sudo systemctl restart nginx`)
2 changes: 1 addition & 1 deletion extension/writing-process/src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var RAW_DEBUG = false;
/* This variable must be manually updated to specify the server that
* the data will be sent to.
*/
var WEBSOCKET_SERVER_URL = "wss://learning-observer.org/wsapi/in/"
var WEBSOCKET_SERVER_URL = "wss://learning-observer.org/wsapi/in?source=extension"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We really ought to have the url not hardcoded and pulled from a config of some sort, but that's way beyond the scope of this PR and the work you are doing.
Mostly leaving this note to push it to the front of my brain (I'm most likely to work on it)


import { googledocs_id_from_url } from './writing_common';

Expand Down
12 changes: 6 additions & 6 deletions learning_observer/learning_observer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@
# Run argparse
args = settings.parse_and_validate_arguments()

# This will need to move but for the moment we hack with
# this to prefer the GPU where possible.
# This will need to move but for the moment we hack with
# this to prefer the GPU where possible.
import spacy
#spacy.prefer_gpu()
#debug_log("Preferring GPU Use.")
spacy.require_gpu()
debug_log("Requiring GPU Use.")
# spacy.prefer_gpu()
# debug_log("Preferring GPU Use.")
# spacy.require_gpu()
# debug_log("Requiring GPU Use.")


def configure_event_loop():
Expand Down
30 changes: 28 additions & 2 deletions learning_observer/learning_observer/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import getpass
import os
import pmss
import secrets
import sys

Expand Down Expand Up @@ -33,6 +34,20 @@

from learning_observer.utility_handlers import *

pmss.register_field(
name='disable_extension_routes',
type=pmss.pmsstypes.TYPES.boolean,
description='Whether to disable extension-related API routes',
default=False
)

pmss.register_field(
name='disable_dashboard_routes',
type=pmss.pmsstypes.TYPES.boolean,
description='Whether to disable dashboard-related API routes',
default=False
)


def add_routes(app):
'''
Expand Down Expand Up @@ -62,9 +77,20 @@ def tracemalloc_handler(request):
aiohttp.web.get('/debug/tracemalloc/', tracemalloc_handler),
])

register_dashboard_api(app)
if not settings.pmss_settings.disable_dashboard_routes(types=['server']):
register_dashboard_api(app)
debug_log("Dashbord routes are enabled")
else:
debug_log("Dashboard routes are disabled")

register_static_routes(app)
register_incoming_event_views(app)

if not settings.pmss_settings.disable_extension_routes(types=['server']):
register_incoming_event_views(app)
debug_log("Extension routes are enabled")
else:
debug_log("Extension routes are disabled")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

General comment: I could see instances where we want to enable/disable a lot more of the Learning Observer routes on the system.
Not sure that it's needed for the first pass, but would be good to think about and toy around with.

register_debug_routes(app)
learning_observer.google.initialize_and_register_routes(app)

Expand Down
6 changes: 3 additions & 3 deletions learning_observer/util/stream_writing.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
[--gpt3=type]

Options:
--url=url URL to connect [default: http://localhost:8888/wsapi/in/]
--url=url URL to connect [default: http://localhost:80/wsapi/in/]
--streams=N How many students typing in parallel? [default: 1]
--users=user_id,uid,uid Supply the user ID
--ici=secs,secs Mean intercharacter interval [default: 0.1]
Expand Down Expand Up @@ -129,7 +129,7 @@ def argument_list(argument, default):
elif ARGS['--fake-name']:
USERS = [names.get_first_name() for i in range(STREAMS)]
else:
USERS = ["test-user-{n}".format(n=i) for i in range(STREAMS)]
USERS = [f"test-user-{i}" for i in range(STREAMS)]

assert len(TEXT) == STREAMS, "len(filenames) != STREAMS."
assert len(ICI) == STREAMS, "len(ICIs) != STREAMS."
Expand Down Expand Up @@ -184,7 +184,7 @@ async def stream_document(text, ici, user, doc_id):
'''
retries_remaining = 5
done = False
url = ARGS["--url"]
url = ARGS["--url"] + "?source=extension"
while not done:
try:
async with aiohttp.ClientSession() as session:
Expand Down
45 changes: 45 additions & 0 deletions servermanagement/RunTwoLO.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env bash
# ===============================
# This a modified version RunLearningObserver.sh that automatically starts two processes,
# each with a different creds.yaml ('creds-a.yaml' and 'creds-b.yaml')--this should be
# used for static routing, where one creds.yaml has only dashboard routes enabled,
# and the other has only extension routes enabled. Note that these creds.yaml should
# specify different ports.
#
# This bash script provides a simple wrapper to run the
# learning observer service and pipe the data to a logfile
# over time this should be integrated into the systemd
# service process. This uses static variables to specify
# the location of the virtualenv and the command and
# specifies the location for the running logfile.

# System Variables
# --------------------------------------
VIRTUALENV_PATH="/usr/local/share/projects/WritingObserver/VirtualENVs/WOvenv"
#VIRTUALENV_PYTHON="/usr/local/share/Projects/WritingObserver/VirtualENVs/learning_observer/bin/python3.9"
LEARNING_OBSERVER_LOC="/usr/local/share/projects/WritingObserver/Repositories/ArgLab_writing_observer/learning_observer"
LOGFILE_DEST="/usr/local/share/projects/WritingObserver/Repositories/ArgLab_writing_observer/learning_observer/learning_observer/logs"

# Make the logfile name
# ---------------------------------------
LOG_DATE=$(date "+%m-%d-%Y--%H-%M-%S")
LOGFILE_NAME="$LOGFILE_DEST/learning_observer_service_$LOG_DATE.log"
echo $LOG_NAME;


# Run both processes
# --------------------------------------
echo "Running Learning Observer Service..."
cd $LEARNING_OBSERVER_LOC
source $VIRTUALENV_PATH/bin/activate
nohup python learning_observer --config-file=creds-a.yaml > $LOGFILE_NAME 2>&1 &
PROCESS_ID=$!
echo $PROCESS_ID > $LOGFILE_DEST/run.pid

# NOTE: if this should go to separate log file location, modify here
nohup python learning_observer --config-file=creds-b.yaml > $LOGFILE_NAME 2>&1 &
PROCESS_ID=$!
echo $PROCESS_ID > $LOGFILE_DEST/run.pid

# Set the number of allowed open files to something large 8192
prlimit --pid $PROCESS_ID --nofile=8192
Loading