def unlock_task(name): """Unlock a locked task. :return: ``True`` if the task has been unlocked; ``False`` if it was not locked. """ cache = GenericCache('task-locks') if not cache.get(name): return False cache.delete(name) return True
def wrapper(*args, **kwargs): cache = GenericCache('task-locks') name = current_task.name if cache.get(name): Logger.get('celery').warning('Task {} is locked; not executing it'.format(name)) return cache.set(name, True) try: return f(*args, **kwargs) finally: cache.delete(name)
def wrapper(*args, **kwargs): cache = GenericCache('task-locks') name = current_task.name if cache.get(name): Logger.get('celery').warning('Task %s is locked; not executing it. ' 'To manually unlock it, run `indico celery unlock %s`', name, name) return cache.set(name, True) try: return f(*args, **kwargs) finally: cache.delete(name)
def wrapper(*args, **kwargs): cache = GenericCache('task-locks') name = current_task.name if cache.get(name): Logger.get('celery').warning('Task %s is locked; not executing it. ' 'To manually unlock it, run `indico celery unlock %s`', name, name) return cache.set(name, True, 86400) try: return f(*args, **kwargs) finally: cache.delete(name)
class IndicoSessionInterface(SessionInterface): pickle_based = True serializer = pickle session_class = IndicoSession temporary_session_lifetime = timedelta(days=7) def __init__(self): self.storage = GenericCache('flask-session') def generate_sid(self): return str(uuid.uuid4()) def get_cookie_secure(self, app): return request.is_secure def get_storage_lifetime(self, app, session): # Permanent sessions are stored for exactly the same duration as the session id cookie. # "Temporary" session are stored for a period that is not too short/long as some people # close their browser very rarely and thus shouldn't be logged out that often. if session.permanent: return app.permanent_session_lifetime else: return self.temporary_session_lifetime def should_refresh_session(self, app, session): if session.new or '_expires' not in session: return False threshold = self.get_storage_lifetime(app, session) / 2 return session['_expires'] - datetime.now() < threshold def should_refresh_sid(self, app, session): if not session.new and self.get_cookie_secure( app) and not session.get('_secure'): return True if getattr(session, '_refresh_sid', False): return True return False def open_session(self, app, request): sid = request.cookies.get(app.session_cookie_name) if not sid: return self.session_class(sid=self.generate_sid(), new=True) data = self.storage.get(sid) if data is not None: try: return self.session_class(self.serializer.loads(data), sid=sid) except TypeError: # fall through to generating a new session; this likely happens when # you have a session saved on Python 2 pass return self.session_class(sid=self.generate_sid(), new=True) def save_session(self, app, session, response): domain = self.get_cookie_domain(app) secure = self.get_cookie_secure(app) refresh_sid = self.should_refresh_sid(app, session) if not session and not session.new: # empty session, delete it from storage and cookie self.storage.delete(session.sid) response.delete_cookie(app.session_cookie_name, domain=domain) return if not refresh_sid and not session.modified and not self.should_refresh_session( app, session): # If the session has not been modified we only store if it needs to be refreshed return if config.SESSION_LIFETIME > 0: # Setting session.permanent marks the session as modified so we only set it when we # are saving the session anyway! session.permanent = True storage_ttl = self.get_storage_lifetime(app, session) cookie_lifetime = self.get_expiration_time(app, session) session['_expires'] = datetime.now() + storage_ttl if refresh_sid: self.storage.delete(session.sid) session.sid = self.generate_sid() session['_secure'] = request.is_secure self.storage.set(session.sid, self.serializer.dumps(dict(session)), storage_ttl) response.set_cookie(app.session_cookie_name, session.sid, expires=cookie_lifetime, httponly=True, secure=secure)
class IndicoSessionInterface(SessionInterface): pickle_based = True serializer = cPickle session_class = IndicoSession temporary_session_lifetime = timedelta(days=7) def __init__(self): self.storage = GenericCache('flask-session') def generate_sid(self): return str(uuid.uuid4()) def get_cookie_secure(self, app): return request.is_secure def get_storage_lifetime(self, app, session): # Permanent sessions are stored for exactly the same duration as the session id cookie. # "Temporary" session are stored for a period that is not too short/long as some people # close their browser very rarely and thus shouldn't be logged out that often. if session.permanent: return app.permanent_session_lifetime else: return self.temporary_session_lifetime def should_refresh_session(self, app, session): if session.new or '_expires' not in session: return False threshold = self.get_storage_lifetime(app, session) / 2 return session['_expires'] - datetime.now() < threshold def should_refresh_sid(self, app, session): if not session.new and self.get_cookie_secure(app) and not session.get('_secure'): return True if getattr(session, '_refresh_sid', False): return True return False def open_session(self, app, request): sid = request.cookies.get(app.session_cookie_name) if not sid: return self.session_class(sid=self.generate_sid(), new=True) data = self.storage.get(sid) if data is not None: return self.session_class(self.serializer.loads(data), sid=sid) return self.session_class(sid=self.generate_sid(), new=True) def save_session(self, app, session, response): domain = self.get_cookie_domain(app) secure = self.get_cookie_secure(app) refresh_sid = self.should_refresh_sid(app, session) if not session and not session.new: # empty session, delete it from storage and cookie self.storage.delete(session.sid) response.delete_cookie(app.session_cookie_name, domain=domain) return if not refresh_sid and not session.modified and not self.should_refresh_session(app, session): # If the session has not been modified we only store if it needs to be refreshed return if config.SESSION_LIFETIME > 0: # Setting session.permanent marks the session as modified so we only set it when we # are saving the session anyway! session.permanent = True storage_ttl = self.get_storage_lifetime(app, session) cookie_lifetime = self.get_expiration_time(app, session) session['_expires'] = datetime.now() + storage_ttl if refresh_sid: self.storage.delete(session.sid) session.sid = self.generate_sid() session['_secure'] = request.is_secure self.storage.set(session.sid, self.serializer.dumps(dict(session)), storage_ttl) response.set_cookie(app.session_cookie_name, session.sid, expires=cookie_lifetime, httponly=True, secure=secure)