|
| 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