Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit18e1d74

Browse files
authored
Improve static deploy (python#2609)
* Add a utility for purging Fastly by Surrogate-Key* add a postdeploy step to purge surroage-keys for static files
1 parenta9fab67 commit18e1d74

File tree

7 files changed

+50
-3
lines changed

7 files changed

+50
-3
lines changed

‎Procfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ release: python manage.py migrate --noinput
22
web: bin/start-nginx gunicorn -c gunicorn.conf pydotorg.wsgi
33
worker: celery -A pydotorg worker -l INFO
44
worker-beat: celery -A pydotorg beat -l INFO --scheduler django_celery_beat.schedulers:DatabaseScheduler
5+
postdeploy: python manage.py postdeploy

‎fastly/utils.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,23 @@ def purge_url(path):
2020
returnresponse
2121

2222
returnNone
23+
24+
25+
defpurge_surrogate_key(key):
26+
"""
27+
Purge a Fastly.com Surrogate-Key given a key.
28+
"""
29+
ifsettings.DEBUG:
30+
return
31+
32+
api_key=getattr(settings,'FASTLY_API_KEY',None)
33+
service_id=getattr(settings,'FASTLY_SERVICE_ID',None)
34+
ifapi_keyandservice_id:
35+
response=requests.request(
36+
"POST",
37+
f'https://api.fastly.com/service/{service_id}/purge/{key}',
38+
headers={'Fastly-Key':api_key},
39+
)
40+
returnresponse
41+
42+
returnNone

‎pydotorg/management/__init__.py

Whitespace-only changes.

‎pydotorg/management/commands/__init__.py

Whitespace-only changes.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
fromdjango.core.management.baseimportBaseCommand
2+
fromdjango.confimportsettings
3+
4+
fromfastly.utilsimportpurge_surrogate_key
5+
6+
7+
classCommand(BaseCommand):
8+
""" Do things after deployment is complete """
9+
10+
defhandle(self,*args,**kwargs):
11+
# If we have a STATIC_SURROGATE_KEY set, purge static files to ensure
12+
# that anything cached mid-deploy is ignored (like 404s).
13+
ifsettings.STATIC_SURROGATE_KEY:
14+
purge_surrogate_key(settings.STATIC_SURROGATE_KEY)

‎pydotorg/settings/base.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,10 @@
285285
MAILING_LIST_PSF_MEMBERS="psf-members-announce-request@python.org"
286286

287287
### Fastly ###
288-
FASTLY_API_KEY=False# Set to Fastly API key in production to allow pages to
289-
# be purged on save
288+
FASTLY_SERVICE_ID=False# Set to a Fastly Service ID in production to allow
289+
# purges by Surrogate-Key
290+
FASTLY_API_KEY=False# Set to Fastly API key in production to allow
291+
# pages to be purged on save
290292

291293
# Jobs
292294
JOB_THRESHOLD_DAYS=90
@@ -349,6 +351,10 @@
349351

350352
GLOBAL_SURROGATE_KEY='pydotorg-app'
351353

354+
### pydotorg.settings.cabotage.add_surrogate_keys_to_static
355+
356+
STATIC_SURROGATE_KEY='pydotorg-static'
357+
352358
### PyCon Integration for Sponsor Voucher Codes
353359
PYCON_API_KEY=config("PYCON_API_KEY",default="deadbeef-dead-beef-dead-beefdeadbeef")
354360
PYCON_API_SECRET=config("PYCON_API_SECRET",default="deadbeef-dead-beef-dead-beefdeadbeef")

‎pydotorg/settings/cabotage.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@
5353
},
5454
}
5555

56+
defadd_surrogate_keys_to_static(headers,path,url):
57+
headers['Surrogate-Key']=STATIC_SURROGATE_KEY
58+
59+
WHITENOISE_ADD_HEADERS_FUNCTION=add_surrogate_keys_to_static
60+
5661
EMAIL_HOST=config('EMAIL_HOST')
5762
EMAIL_HOST_USER=config('EMAIL_HOST_USER')
5863
EMAIL_HOST_PASSWORD=config('EMAIL_HOST_PASSWORD')
@@ -63,7 +68,8 @@
6368
PEP_REPO_PATH=None
6469
PEP_ARTIFACT_URL=config('PEP_ARTIFACT_URL')
6570

66-
# Fastly API Key
71+
# Fastly
72+
FASTLY_SERVICE_ID=config('FASTLY_SERVICE_ID')
6773
FASTLY_API_KEY=config('FASTLY_API_KEY')
6874

6975
SECURE_SSL_REDIRECT=True

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp