Skip to content

Commit

Permalink
Merge branch '8219-start-up-performance'
Browse files Browse the repository at this point in the history
  • Loading branch information
amercader committed May 17, 2024
2 parents 15f32e5 + 6bbbfd8 commit d77ceb7
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 20 deletions.
2 changes: 2 additions & 0 deletions changes/8219.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Improved start-up performance
- JS translations are no longer generated on each server restart. The are built when starting the development server with `ckan run` or explicitly with `ckan translations js`
6 changes: 4 additions & 2 deletions ckan/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ def format_commands(
# provided, except for `--help`. In this case it has to be done
# manually.
if not ctx.obj:
_add_ctx_object(ctx)
if '-h' not in sys.argv[1:] and '--help' not in sys.argv[1:]:
_add_ctx_object(ctx)
_add_external_commands(ctx)

commands = []
Expand Down Expand Up @@ -127,7 +128,8 @@ def parse_args(self, ctx: click.Context, args: list[str]):
def _init_ckan_config(ctx: click.Context, param: str, value: str):
if any(sys.argv[1:len(cmd) + 1] == cmd for cmd in _no_config_commands):
return
_add_ctx_object(ctx, value)
if '-h' not in sys.argv[1:] and '--help' not in sys.argv[1:]:
_add_ctx_object(ctx, value)
_add_external_commands(ctx)


Expand Down
6 changes: 5 additions & 1 deletion ckan/cli/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from ckan.common import config
from . import error_shout
from ckan.lib.i18n import build_js_translations

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -59,7 +60,7 @@ def run(ctx: click.Context, host: str, port: str, disable_reloader: bool,
passthrough_errors: bool, disable_debugger: bool, threaded: bool,
extra_files: list[str], processes: int, ssl_cert: Optional[str],
ssl_key: Optional[str], prefix: Optional[str]):
u"""Runs the Werkzeug development server"""
"""Regenerate JS translations and run the Werkzeug development server"""

if config.get("debug"):
warnings.filterwarnings("default", category=CkanDeprecationWarning)
Expand Down Expand Up @@ -115,6 +116,9 @@ def run(ctx: click.Context, host: str, port: str, disable_reloader: bool,
error_shout(u"Server port must be an integer, not {}".format(port))
raise click.Abort()

# Once automatic on startup, run only here for faster debug iterations
build_js_translations()

log.info(u"Running CKAN on {scheme}://{host}:{port}{prefix}".format(
scheme='https' if ssl_context else 'http', host=host, port=port_int,
prefix=prefix))
Expand Down
6 changes: 3 additions & 3 deletions ckan/config/declaration/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def load_from_custom(declaration, *args, **kwargs):
import pathlib
from typing import TYPE_CHECKING, Any, Callable, Dict, List
from typing_extensions import TypedDict
import yaml
import msgspec

from .key import Key
from .option import Flag, Option
Expand Down Expand Up @@ -156,6 +156,6 @@ def load_core(declaration: "Declaration"):
"""Load core declarations.
"""
source = pathlib.Path(__file__).parent / ".." / "config_declaration.yaml"
with source.open("r") as stream:
data = yaml.safe_load(stream)
with source.open("rb") as stream:
data = msgspec.yaml.decode(stream.read())
load_dict(declaration, data)
5 changes: 0 additions & 5 deletions ckan/config/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import ckan.logic as logic
import ckan.authz as authz
from ckan.lib.webassets_tools import webassets_init, register_core_assets
from ckan.lib.i18n import build_js_translations

from ckan.common import CKANConfig, config, config_declaration
from ckan.exceptions import CkanConfigurationException
Expand Down Expand Up @@ -74,10 +73,6 @@ def load_environment(conf: Union[Config, CKANConfig]):

app_globals.reset()

# Build JavaScript translations. Must be done after plugins have
# been loaded.
build_js_translations()


# A mapping of config settings that can be overridden by env vars.
# Note: Do not remove the following lines, they are used in the docs
Expand Down
11 changes: 7 additions & 4 deletions ckan/plugins/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from pkg_resources import iter_entry_points


from ckan.common import config
from ckan.common import config, aslist
from ckan.types import SignalMapping

from . import interfaces
Expand Down Expand Up @@ -143,7 +143,7 @@ def load_all() -> None:
# Clear any loaded plugins
unload_all()

plugins = config['ckan.plugins'] + find_system_plugins()
plugins = aslist(config.get('ckan.plugins')) + find_system_plugins()

load(*plugins)

Expand Down Expand Up @@ -175,7 +175,9 @@ def load(
if implemented_by(service, interfaces.ISignal):
_connect_signals(service.get_signal_subscriptions())
output.append(service)
plugins_update()

if plugins:
plugins_update()

# Return extension instance if only one was loaded. If more that one
# has been requested then a list of instances is returned in the order
Expand Down Expand Up @@ -218,7 +220,8 @@ def unload(*plugins: str) -> None:
if implemented_by(service, interfaces.ISignal):
_disconnect_signals(service.get_signal_subscriptions())

plugins_update()
if plugins:
plugins_update()


def plugin_loaded(name: str) -> bool:
Expand Down
3 changes: 1 addition & 2 deletions ckan/tests/config/test_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ def test_update_config_env_vars(ckan_config):
"""
for env_var, value in ENV_VAR_LIST:
os.environ.setdefault(env_var, value)
# plugin.load() will force the config to update
p.load()
p.plugins_update()

assert ckan_config[u"solr_url"] == u"http://mynewsolrurl/solr"
assert ckan_config[u"sqlalchemy.url"] == u"postgresql://mynewsqlurl/"
Expand Down
19 changes: 17 additions & 2 deletions doc/maintaining/installing/deployment.rst
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,23 @@ To prevent conflicts, disable your default nginx sites and restart:
sudo ln -s |nginx_config_file| /etc/nginx/sites-enabled/ckan
|restart_nginx|
-----------------------------------
7. Generate JavaScript Translations
-----------------------------------

Some front-end features require translated strings from CKAN and its
extensions. Run this command once to extract and make these
strings available after initial installation, upgrades, enabling new
plugins or enabling new languages.

.. parsed-literal::
ckan -c |ckan.ini| translation js
------------------------
7. Access your CKAN site
8. Access your CKAN site
------------------------


Expand All @@ -208,7 +223,7 @@ CKAN instance.


--------------------------------------
8. Setup a worker for background jobs
9. Setup a worker for background jobs
--------------------------------------
CKAN uses asynchronous :ref:`background jobs` for long tasks. These jobs are
executed by a separate process which is called a :ref:`worker <background jobs
Expand Down
1 change: 1 addition & 0 deletions requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ tzlocal==5.2
webassets==2.0
Werkzeug[watchdog]==3.0.3
zope.interface==6.2
msgspec==0.18.6
4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ markupsafe==2.1.5
# werkzeug
# wtforms
msgspec==0.18.6
# via flask-session
# via
# -r requirements.in
# flask-session
mypy==1.9.0
# via sqlalchemy
mypy-extensions==1.0.0
Expand Down

0 comments on commit d77ceb7

Please sign in to comment.