Merge pull request #224 from niphlod/enhancement/sessions2trash.py
session2trash.py: easier to use within a module, e.g. in the scheduler
This commit is contained in:
+25
-13
@@ -20,6 +20,12 @@ Typical usage:
|
|||||||
|
|
||||||
# Delete all sessions regardless of expiry and exit.
|
# Delete all sessions regardless of expiry and exit.
|
||||||
python web2py.py -S app -M -R scripts/sessions2trash.py -A -o -x 0
|
python web2py.py -S app -M -R scripts/sessions2trash.py -A -o -x 0
|
||||||
|
|
||||||
|
# Delete session in a module (move to the modules folder)
|
||||||
|
from sessions2trash import single_loop
|
||||||
|
def delete_sessions():
|
||||||
|
single_loop()
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import with_statement
|
from __future__ import with_statement
|
||||||
@@ -109,7 +115,7 @@ class SessionSetFiles(SessionSet):
|
|||||||
|
|
||||||
def get(self):
|
def get(self):
|
||||||
"""Return list of SessionFile instances for existing sessions."""
|
"""Return list of SessionFile instances for existing sessions."""
|
||||||
path = os.path.join(request.folder, 'sessions')
|
path = os.path.join(current.request.folder, 'sessions')
|
||||||
return [SessionFile(os.path.join(path, x)) for x in os.listdir(path)]
|
return [SessionFile(os.path.join(path, x)) for x in os.listdir(path)]
|
||||||
|
|
||||||
|
|
||||||
@@ -149,7 +155,10 @@ class SessionFile(object):
|
|||||||
self.filename = filename
|
self.filename = filename
|
||||||
|
|
||||||
def delete(self):
|
def delete(self):
|
||||||
os.unlink(self.filename)
|
try:
|
||||||
|
os.unlink(self.filename)
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
def get(self):
|
def get(self):
|
||||||
session = Storage()
|
session = Storage()
|
||||||
@@ -175,6 +184,17 @@ def total_seconds(delta):
|
|||||||
return (delta.microseconds + (delta.seconds + (delta.days * 24 * 3600)) *
|
return (delta.microseconds + (delta.seconds + (delta.days * 24 * 3600)) *
|
||||||
10 ** 6) / 10 ** 6
|
10 ** 6) / 10 ** 6
|
||||||
|
|
||||||
|
def single_loop(expiration=None, force=False, verbose=False):
|
||||||
|
if expiration is None:
|
||||||
|
try:
|
||||||
|
expiration = auth.settings.expiration
|
||||||
|
except:
|
||||||
|
expiration = EXPIRATION_MINUTES * 60
|
||||||
|
|
||||||
|
set_db = SessionSetDb(expiration, force, verbose)
|
||||||
|
set_files = SessionSetFiles(expiration, force, verbose)
|
||||||
|
set_db.trash()
|
||||||
|
set_files.trash()
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
"""Main processing."""
|
"""Main processing."""
|
||||||
@@ -206,17 +226,9 @@ def main():
|
|||||||
(options, unused_args) = parser.parse_args()
|
(options, unused_args) = parser.parse_args()
|
||||||
|
|
||||||
expiration = options.expiration
|
expiration = options.expiration
|
||||||
if expiration is None:
|
|
||||||
try:
|
|
||||||
expiration = auth.settings.expiration
|
|
||||||
except:
|
|
||||||
expiration = EXPIRATION_MINUTES * 60
|
|
||||||
|
|
||||||
set_db = SessionSetDb(expiration, options.force, options.verbose)
|
|
||||||
set_files = SessionSetFiles(expiration, options.force, options.verbose)
|
|
||||||
while True:
|
while True:
|
||||||
set_db.trash()
|
single_loop(expiration, options.force, options.verbose)
|
||||||
set_files.trash()
|
|
||||||
|
|
||||||
if options.once:
|
if options.once:
|
||||||
break
|
break
|
||||||
@@ -225,5 +237,5 @@ def main():
|
|||||||
print 'Sleeping %s seconds' % (options.sleep)
|
print 'Sleeping %s seconds' % (options.sleep)
|
||||||
time.sleep(options.sleep)
|
time.sleep(options.sleep)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|||||||
Reference in New Issue
Block a user