Skip to content

Commit

Permalink
Fix dev environment, misc. fixes
Browse files Browse the repository at this point in the history
- Fix dev flask env
- Add separate `flask run-eventlet` command
- Change dev port to 3000
- Update docker registry URL
- Fix scripts
- Linter noise I can't be fucked to clean up
  • Loading branch information
hndrewaall committed May 9, 2020
1 parent 49919a1 commit 7d4bf54
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 56 deletions.
29 changes: 4 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,32 +56,11 @@ flask shell
Room.query.all()
```

### Running the app locally in production mode
We use multiple workers in production, so we've added an experimental message queue that only gets activated in production mode. This is a tentative plan for Celery; don't worry about this for now.
```bash
# Install kombu (this also comes installed as part of your dev env)
source venv/bin/activate
pip install kombu
# or
pip install -r requirements.txt

# Install rabbitmq
brew install rabbitmq

# If not in path, add it
export PATH=$PATH:/usr/local/opt/rabbitmq/sbin

# Start server
rabbitmq-server

# Run the app in production mode
cd add/
source setup_prod.sh
flask run

# To shut down the rabbitmq server
rabbitmqctl shutdown
### Running with eventlet
```
flask run-eventlet
```


### Jitsi API documentation
API doc:
Expand Down
4 changes: 2 additions & 2 deletions app/client/jsx/WebAPI.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { createAdventureActions } from './utils.js'
// TODO this should go into a config / use process.env
const hostname = window && window.location && window.location.hostname
const production = 'https://party.gbre.org/'
const development = 'http://127.0.0.1:5000'
const development = 'http://127.0.0.1:3000'
const url = hostname === 'party.gbre.org' ? production : development

const PING_INTERVAL_MS = 10000
Expand Down Expand Up @@ -54,7 +54,7 @@ export class HttpApi {
return { success: false }
}
}

