-
Notifications
You must be signed in to change notification settings - Fork 0
/
garbage_collector.py
71 lines (58 loc) · 2.09 KB
/
garbage_collector.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env python
import logging
import os, sys
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template, util
from google.appengine.ext import db
ROOT_PATH = os.path.dirname(__file__)
sys.path.append(ROOT_PATH)
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'gaenv'))
from sources.gsql_datastore import *
GQL_MODELS = [GsqlGroupLesson,
GsqlClassroomData,
GsqlClassroomDescriptor,
GsqlFacultyDescriptor,
GsqlFreeClassroomsView,
GsqlGroupData,
GsqlGroupDescriptor,
GsqlYearDescriptor,
GsqlVersion]
def dropVersion(versionNum):
for model in GQL_MODELS:
while True:
l = model.all().filter("version =", versionNum).fetch(100)
if len(l):
db.delete(l)
else:
break
def cleanupDatastore(logger):
latest_versions = \
GsqlVersion.all().filter('valid =', True).order('-version').fetch(5)
latest_preserved = latest_versions[-1]
logger("Five last versions are: %s" %
map(lambda x: x.version, latest_versions))
oldest = GsqlVersion.all().filter('valid =', True).order('version').get()
logger("Oldest version is %s created at %s" %
(oldest.version, oldest.create_time))
if oldest.version < latest_preserved.version:
logger("No need to store version %s" % oldest.version)
logger("Removing old records...")
dropVersion(oldest.version)
else:
logger("Have only five valid versions, no need to do cleanup.")
class MainHandler(webapp.RequestHandler):
def get(self):
self.post()
def post(self):
self.log("Starting cleanup...")
cleanupDatastore(logger = self.log)
self.log("Cleanup finished")
def log(self, msg):
logging.info(msg)
self.response.out.write(msg + "<br/>")
def main():
application = webapp.WSGIApplication([('/tasks/clean_oldest', MainHandler)],
debug=True)
util.run_wsgi_app(application)
if __name__ == '__main__':
main()