Skip to content

Commit

Permalink
Delay starting indexing until actually deploying server. (#465)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewwardrop committed Oct 8, 2018
1 parent a47ada5 commit 60e46ad
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
6 changes: 3 additions & 3 deletions knowledge_repo/app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,6 @@ def ensure_excluded_tags_exist():
Tag(name=tag)
db_session.commit()

# Set up indexing timers
set_up_indexing_timers(self)

@self.before_request
def open_repository_session():
if not request.path.startswith('/static'):
Expand Down Expand Up @@ -322,6 +319,9 @@ def db_update_index(self, check_timeouts=True, force=False, reindex=False):
with self.app_context():
update_index(check_timeouts=check_timeouts, force=force, reindex=reindex)

def start_indexing(self):
set_up_indexing_timers(self)

def check_thread_support(self, check_index=True, check_repositories=True):
# If index database is an sqlite database, it will lock on any write action, and so breaks on multiple threads
# Repository uris will break as above (but less often since they are not often written too), but will also
Expand Down
18 changes: 12 additions & 6 deletions knowledge_repo/app/deploy/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@

def get_app_builder(uris, debug, db_uri, config, **kwargs):
def get_app():
return knowledge_repo.KnowledgeRepository.for_uri(uris).get_app(db_uri=db_uri, debug=debug, config=config, **kwargs)
return (
knowledge_repo.KnowledgeRepository
.for_uri(uris)
.get_app(db_uri=db_uri, debug=debug, config=config, **kwargs)
)
return get_app


Expand All @@ -24,7 +28,8 @@ def __init__(self,
port=7000,
workers=4,
timeout=60):
assert isinstance(knowledge_builder, (str, types.FunctionType)), u"Unknown builder type {}".format(type(knowledge_builder))
assert isinstance(knowledge_builder, (str, types.FunctionType)), \
u"Unknown builder type {}".format(type(knowledge_builder))
self.knowledge_builder = knowledge_builder
self.host = host
self.port = port
Expand All @@ -36,13 +41,13 @@ def using(cls, engine):
if engine == 'gunicorn':
if sys.platform == 'win32':
raise RuntimeError(
"`gunicorn` deployer is not available for Windows. Please use "
"`uwsgi` or `flask` engines instead."
"`gunicorn` deployer is not available for Windows. Please "
"use `uwsgi` or `flask` engines instead."
)
elif 'gunicorn' not in cls._registry:
raise RuntimeError(
"`gunicorn` does not appear to be installed. Please install "
"it and try again."
"`gunicorn` does not appear to be installed. Please "
"install it and try again."
)
return cls._get_subclass_for(engine)

Expand Down Expand Up @@ -87,6 +92,7 @@ def write_temp_files(self):
out.append(self.builder_str)
if not isinstance(self.knowledge_builder, str):
out.append('app = %s()' % self.knowledge_builder.__name__)
out.append('app.start_indexing()')

with open(tmp_path, 'w') as f:
f.write(u'\n'.join(out))
Expand Down
8 changes: 4 additions & 4 deletions knowledge_repo/app/deploy/flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ class FlaskDeployer(KnowledgeDeployer):
_registry_keys = ['flask']

def run(self, **kwargs):
app = self.app
return app.run(
debug=app.config['DEBUG'],
self.app.start_indexing()
return self.app.run(
debug=self.app.config['DEBUG'],
host=self.host,
port=self.port,
threaded=app.check_thread_support(),
threaded=self.app.check_thread_support(),
**kwargs
)
1 change: 1 addition & 0 deletions knowledge_repo/app/deploy/gunicorn.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,5 @@ def load(self):
def run(self):
if not self.app.check_thread_support():
raise RuntimeError("Database configuration is not suitable for deployment (not thread-safe).")
self.app.start_indexing()
return BaseApplication.run(self)

0 comments on commit 60e46ad

Please sign in to comment.