##// END OF EJS Templates
user-sessions: add count and cleanup of file-based sessions.
marcink -
r1364:bf929781 default
parent child Browse files
Show More
@@ -18,7 +18,6 b''
18 # RhodeCode Enterprise Edition, including its added features, Support services,
18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20
20
21 import collections
22 import logging
21 import logging
23
22
24 from pylons import tmpl_context as c
23 from pylons import tmpl_context as c
@@ -18,6 +18,8 b''
18 # RhodeCode Enterprise Edition, including its added features, Support services,
18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20
20
21 import os
22 import time
21 import datetime
23 import datetime
22 import dateutil
24 import dateutil
23 from rhodecode.model.db import DbSession, Session
25 from rhodecode.model.db import DbSession, Session
@@ -29,6 +31,7 b' class CleanupCommand(Exception):'
29
31
30 class BaseAuthSessions(object):
32 class BaseAuthSessions(object):
31 SESSION_TYPE = None
33 SESSION_TYPE = None
34 NOT_AVAILABLE = 'NOT AVAILABLE'
32
35
33 def __init__(self, config):
36 def __init__(self, config):
34 session_conf = {}
37 session_conf = {}
@@ -70,28 +73,74 b' class DbAuthSessions(BaseAuthSessions):'
70 class FileAuthSessions(BaseAuthSessions):
73 class FileAuthSessions(BaseAuthSessions):
71 SESSION_TYPE = 'file sessions'
74 SESSION_TYPE = 'file sessions'
72
75
73 def get_count(self):
76 def _get_sessions_dir(self):
74 return 'NOT AVAILABLE'
77 data_dir = self.config.get('beaker.session.data_dir')
75
78 return data_dir
76 def get_expired_count(self, older_than_seconds=None):
77 return self.get_count()
78
79
79 def clean_sessions(self, older_than_seconds=None):
80 def _count_on_filesystem(self, path, older_than=0, callback=None):
80 data_dir = self.config.get('beaker.session.data_dir')
81 value = dict(percent=0, used=0, total=0, items=0, path=path, text='')
81 raise CleanupCommand(
82 items_count = 0
82 'Please execute this command: '
83 used = 0
83 '`find . -mtime +60 -exec rm {{}} \;` inside {} directory'.format(
84 cur_time = time.time()
84 data_dir))
85 for root, dirs, files in os.walk(path):
86 for f in files:
87 final_path = os.path.join(root, f)
88 try:
89 mtime = os.stat(final_path).st_mtime
90 if (cur_time - mtime) > older_than:
91 items_count += 1
92 if callback:
93 callback_res = callback(final_path)
94 else:
95 used += os.path.getsize(final_path)
96 except OSError:
97 pass
98 value.update({
99 'percent': 100,
100 'used': used,
101 'total': used,
102 'items': items_count
103 })
104 return value
105
106 def get_count(self):
107 try:
108 sessions_dir = self._get_sessions_dir()
109 items_count = self._count_on_filesystem(sessions_dir)['items']
110 except Exception:
111 items_count = self.NOT_AVAILABLE
112 return items_count
113
114 def get_expired_count(self, older_than_seconds=0):
115 try:
116 sessions_dir = self._get_sessions_dir()
117 items_count = self._count_on_filesystem(
118 sessions_dir, older_than=older_than_seconds)['items']
119 except Exception:
120 items_count = self.NOT_AVAILABLE
121 return items_count
122
123 def clean_sessions(self, older_than_seconds=0):
124 # find . -mtime +60 -exec rm {} \;
125
126 sessions_dir = self._get_sessions_dir()
127
128 def remove_item(path):
129 os.remove(path)
130
131 return self._count_on_filesystem(
132 sessions_dir, older_than=older_than_seconds,
133 callback=remove_item)['items']
85
134
86
135
87 class MemcachedAuthSessions(BaseAuthSessions):
136 class MemcachedAuthSessions(BaseAuthSessions):
88 SESSION_TYPE = 'ext:memcached'
137 SESSION_TYPE = 'ext:memcached'
89
138
90 def get_count(self):
139 def get_count(self):
91 return 'NOT AVAILABLE'
140 return self.NOT_AVAILABLE
92
141
93 def get_expired_count(self, older_than_seconds=None):
142 def get_expired_count(self, older_than_seconds=None):
94 return self.get_count()
143 return self.NOT_AVAILABLE
95
144
96 def clean_sessions(self, older_than_seconds=None):
145 def clean_sessions(self, older_than_seconds=None):
97 raise CleanupCommand('Cleanup for this session type not yet available')
146 raise CleanupCommand('Cleanup for this session type not yet available')
@@ -101,10 +150,10 b' class MemoryAuthSessions(BaseAuthSession'
101 SESSION_TYPE = 'memory'
150 SESSION_TYPE = 'memory'
102
151
103 def get_count(self):
152 def get_count(self):
104 return 'NOT AVAILABLE'
153 return self.NOT_AVAILABLE
105
154
106 def get_expired_count(self, older_than_seconds=None):
155 def get_expired_count(self, older_than_seconds=None):
107 return self.get_count()
156 return self.NOT_AVAILABLE
108
157
109 def clean_sessions(self, older_than_seconds=None):
158 def clean_sessions(self, older_than_seconds=None):
110 raise CleanupCommand('Cleanup for this session type not yet available')
159 raise CleanupCommand('Cleanup for this session type not yet available')
General Comments 0
You need to be logged in to leave comments. Login now