Add expiration handler

This commit is contained in:
Rachel Fenichel
2020-08-18 15:22:33 -07:00
parent 34aff27f96
commit 030ad3e836
2 changed files with 52 additions and 0 deletions

49
appengine/expiration.py Normal file
View File

@@ -0,0 +1,49 @@
"""
Copyright 2020 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
"""Delete expired XML.
"""
__author__ = "fenichel@google.com (Rachel Fenichel)"
import storage
import datetime
from google.cloud import ndb
def delete_expired():
# Delete entries that have not been accessed in more than a year.
EXPIRATION_DAYS = 365
# Limit the query to avoid timeouts.
QUERY_LIMIT = 1000
bestBefore = datetime.datetime.utcnow() - datetime.timedelta(days=EXPIRATION_DAYS)
client = ndb.Client()
with client.context():
query = storage.Xml.query(storage.Xml.last_accessed < bestBefore)
results = query.fetch(limit=QUERY_LIMIT, keys_only=True)
for x in results:
x.delete()
def app(environ, start_response):
out = ""
headers = [
("Content-Type", "text/plain")
]
start_response("200 OK", headers)
delete_expired()
return [out.encode("utf-8")]

View File

@@ -15,6 +15,7 @@ limitations under the License.
"""
import storage
import expiration
# Route to requested handler.
@@ -23,6 +24,8 @@ def app(environ, start_response):
return redirect(environ, start_response)
if environ["PATH_INFO"] == "/storage":
return storage.app(environ, start_response)
if environ["PATH_INFO"] == "/expiration":
return retention_job.app(environ, start_response)
start_response("404 Not Found", [])
return [b"Page not found."]