Skip to content

Commit

Permalink
dev: reload & build web assets in iris dev server
Browse files Browse the repository at this point in the history
  • Loading branch information
Qingping Hou authored and jrgp committed May 16, 2017
1 parent c983df6 commit 2fc72a4
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
all: serve

serve:
iris-server ./configs/config.dev.yaml
iris-dev ./configs/config.dev.yaml

sender:
iris-sender configs/config.dev.yaml
Expand Down
5 changes: 3 additions & 2 deletions Procfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
web: make serve
web: iris-dev ./configs/config.dev.yaml --skip-build-assets
assets: build_assets watch
sender: make sender
doc: cd docs && make html && cd build/html && python -m SimpleHTTPServer 16647
# doc: cd docs && make html && cd build/html && python -m SimpleHTTPServer 16647
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ pip install -e '.[prometheus]'
For list of extra features, please see `extras_require` setting in `setup.py`.


Run everything
--------------

```bash
forego start
```


Run API server
--------------

Expand All @@ -52,7 +60,6 @@ Run sender
iris-sender configs/config.dev.yaml
```


Tests
-----

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
},
entry_points={
'console_scripts': [
'iris-server = iris.bin.run_server:main',
'iris-dev = iris.bin.run_server:main',
'iris = iris.bin.run_server:main',
'iris-sender = iris.bin.sender:main',
'iris-sync-targets = iris.bin.sync_targets:main',
Expand Down
2 changes: 1 addition & 1 deletion src/iris/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3310,7 +3310,7 @@ def on_get(self, req, resp, app_name):

def update_cache_worker():
while True:
logger.info('Reinitializing cache')
logger.debug('Reinitializing cache')
cache.init()
sleep(60)

Expand Down
36 changes: 31 additions & 5 deletions src/iris/bin/run_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@
# Copyright (c) LinkedIn Corporation. All rights reserved. Licensed under the BSD-2 Clause license.
# See LICENSE in the project root for license information.

import gc
import logging
import sys
import multiprocessing
import gunicorn.app.base
from gunicorn.six import iteritems
import iris
import iris.config
import iris.api


class StandaloneApplication(gunicorn.app.base.BaseApplication):

def __init__(self, options=None):
def __init__(self, options=None, skip_build_assets=False):
self.options = options or {}
self.skip_build_assets = skip_build_assets
super(StandaloneApplication, self).__init__()

def load_config(self):
Expand All @@ -25,14 +27,38 @@ def load_config(self):
self.cfg.set(key.lower(), value)

def load(self):
import iris
reload(iris)
reload(iris.config)
reload(iris.api)
config = iris.config.load_config(sys.argv[1])
return iris.api.get_api(config)

import iris.api
app = iris.api.get_api(config)

if not self.skip_build_assets:
for r in gc.get_referrers(self):
if isinstance(r, dict) and '_num_workers' in r:
gunicorn_arbiter = r

# only build assets on one worker to avoid race conditions
if gunicorn_arbiter['worker_age'] % self.options['workers'] == 0:
import iris.ui
iris.ui.build_assets()

return app


def main():
if len(sys.argv) <= 1:
sys.exit('USAGE: %s CONFIG_FILE [--skip-build-assets]' % sys.argv[0])
elif len(sys.argv) >= 3:
skip_build_assets = (sys.argv[2] == '--skip-build-assets')
else:
skip_build_assets = False

logging.basicConfig(format='[%(asctime)s] [%(process)d] [%(levelname)s] %(name)s %(message)s',
level=logging.INFO, datefmt='%Y-%m-%d %H:%M:%S %z')

config = iris.config.load_config(sys.argv[1])
server = config['server']

Expand All @@ -45,7 +71,7 @@ def main():
'workers': (multiprocessing.cpu_count() * 2) + 1
}

gunicorn_server = StandaloneApplication(options)
gunicorn_server = StandaloneApplication(options, skip_build_assets)
gunicorn_server.run()


Expand Down
5 changes: 5 additions & 0 deletions src/iris/ui/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from jinja2.sandbox import SandboxedEnvironment
from webassets import Environment as AssetsEnvironment, Bundle
from webassets.ext.jinja2 import AssetsExtension
from webassets.script import CommandLineEnvironment
import os
import ujson
import requests
Expand Down Expand Up @@ -65,6 +66,10 @@ def hms(seconds):
jinja2_env.globals['default_route'] = default_route


def build_assets():
CommandLineEnvironment(assets_env, logger).build()


def login_url(req):
if req.path and req.path != '/login' and req.path != '/logout' and req.path != '/':
return '/login/?next=%s' % uri.encode_value(req.path)
Expand Down

0 comments on commit 2fc72a4

Please sign in to comment.