This is a snapshot of Indico's old Trac site. Any information contained herein is most probably outdated. Access our new GitHub site here.

Ticket #983: cleanupWebSessions.py

File cleanupWebSessions.py, 1.1 KB (added by makub, 4 years ago)

a script that deletes in batches

Line 
1from MaKaC.common import DBMgr
2from MaKaC.webinterface.session.sessionManagement import getSessionManager
3
4def deleteSessions(sm,todelete):
5    done = 0
6    for key in todelete :
7      sm.delete_session(key)
8      done+=1
9    try:
10      DBMgr.getInstance().commit()
11    except:
12      DBMgr.getInstance().sync()
13    print "deleted %s sessions" % (len(todelete))
14
15DBMgr.getInstance().startRequest()
16websessionDelay = float(24 * 3600)
17sm = getSessionManager()
18print "ok got session manager"
19keys = sm.keys()
20print "ok got keys"
21done = 0
22fresh = 0
23todelete = []
24batchsize = 1000
25
26print "start deleting"
27for key in keys:
28  value = sm[key]
29  try:
30    if value.get_access_age() > websessionDelay:
31      todelete.append(key)
32    else:
33      fresh+=1
34      if (fresh%batchsize)==0:
35        print "too fresh %s sessions" % (fresh)
36  except:
37    print "cannot delete %s" % key
38  if len(todelete) >= batchsize :
39    deleteSessions(sm,todelete)
40    todelete = []
41
42if len(todelete) > 0 :
43    deleteSessions(sm,todelete)
44
45print "too fresh %s sessions" % (fresh)
46
47DBMgr.getInstance().endRequest()