Skip to content

Commit

Permalink
Create a uWSGI postfork function registry. Also, start the tool conf
Browse files Browse the repository at this point in the history
watcher thread post-fork.
  • Loading branch information
natefoo committed Aug 11, 2016
1 parent 4b86f61 commit af533a7
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 42 deletions.
1 change: 1 addition & 0 deletions .ci/flake8_lint_include_list.txt
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ lib/galaxy/util/multi_byte.py
lib/galaxy/util/odict.py
lib/galaxy/util/pastescript/__init__.py
lib/galaxy/util/plugin_config.py
lib/galaxy/util/postfork.py
lib/galaxy/util/simplegraph.py
lib/galaxy_utils/__init__.py
lib/galaxy/util/sleeper.py
Expand Down
15 changes: 2 additions & 13 deletions lib/galaxy/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,6 @@
import time
import os

try:
from uwsgidecorators import postfork
except:
def pf_dec(func):
return func
postfork = pf_dec

from galaxy import config, jobs
import galaxy.model
import galaxy.security
Expand All @@ -31,6 +24,7 @@ def pf_dec(func):
from galaxy.web.proxy import ProxyManager
from galaxy.queue_worker import GalaxyQueueWorker
from galaxy.util import heartbeat
from galaxy.util.postfork import register_postfork_function
from tool_shed.galaxy_install import update_repository_manager


Expand Down Expand Up @@ -154,12 +148,7 @@ def __init__( self, **kwargs ):
fname=self.config.heartbeat_log
)
self.heartbeat.daemon = True

@postfork
def _start():
self.heartbeat.start()
if not config.process_is_uwsgi:
_start()
register_postfork_function(self.heartbeat.start)
if self.config.sentry_dsn:
import raven
self.sentry_client = raven.Client(self.config.sentry_dsn)
Expand Down
11 changes: 0 additions & 11 deletions lib/galaxy/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,6 @@

log = logging.getLogger( __name__ )

# The uwsgi module is automatically injected by the parent uwsgi
# process and only exists that way. If anything works, this is a
# uwsgi-managed process.
try:
import uwsgi
if uwsgi.numproc:
process_is_uwsgi = True
except ImportError:
# This is not a uwsgi process, or something went horribly wrong.
process_is_uwsgi = False


def resolve_path( path, root ):
"""If 'path' is relative make absolute by prepending 'root'"""
Expand Down
2 changes: 1 addition & 1 deletion lib/galaxy/queues.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"""

from galaxy.config import process_is_uwsgi
from galaxy.util.postfork import process_is_uwsgi

from kombu import Exchange, Queue, Connection

Expand Down
2 changes: 2 additions & 0 deletions lib/galaxy/tools/toolbox/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from galaxy.util import parse_xml
from galaxy.util import string_as_bool
from galaxy.util.bunch import Bunch
from galaxy.util.postfork import register_postfork_function

from .parser import get_toolbox_parser, ensure_tool_conf_item

Expand Down Expand Up @@ -103,6 +104,7 @@ def _init_tools_from_configs( self, config_filenames ):
self._init_tools_from_config( config_filename )
except:
log.exception( "Error loading tools defined in config %s", config_filename )
register_postfork_function(self._tool_conf_watcher.start)

def _init_tools_from_config( self, config_filename ):
"""
Expand Down
3 changes: 1 addition & 2 deletions lib/galaxy/tools/toolbox/watcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def __init__(self, reload_callback):
self.paths = {}
self._active = False
self._lock = threading.Lock()
self.thread = threading.Thread(target=self.check)
self.thread = threading.Thread(target=self.check, name="ToolConfWatcher.thread")
self.thread.daemon = True
self.event_handler = ToolConfFileEventHandler(reload_callback)

Expand Down Expand Up @@ -107,7 +107,6 @@ def monitor(self, path):
mod_time = time.ctime(os.path.getmtime(path))
with self._lock:
self.paths[path] = mod_time
self.start()

def watch_file(self, tool_conf_file):
self.monitor(tool_conf_file)
Expand Down
35 changes: 35 additions & 0 deletions lib/galaxy/util/postfork.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""
Handle postfork functions under uWSGI
"""

# The uwsgi module is automatically injected by the parent uwsgi
# process and only exists that way. If anything works, this is a
# uwsgi-managed process.
try:
import uwsgi
from uwsgidecorators import postfork
if uwsgi.numproc:
process_is_uwsgi = True
except ImportError:
# This is not a uwsgi process, or something went horribly wrong.
process_is_uwsgi = False

def pf_dec(func):
return func
postfork = pf_dec


postfork_functions = []


@postfork
def do_postfork():
for f, args, kwargs in [ t for t in postfork_functions ]:
f(*args, **kwargs)


def register_postfork_function(f, *args, **kwargs):
if process_is_uwsgi:
postfork_functions.append((f, args, kwargs))
else:
f(*args, **kwargs)
15 changes: 2 additions & 13 deletions lib/galaxy/webapps/galaxy/buildapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@


import galaxy.app
from galaxy.config import process_is_uwsgi
import galaxy.model
import galaxy.model.mapping
import galaxy.datatypes.registry
Expand All @@ -22,22 +21,14 @@
from galaxy.webapps.util import build_template_error_formatters
from galaxy import util
from galaxy.util import asbool
from galaxy.util.postfork import process_is_uwsgi, register_postfork_function
from galaxy.util.properties import load_app_properties

from paste import httpexceptions

import logging
log = logging.getLogger( __name__ )

try:
from uwsgidecorators import postfork
except:
# TODO: Make this function more like flask's @before_first_request w/
# registered methods etc.
def pf_dec(func):
return func
postfork = pf_dec


class GalaxyWebApplication( galaxy.web.framework.webapp.WebApplication ):
pass
Expand Down Expand Up @@ -135,8 +126,7 @@ def paste_app_factory( global_conf, **kwargs ):
except:
log.exception("Unable to dispose of pooled toolshed install model database connections.")

if not process_is_uwsgi:
postfork_setup()
register_postfork_function(postfork_setup)

# Return
return webapp
Expand All @@ -158,7 +148,6 @@ def uwsgi_app_factory():
return app_factory(global_conf, **kwargs)


@postfork
def postfork_setup():
from galaxy.app import app
if process_is_uwsgi:
Expand Down
2 changes: 1 addition & 1 deletion lib/galaxy/webapps/reports/buildapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

from paste import httpexceptions

from galaxy.config import process_is_uwsgi
from galaxy.util import asbool
from galaxy.util.postfork import process_is_uwsgi
from galaxy.webapps.util import build_template_error_formatters

import galaxy.model
Expand Down
2 changes: 1 addition & 1 deletion lib/galaxy/webapps/tool_shed/buildapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from galaxy.webapps.util import build_template_error_formatters
from galaxy.webapps.tool_shed.framework.middleware import hg
from galaxy import util
from galaxy.config import process_is_uwsgi
from galaxy.util.postfork import process_is_uwsgi
from galaxy.util.properties import load_app_properties

log = logging.getLogger( __name__ )
Expand Down

0 comments on commit af533a7

Please sign in to comment.