-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #114 from magfest/celery_redis
Drop usage of cherrys library
- Loading branch information
Showing
3 changed files
with
100 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,16 @@ | ||
configobj>=5.0.5 | ||
cherrypy==17.3.0 | ||
ws4py>=0.3.2 | ||
SQLAlchemy>=1.1.0 | ||
six>=1.5.2 | ||
configobj>=5.0.5 | ||
Jinja2>=2.7 | ||
rpctools>=0.3.1 | ||
logging_unterpolation>=0.2.0 | ||
requests>=2.2.1 | ||
paver>=1.2.2 | ||
wheel>=0.24.0 | ||
pip>=1.5.6 | ||
sh>=1.09 | ||
python-prctl>=1.6.1; 'linux' in sys_platform | ||
psutil>=5.4.1 | ||
python-prctl>=1.6.1; 'linux' in sys_platform | ||
redis==4.3.6 | ||
requests>=2.2.1 | ||
rpctools>=0.3.1 | ||
sh>=1.09 | ||
six>=1.5.2 | ||
SQLAlchemy>=1.1.0 | ||
wheel>=0.24.0 | ||
ws4py>=0.3.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import threading | ||
|
||
try: | ||
import cPickle as pickle | ||
except ImportError: | ||
import pickle | ||
|
||
from cherrypy.lib.sessions import Session | ||
import redis | ||
from redis import Sentinel | ||
|
||
class RedisSession(Session): | ||
|
||
# the default settings | ||
host = '127.0.0.1' | ||
port = 6379 | ||
db = 0 | ||
password = None | ||
tls_skip_verify = False | ||
is_sentinel = False | ||
ssl = False | ||
user = "" | ||
|
||
|
||
@classmethod | ||
def setup(cls, **kwargs): | ||
"""Set up the storage system for redis-based sessions. | ||
Called once when the built-in tool calls sessions.init. | ||
""" | ||
# overwritting default settings with the config dictionary values | ||
for k, v in kwargs.items(): | ||
setattr(cls, k, v) | ||
|
||
if cls.tls_skip_verify: | ||
cls.ssl_cert_req=None | ||
else: | ||
cls.ssl_cert_req="required" | ||
|
||
if cls.is_sentinel: | ||
sentinel = Sentinel([(cls.host, cls.port)], ssl=cls.ssl, ssl_cert_reqs=cls.ssl_cert_req, sentinel_kwargs={"password":cls.sentinel_pass, "ssl": cls.ssl, "ssl_cert_reqs": cls.ssl_cert_req}, username=cls.user, password=cls.password) | ||
cls.cache = sentinel.master_for(cls.sentinel_service) | ||
|
||
else: | ||
cls.cache = redis.Redis( | ||
host=cls.host, | ||
port=cls.port, | ||
db=cls.db, | ||
ssl=cls.ssl, | ||
username=cls.user, | ||
password=cls.password) | ||
|
||
def _exists(self): | ||
return bool(self.cache.exists(self.prefix+self.id)) | ||
|
||
def _load(self): | ||
try: | ||
return pickle.loads(self.cache.get(self.prefix+self.id)) | ||
except TypeError: | ||
# if id not defined pickle can't load None and raise TypeError | ||
return None | ||
|
||
def _save(self, expiration_time): | ||
pickled_data = pickle.dumps( | ||
(self._data, expiration_time), | ||
pickle.HIGHEST_PROTOCOL) | ||
|
||
result = self.cache.setex(self.prefix+self.id, self.timeout * 60, pickled_data) | ||
|
||
if not result: | ||
raise AssertionError("Session data for id %r not set." % self.prefix+self.id) | ||
|
||
def _delete(self): | ||
self.cache.delete(self.prefix+self.id) | ||
|
||
# http://docs.cherrypy.org/dev/refman/lib/sessions.html?highlight=session#locking-sessions | ||
# session id locks as done in RamSession | ||
|
||
locks = {} | ||
|
||
def acquire_lock(self): | ||
"""Acquire an exclusive lock on the currently-loaded session data.""" | ||
self.locked = True | ||
self.locks.setdefault(self.prefix+self.id, threading.RLock()).acquire() | ||
|
||
def release_lock(self): | ||
"""Release the lock on the currently-loaded session data.""" | ||
self.locks[self.prefix+self.id].release() | ||
self.locked = False |