Merge pull request #4191 from rachel-fenichel/data_expiration

Add expiration handler
This commit is contained in:
Rachel Fenichel
2020-08-26 10:05:26 -07:00
committed by GitHub
2 changed files with 54 additions and 0 deletions

51
appengine/expiration.py Normal file
View File

@@ -0,0 +1,51 @@
"""
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
EXPIRATION_DAYS = 365
# Limit the query to avoid timeouts.
QUERY_LIMIT = 1000
def delete_expired():
"""Deletes entries that have not been accessed in more than a year."""
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 expiration.app(environ, start_response)
start_response("404 Not Found", [])
return [b"Page not found."]