Skip to content

Commit 89b9283

Browse files
committed
scheduled session cleanup
1 parent 5fa2d71 commit 89b9283

File tree

4 files changed

+86
-0
lines changed

4 files changed

+86
-0
lines changed

www/WEB-INF/cron.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<cronentries>
3+
<cron>
4+
<url>/session-cleanup.jsp?num=5000</url>
5+
<description>Daily session purge job</description>
6+
<schedule>every day 02:30</schedule>
7+
<timezone>America/New_York</timezone>
8+
</cron>
9+
</cronentries>
21.5 MB
Binary file not shown.

www/WEB-INF/logging.properties

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.level = INFO
2+
3+

www/session-cleanup.jsp

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<%@ page buffer="16kb"
2+
contentType="text/plain;charset=utf-8"
3+
import="java.io.*,
4+
java.text.*,
5+
java.util.*,
6+
com.google.appengine.api.datastore.*,
7+
com.google.appengine.api.datastore.Query.FilterOperator"
8+
session="false"
9+
%><%
10+
11+
TimedOutput tout = new TimedOutput(out);
12+
13+
String numString = request.getParameter("num");
14+
int limit = numString == null ? 375 : Integer.parseInt(numString);
15+
tout.println(MessageFormat.format("Will try to purge {0} expired sessions", limit));
16+
17+
Query query = new Query("_ah_SESSION");
18+
query.addFilter("_expires", FilterOperator.LESS_THAN, System.currentTimeMillis() - (7*24*60*60*1000));
19+
query.setKeysOnly();
20+
List<Key> killList = new ArrayList<Key>(limit);
21+
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
22+
tout.println("got datastore");
23+
PreparedQuery pq = datastore.prepare(query);
24+
tout.println("Prepared query");
25+
Iterable<Entity> entities = pq.asIterable(FetchOptions.Builder.withLimit(limit));
26+
tout.println("asIterable ready");
27+
for(Entity expiredSession : entities)
28+
{
29+
Key key = expiredSession.getKey();
30+
killList.add(key);
31+
}
32+
tout.println("KillList built");
33+
34+
try
35+
{
36+
datastore.delete(killList);
37+
}
38+
catch (Exception e)
39+
{
40+
tout.println(MessageFormat.format("DatastoreTimeoutException after {0}", killList.size()));
41+
}
42+
43+
tout.println(MessageFormat.format("Cleared {0} expired sessions", killList.size()));
44+
45+
%><%!
46+
47+
class TimedOutput
48+
{
49+
JspWriter out;
50+
long start;
51+
long lap;
52+
53+
public TimedOutput(JspWriter out)
54+
{
55+
this.out = out;
56+
this.start = System.currentTimeMillis();
57+
}
58+
59+
public void println(String msg)
60+
throws IOException
61+
{
62+
long now = System.currentTimeMillis();
63+
out.print(msg);
64+
if (lap != 0)
65+
{
66+
out.print(MessageFormat.format(" ({0} ms)", (now - lap)));
67+
}
68+
out.println();
69+
out.flush();
70+
lap = System.currentTimeMillis();
71+
}
72+
}
73+
74+
%>

0 commit comments

Comments
 (0)