Merge ~hloeung/content-cache-charm:master into content-cache-charm:master

Proposed by Haw Loeung
Status: Merged
Approved by: Haw Loeung
Approved revision: 8b03e579d5b473f8f068fb4a1848a474ec4a5169
Merged at revision: 0d60ed4081322396ce2101e75ebf1eaa9cf706d2
Proposed branch: ~hloeung/content-cache-charm:master
Merge into: content-cache-charm:master
Diff against target: 146 lines (+27/-8)
7 files modified
config.yaml (+8/-0)
lib/nginx.py (+8/-1)
reactive/content_cache.py (+2/-1)
tests/unit/files/nginx_config_rendered_test_output-basic_site.txt (+2/-2)
tests/unit/files/nginx_config_rendered_test_output-token_site.txt (+2/-2)
tests/unit/test_content_cache.py (+2/-0)
tests/unit/test_nginx.py (+3/-2)
Reviewer Review Type Date Requested Status
Stuart Bishop (community) Approve
Canonical IS Reviewers Pending
Review via email: mp+388277@code.launchpad.net

Commit message

Allow overriding the default proxy cache background update

To post a comment you must log in.
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

This merge proposal is being monitored by mergebot. Change the status to Approved to merge.

Revision history for this message
Stuart Bishop (stub) wrote :

Looks fine. Pedantic grammatical corrections inline.

review: Approve
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

Change successfully merged at revision 0d60ed4081322396ce2101e75ebf1eaa9cf706d2

Revision history for this message
Joel Sing (jsing) wrote :

This has already been landed, however the naming seems a bit backwards - IMO it would be better to have background_cache_updates (even if that defaults to true), rather than a disable_cache_background_update that defaults to false (even enable_background_cache_updates would at least be consistent with the following option).

