def __init__(self, capacity=128, ttl=0, redis_host='localhost', redis_port=6379, redis_db=0, cache_name='lru', connect=True): self.capacity = capacity # Cache is a dictionary containing keys and values self.cache = {} # Queue just contains the keys ordered according to most recently used self.queue = deque() self.ttl = ttl # Connect to the redis cache self.redis_conn = RedisCache(cache_name, redis_host, redis_port, redis_db, connect) # Get all existing items from redis cache if connect: self.getItemsFromCache()
def start_consumer(cfg, args): model = args['model'] # import wisdom 下的类名(识别方法类) mod = importlib.import_module('models.' + model + '.' + model) # 获取类名 main_class = getattr(mod, camel_string(model)) ins = main_class(model) # 设置告警图片存储介质 ins.set_saver(AliBucket(args['config'])) # 设置缓存兑现 ins.set_cacher(RedisCache(args['config'])) # 队列 if args['queue'] == 'default': args['queue'] = model if cfg.getint('app', 'debug') == 1: mq = MQNormal(cfg).start_mq_debug() else: mq = MQNormal(cfg).start_mq() chan = mq.channel() chan.basic_qos(prefetch_count=1) chan.queue_declare(queue=args['queue'], arguments={'x-message-ttl': cfg.getint('mq', 'ttl')}) chan.basic_consume(queue=args['queue'], on_message_callback=ins.callback) print("Start consuming...") try: chan.start_consuming() except KeyboardInterrupt or ConnectionResetError: chan.stop_consuming() mq.close()
import sys from .celery import app from utils import RedisCache from utils import RedisStream from datetime import timedelta from southwest.checkin import auto_checkin redis_client = RedisCache().connect() @app.task(bind=True) def autocheckin(self, confirmation, firstname, lastname, notifications): try: redis_client.hset(confirmation, "running", "True") redis_client.expire(confirmation, timedelta(days=60)) sys.stdout = RedisStream(confirmation, redis_client) auto_checkin(confirmation, firstname, lastname, notifications) except (SystemExit, Exception) as exc: print(exc) redis_client.hset(confirmation, "running", "False")
def create_app(): app = Flask(__name__) app.config["SECRET_KEY"] == os.getenv("SECRET_KEY") # Initialize flask-marshmallow extension ma.init_app(app) redis_client = RedisCache().connect() @app.route("/checkin", methods=["POST"]) def checkin(): try: CheckinSerializer = CheckinSchema(strict=True) checkin_data = CheckinSerializer.loads(request.data).data confirmation = checkin_data.get("confirmation") firstname = checkin_data.get("firstname") lastname = checkin_data.get("lastname") email = checkin_data.get("email") phone = checkin_data.get("phone") flight_confirm = redis_client.exists(confirmation) running_task = redis_client.hget(confirmation, "running") if flight_confirm and running_task == "True": return jsonify({ "error": "A task is already running for this confirmation." }), 400 # Delete any existing messages if any redis_client.hdel(confirmation, "messages") # Check if there are any optional email or phone fields # sent to get a notification back on checkin. notifications = [] if email is not None: notifications.append({ 'mediaType': 'EMAIL', 'emailAddress': email }) if phone is not None: notifications.append({ 'mediaType': 'SMS', 'phoneNumber': phone }) # Run the auto_checkin script celery task in the background autocheckin.delay(confirmation, firstname, lastname, notifications) return jsonify( {"status": "Created a new SouthwestCheckin task successfully"}), 200 except ValidationError as exc: return jsonify({"error": exc.messages}), 422 except (ConnectionError, TypeError, Exception): return jsonify( {"error": "There was an error. Contact admin at once."}), 500 @app.route("/info/<string:confirmation>", methods=["GET"]) def info(confirmation): try: checkin_info = redis_client.hgetall(confirmation) if not checkin_info: return jsonify( {"status": "No tasks running with this confirmation."}), 200 InfoSerializer = InfoSchema(strict=True) return InfoSerializer.jsonify(checkin_info), 200 except (ConnectionError, Exception): return jsonify( {"error": "There was an error. Contact admin at once."}), 500 return app
if isinstance(o, datetime): return str(o) return json.JSONEncoder.default(self, o) # pylint: enable=method-hidden # pylint: disable=invalid-name app = Eve() app.logger.setLevel(logging.INFO) app.name = 'API' app.json_encoder = MongoJSONEncoder CORS(app) BCRYPT = Bcrypt(app) app.__bcrypt__ = BCRYPT USER_HOST = environ.get("MONGO_HOST_USER", 'users') EveHealthCheck(app, '/healthcheck') RedisCache(app, host=environ.get("REDIS", 'redis')) app.gr_auth = GrAuth(app) p = argparse.ArgumentParser() p.add_argument('-b', '--basic-data', help='Apenas insere dados', action='store_true') #pylint: enable=invalid-name @requires_auth('home') def before_insert(documents): """Generate a hash for each user document on insert using bcrypt""" # argumentos = request.args.to_dict() # contests = contests.find({'platform': }) for doc in documents: if ('auth' in doc and 'password' in doc['auth']):
class LRUCache(object): def __init__(self, capacity=128, ttl=0, redis_host='localhost', redis_port=6379, redis_db=0, cache_name='lru', connect=True): self.capacity = capacity # Cache is a dictionary containing keys and values self.cache = {} # Queue just contains the keys ordered according to most recently used self.queue = deque() self.ttl = ttl # Connect to the redis cache self.redis_conn = RedisCache(cache_name, redis_host, redis_port, redis_db, connect) # Get all existing items from redis cache if connect: self.getItemsFromCache() def setRedisConn(self, redis, cache_name): self.redis_conn = RedisCache(cache_name=cache_name) self.redis_conn.setRedisConn(redis) def connectCache(self): self.redis_conn.connect() def clearCache(self): self.redis_conn.clear() self._clearNodes() def get(self, key: str): # Refresh local cache from redis self.getItemsFromCache() if key not in self.cache: return -1 else: return self._getCacheValue(key) def peek(self, key: str): if key in self.cache: return self.cache[key] else: return -1 def _getCacheValue(self, key: str): value = self.cache[key] # Remove the key from redis and queue self.queue.remove(key) self.redis_conn.remove(key) # Append the key again on redis and queue self.queue.append(key) self.redis_conn.set(key, value) # Obtain the value from the node/key return value def setCapacity(self, n: int): self.capacity = n while len(self.queue) > self.capacity: deletedKey = self.queue.popleft() del self.cache[deletedKey] self.redis_conn.remove(deletedKey) def getItemsFromCache(self): self._clearNodes() for key, value in self.redis_conn.getAllKeys().items(): self.cache[key] = value self.queue.append(key) def put(self, key: str, value: str, ttl=0): # Fail first if an invalid argument is given if not isinstance(key, str) or not isinstance(value, str): raise ValueError("A key or value was not given") # Refresh local cache from redis self.getItemsFromCache() # Checks queue capacity and removes the last one if full self._validateCapacity() # Checks if key is in queue; if it is, delete it so it can be refreshed self._validateKey(key) # Create the key again on the local instance and redis self.cache[key] = value self.queue.append(key) self.redis_conn.set(key, value) # Set an expiration time for the key self._expireCache(key, ttl) return value def _validateCapacity(self): while len(self.queue) >= self.capacity: deletedKey = self.queue.popleft() del self.cache[deletedKey] self.redis_conn.remove(deletedKey) def _validateKey(self, key): if key in self.cache: self.queue.remove(key) self.redis_conn.remove(key) def _expireCache(self, key, ttl): if ttl > 0: # Add an specific expiration time for this record Timer(ttl, self._removeCache, [key]).start() elif self.ttl > 0: # Add the default expiration time for this record if no specific time is given Timer(self.ttl, self._removeCache, [key]).start() def _clearNodes(self): self.queue.clear() self.cache.clear() def _removeCache(self, *args): if args[0] in self.cache: self.queue.remove(args[0]) del self.cache[args[0]] self.redis_conn.remove(args[0])
def setRedisConn(self, redis, cache_name): self.redis_conn = RedisCache(cache_name=cache_name) self.redis_conn.setRedisConn(redis)
def create_app(): app = Flask(__name__) app.config["SECRET_KEY"] == os.getenv("SECRET_KEY") # Initialize flask-marshmallow extension ma.init_app(app) redis_client = RedisCache().connect() @app.route("/checkin", methods=["POST"]) def checkin(): try: CheckinSerializer = CheckinSchema() checkin_data = CheckinSerializer.loads(request.data) confirmation = checkin_data.get("confirmation") firstname = checkin_data.get("firstname") lastname = checkin_data.get("lastname") email = checkin_data.get("email") phone = checkin_data.get("phone") flight_confirm = redis_client.exists(confirmation) running_task = redis_client.hget(confirmation, "running") if flight_confirm and running_task == "True": return ( jsonify({ "error": "A task is already running for this confirmation." }), 400, ) # Delete any existing messages if any redis_client.hdel(confirmation, "messages") # Check if there are any optional email or phone fields # sent to get a notification back on checkin. notifications = [] if email is not None: notifications.append({ "mediaType": "EMAIL", "emailAddress": email }) if phone is not None: notifications.append({ "mediaType": "SMS", "phoneNumber": phone }) # Run the auto_checkin script in a background task with multiprocessing. proc = Process( target=autocheckin, args=(confirmation, firstname, lastname, notifications), ) proc.start() return ( jsonify({ "status": "Created a new SouthwestCheckin task successfully" }), 200, ) except ValidationError as exc: return jsonify({"error": exc.messages}), 422 except (ConnectionError, TypeError, Exception): return jsonify( {"error": "There was an error. Contact admin at once."}), 500 @app.route("/info/<string:confirmation>", methods=["GET", "DELETE"]) def info(confirmation): try: checkin_info = redis_client.hgetall(confirmation) if not checkin_info: return ( jsonify( {"status": "No tasks running with this confirmation."}), 200, ) elif request.method == "DELETE": pid = checkin_info.get("PID") running = checkin_info.get("running") # Kill process if running if pid and running == "True": os.kill(int(pid), signal.SIGTERM) # Delete the confirmation info from redis redis_client.delete(confirmation) return jsonify({"status": "Task deleted successfully."}), 200 InfoSerializer = InfoSchema() return InfoSerializer.jsonify(checkin_info), 200 except (ConnectionError, Exception): return ( jsonify( {"status": "There was an error. Contact admin at once."}), 500, ) return app