async getRooms() {
/* Fetches room definition, including adventure rooms */
try {
Expand Down
10 changes: 5 additions & 5 deletions app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,29 @@

basedir = os.path.abspath(os.path.dirname(__file__))


class Config:
# Need to change this
SECRET_KEY = 'figure-this-out'
# To silence deprecation warning
SQLALCHEMY_TRACK_MODIFICATIONS = False
BASE_DIR = basedir
MESSAGE_QUEUE = None

@staticmethod
def init_app(app):
pass


class DevelopmentConfig(Config):
DEBUG = True
SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'db.sqlite')
MESSAGE_QUEUE = None


class ProductionConfig(Config):
# These URIs are the same for now. If we ever introduce tests we'll want to have
# a test config that has its own db that doesn't interfere with these.
SQLALCHEMY_DATABASE_URI = 'postgresql+psycopg2://waa:woo@jitsi-party-db:5432/jitsi'
# We use multiple workers in production, so we need a message queue to coordinate
# websocket broadcasting
MESSAGE_QUEUE = None


config = {
'development': DevelopmentConfig,
Expand Down
17 changes: 11 additions & 6 deletions app/jitsi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,23 @@
staticdir = os.path.join(basedir, 'client/js')

db = SQLAlchemy()
socketio = SocketIO(async_mode="eventlet")
socketio = SocketIO()

# Register socket events with SocketIO instance
from . import events
from . import events # noqa


# Factory function that returns the created application instance
def create_app(config_name):
'''
Factory function that returns the created application instance
'''
app = Flask(__name__, static_folder=staticdir)
app.config.from_object(config[config_name])
config[config_name].init_app(app)

from jitsi import main

# Endpoints
# Endpoints
app.register_blueprint(main.main)

# Enable cross-origin requests
Expand All @@ -36,5 +39,7 @@ def create_app(config_name):

return app

def run_prod(app):
socketio.run(app, host='0.0.0.0', port=80)

def run_eventlet(app):
socketio.init_app(app, async_mode="eventlet")
socketio.run(app, host='0.0.0.0', port=os.getenv('FLASK_RUN_PORT'))
6 changes: 6 additions & 0 deletions app/jitsi/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,35 @@
from flask_socketio import emit
from .models import User


def broadcast_state():
users = User.get_active_users_by_room()
emit('user-event', users, broadcast=True)


@socketio.on('disconnect')
def on_disconnect():
broadcast_state()


@socketio.on('connect')
def on_connect():
broadcast_state()


@socketio.on('ping-user')
def on_ping(message):
user = User.query.filter_by(id=message['user_id']).first()
if user:
user.ping()


@socketio.on('leave-room')
def on_leave_room(message):
User.leave_room(message['user']['userId'], message['room'])
broadcast_state()


@socketio.on('enter-room')
def on_enter_room(message):
User.enter_room(message['user']['userId'], message['room'])
Expand Down
17 changes: 10 additions & 7 deletions app/manager.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import os
import json
from jitsi import create_app, db, run_prod
from jitsi import create_app, db, run_eventlet
from jitsi.models import Room, User

# Create the application context
app = create_app(os.getenv('FLASK_CONFIG') or 'default')

# Create the application context
app = create_app(os.getenv('FLASK_ENV') or 'default')
# The following are custom flask commands


@app.shell_context_processor
def make_shell_context():
'''Usage: flask shell
Expand All @@ -16,9 +17,10 @@ def make_shell_context():
return dict(db=db, Room=Room, User=User)


@app.cli.command('run-prod')
def run_prod_cmd():
run_prod(app)
@app.cli.command('run-eventlet')
def run_eventlet_cmd():
run_eventlet(app)


@app.cli.command('create-db')
def create_db():
Expand All @@ -38,6 +40,7 @@ def create_db():
rooms = json.load(open('rooms.json'))

# Create rooms and insert
db_rooms = [Room(name=room_name, room_type=room['type']) for room_name, room in rooms.items()]
db_rooms = [Room(name=room_name, room_type=room['type'])
for room_name, room in rooms.items()]
db.session.add_all(db_rooms)
db.session.commit()
7 changes: 3 additions & 4 deletions app/setup.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export FLASK_APP=manager.py
export FLASK_DEBUG=1
export FLASK_CONFIG=development
export FLASK_RUN_PORT=5000
export FLASK_APP="manager.py"
export FLASK_ENV=development
export FLASK_RUN_PORT=3000
2 changes: 0 additions & 2 deletions app/setup_prod.sh

This file was deleted.

2 changes: 1 addition & 1 deletion build_head.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ SCRIPT_DIR=$(dirname "$SCRIPT")

pushd "$SCRIPT_DIR"

DOCKER_REPO="localhost:5000/gbre/jitsi-party"
DOCKER_REPO="docker.gbre.org/jitsi-party"

QUERY='{ repository(owner: \"morganecf\", name: \"jitsi-party\") { releases(last: 1, orderBy: {field: CREATED_AT, direction: ASC}) {edges { node { tagName }}}}}'

Expand Down
2 changes: 1 addition & 1 deletion build_latest_release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ SCRIPT_DIR=$(dirname "$SCRIPT")

pushd "$SCRIPT_DIR"

DOCKER_REPO="localhost:5000/gbre/jitsi-party"
DOCKER_REPO="docker.gbre.org/jitsi-party"

QUERY='{ repository(owner: \"morganecf\", name: \"jitsi-party\") { releases(last: 1, orderBy: {field: CREATED_AT, direction: ASC}) {edges { node { tagName }}}}}'

Expand Down
9 changes: 6 additions & 3 deletions start.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#! /bin/bash

source setup_prod.sh
flask create-db
FLASK_DEBUG=1 PYTHONUNBUFFERED=1 flask run-prod
export FLASK_APP="manager.py"
export FLASK_ENV=production
export FLASK_DEBUG=1
export PYTHONUNBUFFERED=1
export FLASK_RUN_PORT=80
flask run-eventlet

0 comments on commit 7d4bf54

Please sign in to comment.