Revision history for this message
Haw Loeung (hloeung) wrote :

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
diff --git a/config.yaml b/config.yaml
index 03bf2eb..0a81a0d 100644
--- a/config.yaml
+++ b/config.yaml
@@ -25,6 +25,14 @@ options:
25 default: "/var/lib/nginx/proxy"25 default: "/var/lib/nginx/proxy"
26 description: >26 description: >
27 Path or directory to store cached objects.27 Path or directory to store cached objects.
28 disable_cache_background_update:
29 default: false
30 type: boolean
31 description: >
32 Default is to enable serving contents from the cache with a
33 background subrequest to update objects in cache. This works for
34 large objects, but can cause issues with sites updated recently
35 to redirects that still have objects in the cache.
28 enable_prometheus_metrics:36 enable_prometheus_metrics:
29 default: true37 default: true
30 type: boolean38 type: boolean
diff --git a/lib/nginx.py b/lib/nginx.py
index ff58d60..74f3e58 100644
--- a/lib/nginx.py
+++ b/lib/nginx.py
@@ -24,13 +24,16 @@ PROXY_CACHE_DEFAULTS = {
2424
2525
26class NginxConf:26class NginxConf:
27 def __init__(self, conf_path=None, unit='content-cache'):27 def __init__(self, conf_path=None, unit='content-cache', disable_cache_bg_update=False):
28 if not conf_path:28 if not conf_path:
29 conf_path = NGINX_BASE_PATH29 conf_path = NGINX_BASE_PATH
30 self.unit = unit30 self.unit = unit
31
31 self._base_path = conf_path32 self._base_path = conf_path
32 self._conf_path = os.path.join(self.base_path, 'conf.d')33 self._conf_path = os.path.join(self.base_path, 'conf.d')
34 self._disable_cache_bg_update = disable_cache_bg_update
33 self._sites_path = os.path.join(self.base_path, 'sites-available')35 self._sites_path = os.path.join(self.base_path, 'sites-available')
36
34 script_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))37 script_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
35 self.jinja_env = jinja2.Environment(loader=jinja2.FileSystemLoader(script_dir))38 self.jinja_env = jinja2.Environment(loader=jinja2.FileSystemLoader(script_dir))
3639
@@ -110,6 +113,10 @@ class NginxConf:
110 continue113 continue
111 lc.setdefault(cache_key, v)114 lc.setdefault(cache_key, v)
112115
116 if self._disable_cache_bg_update:
117 lc['cache-background-update'] = 'off'
118 lc['cache-use-stale'] = lc['cache-use-stale'].replace('updating ', '')
119
113 cache_val = self.proxy_cache_configs['valid']120 cache_val = self.proxy_cache_configs['valid']
114 # Backwards compatibility121 # Backwards compatibility
115 if 'cache-validity' in lc:122 if 'cache-validity' in lc:
diff --git a/reactive/content_cache.py b/reactive/content_cache.py
index 03d012c..6c6eeef 100644
--- a/reactive/content_cache.py
+++ b/reactive/content_cache.py
@@ -140,7 +140,8 @@ def configure_nginx(conf_path=None):
140140
141 enable_prometheus_metrics = config.get('enable_prometheus_metrics')141 enable_prometheus_metrics = config.get('enable_prometheus_metrics')
142142
143 ngx_conf = nginx.NginxConf(conf_path, hookenv.local_unit())143 disable_cache_bg_update = config.get('disable_cache_background_update', False)
144 ngx_conf = nginx.NginxConf(conf_path, hookenv.local_unit(), disable_cache_bg_update=disable_cache_bg_update)
144 sites_secrets = secrets_from_config(config.get('sites_secrets'))145 sites_secrets = secrets_from_config(config.get('sites_secrets'))
145 blacklist_ports = [int(x.strip()) for x in config.get('blacklist_ports', '').split(',') if x.strip()]146 blacklist_ports = [int(x.strip()) for x in config.get('blacklist_ports', '').split(',') if x.strip()]
146 sites = sites_from_config(config.get('sites'), sites_secrets, blacklist_ports=blacklist_ports)147 sites = sites_from_config(config.get('sites'), sites_secrets, blacklist_ports=blacklist_ports)
diff --git a/tests/unit/files/nginx_config_rendered_test_output-basic_site.txt b/tests/unit/files/nginx_config_rendered_test_output-basic_site.txt
index 7add210..98a3042 100644
--- a/tests/unit/files/nginx_config_rendered_test_output-basic_site.txt
+++ b/tests/unit/files/nginx_config_rendered_test_output-basic_site.txt
@@ -20,11 +20,11 @@ server {
20 add_header X-Cache-Status "$upstream_cache_status from mock-content-cache/0";20 add_header X-Cache-Status "$upstream_cache_status from mock-content-cache/0";
21 proxy_force_ranges on;21 proxy_force_ranges on;
22 proxy_cache e176ab9d1f16-cache;22 proxy_cache e176ab9d1f16-cache;
23 proxy_cache_background_update on;23 proxy_cache_background_update off;
24 proxy_cache_lock on;24 proxy_cache_lock on;
25 proxy_cache_min_uses 1;25 proxy_cache_min_uses 1;
26 proxy_cache_revalidate on;26 proxy_cache_revalidate on;
27 proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;27 proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504;
28 proxy_cache_valid 200 1d;28 proxy_cache_valid 200 1d;
2929
30 access_by_lua_block {30 access_by_lua_block {
diff --git a/tests/unit/files/nginx_config_rendered_test_output-token_site.txt b/tests/unit/files/nginx_config_rendered_test_output-token_site.txt
index 3eed240..21638c3 100644
--- a/tests/unit/files/nginx_config_rendered_test_output-token_site.txt
+++ b/tests/unit/files/nginx_config_rendered_test_output-token_site.txt
@@ -20,11 +20,11 @@ server {
20 add_header X-Cache-Status "$upstream_cache_status from mock-content-cache/0";20 add_header X-Cache-Status "$upstream_cache_status from mock-content-cache/0";
21 proxy_force_ranges on;21 proxy_force_ranges on;
22 proxy_cache c27ba6753fee-cache;22 proxy_cache c27ba6753fee-cache;
23 proxy_cache_background_update on;23 proxy_cache_background_update off;
24 proxy_cache_lock on;24 proxy_cache_lock on;
25 proxy_cache_min_uses 1;25 proxy_cache_min_uses 1;
26 proxy_cache_revalidate on;26 proxy_cache_revalidate on;
27 proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;27 proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504;
28 proxy_cache_valid 200 1d;28 proxy_cache_valid 200 1d;
2929
30 access_by_lua_block {30 access_by_lua_block {
diff --git a/tests/unit/test_content_cache.py b/tests/unit/test_content_cache.py
index 39f92c8..ad9964f 100644
--- a/tests/unit/test_content_cache.py
+++ b/tests/unit/test_content_cache.py
@@ -192,6 +192,7 @@ class TestCharm(unittest.TestCase):
192 'cache_inactive_time': '2h',192 'cache_inactive_time': '2h',
193 'cache_max_size': '1g',193 'cache_max_size': '1g',
194 'cache_path': '/var/lib/nginx/proxy',194 'cache_path': '/var/lib/nginx/proxy',
195 'disable_cache_background_update': False,
195 'enable_prometheus_metrics': False,196 'enable_prometheus_metrics': False,
196 'sites': ngx_config,197 'sites': ngx_config,
197 'worker_connections': 768,198 'worker_connections': 768,
@@ -1154,6 +1155,7 @@ site1.local:
1154 'cache_inactive_time': '2h',1155 'cache_inactive_time': '2h',
1155 'cache_max_size': '1g',1156 'cache_max_size': '1g',
1156 'cache_path': '/var/lib/nginx/proxy',1157 'cache_path': '/var/lib/nginx/proxy',
1158 'disable_cache_background_update': True,
1157 'enable_prometheus_metrics': True,1159 'enable_prometheus_metrics': True,
1158 'sites': ngx_config,1160 'sites': ngx_config,
1159 'worker_connections': 768,1161 'worker_connections': 768,
diff --git a/tests/unit/test_nginx.py b/tests/unit/test_nginx.py
index 5123f6e..7f95ba8 100644
--- a/tests/unit/test_nginx.py
+++ b/tests/unit/test_nginx.py
@@ -124,7 +124,7 @@ class TestLibNginx(unittest.TestCase):
124124
125 def test_nginx_config_render_with_metrics(self):125 def test_nginx_config_render_with_metrics(self):
126 """Test rendering with metrics exposed."""126 """Test rendering with metrics exposed."""
127 ngx_conf = nginx.NginxConf(unit='mock-content-cache/0')127 ngx_conf = nginx.NginxConf(unit='mock-content-cache/0', disable_cache_bg_update=True)
128128
129 with open('tests/unit/files/config_test_basic_config.txt', 'r', encoding='utf-8') as f:129 with open('tests/unit/files/config_test_basic_config.txt', 'r', encoding='utf-8') as f:
130 sites = yaml.safe_load(f.read())130 sites = yaml.safe_load(f.read())
@@ -133,8 +133,9 @@ class TestLibNginx(unittest.TestCase):
133 'cache_inactive_time': '2h',133 'cache_inactive_time': '2h',
134 'cache_max_size': '1g',134 'cache_max_size': '1g',
135 'cache_path': '/var/lib/nginx/proxy',135 'cache_path': '/var/lib/nginx/proxy',
136 'listen_address': '127.0.0.1',136 'disable_cache_background_update': True,
137 'enable_prometheus_metrics': True,137 'enable_prometheus_metrics': True,
138 'listen_address': '127.0.0.1',
138 }139 }
139 for site, site_conf in sites.items():140 for site, site_conf in sites.items():
140 conf['site'] = site141 conf['site'] = site

Subscribers

People subscribed via source and target branches