mirror of https://github.com/veops/cmdb.git
fix outside of application context
This commit is contained in:
parent
bd94bdc575
commit
eb092d4983
33
__init__.py
33
__init__.py
|
@ -1,4 +1,4 @@
|
|||
# encoding=utf-8
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import os
|
||||
import logging
|
||||
|
@ -11,18 +11,12 @@ from flask import g
|
|||
from flask.ext.babel import Babel
|
||||
from flask.ext.principal import identity_loaded
|
||||
|
||||
import core
|
||||
from extensions import db
|
||||
from extensions import mail
|
||||
from extensions import cache
|
||||
from extensions import celery
|
||||
from core import attribute
|
||||
from core import citype
|
||||
from core import cityperelation
|
||||
from core import cirelation
|
||||
from core import ci
|
||||
from core import history
|
||||
from core import account
|
||||
from core import special
|
||||
from extensions import rd
|
||||
from models.account import User
|
||||
from lib.template import filters
|
||||
|
||||
|
@ -30,19 +24,18 @@ from lib.template import filters
|
|||
APP_NAME = "CMDB-API"
|
||||
|
||||
MODULES = (
|
||||
(attribute, "/api/v0.1/attributes"),
|
||||
(citype, "/api/v0.1/citypes"),
|
||||
(cityperelation, "/api/v0.1/cityperelations"),
|
||||
(cirelation, "/api/v0.1/cirelations"),
|
||||
(ci, "/api/v0.1/ci"),
|
||||
(history, "/api/v0.1/history"),
|
||||
(account, "/api/v0.1/accounts"),
|
||||
(special, ""),
|
||||
(core.attribute, "/api/v0.1/attributes"),
|
||||
(core.citype, "/api/v0.1/citypes"),
|
||||
(core.cityperelation, "/api/v0.1/cityperelations"),
|
||||
(core.cirelation, "/api/v0.1/cirelations"),
|
||||
(core.ci, "/api/v0.1/ci"),
|
||||
(core.history, "/api/v0.1/history"),
|
||||
(core.account, "/api/v0.1/accounts"),
|
||||
# (core.special, ""),
|
||||
)
|
||||
|
||||
|
||||
def make_app(config=None, modules=None):
|
||||
modules = modules
|
||||
if not modules:
|
||||
modules = MODULES
|
||||
app = Flask(APP_NAME)
|
||||
|
@ -58,11 +51,11 @@ def make_app(config=None, modules=None):
|
|||
|
||||
def configure_extensions(app):
|
||||
db.app = app
|
||||
celery.init_app(app)
|
||||
db.init_app(app)
|
||||
mail.init_app(app)
|
||||
cache.init_app(app)
|
||||
celery.init_app(app)
|
||||
rd.init_app(app)
|
||||
|
||||
|
||||
def configure_i18n(app):
|
||||
|
@ -97,7 +90,7 @@ def configure_logging(app):
|
|||
app.config['MAIL_SERVER'],
|
||||
app.config['DEFAULT_MAIL_SENDER'],
|
||||
app.config['ADMINS'],
|
||||
'[%s] CMDB error' % hostname,
|
||||
'[%s] CMDB API error' % hostname,
|
||||
(
|
||||
app.config['MAIL_USERNAME'],
|
||||
app.config['MAIL_PASSWORD'],
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
# encoding=utf-8
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
|
||||
from flask.ext.mail import Mail
|
||||
|
@ -6,11 +6,14 @@ from flask.ext.sqlalchemy import SQLAlchemy
|
|||
from flask.ext.cache import Cache
|
||||
from flask.ext.celery import Celery
|
||||
|
||||
from lib.utils import RedisHandler
|
||||
|
||||
__all__ = ['mail', 'db', 'cache', 'celery']
|
||||
|
||||
__all__ = ['mail', 'db', 'cache', 'celery', "rd"]
|
||||
|
||||
|
||||
mail = Mail()
|
||||
db = SQLAlchemy()
|
||||
cache = Cache()
|
||||
celery = Celery()
|
||||
celery = Celery()
|
||||
rd = RedisHandler()
|
|
@ -1,4 +1,4 @@
|
|||
# encoding=utf-8
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from flask_script import Command, Option
|
||||
|
||||
|
@ -7,7 +7,7 @@ class GunicornServer(Command):
|
|||
description = 'Run the app within Gunicorn'
|
||||
|
||||
def __init__(self, host='127.0.0.1', port=5000, workers=8,
|
||||
worker_class="gevent", daemon=False):
|
||||
worker_class="sync", daemon=False):
|
||||
self.port = port
|
||||
self.host = host
|
||||
self.workers = workers
|
||||
|
|
|
@ -11,6 +11,7 @@ from flask import abort
|
|||
from sqlalchemy import or_
|
||||
|
||||
from extensions import db
|
||||
from extensions import rd
|
||||
from models.ci import CI
|
||||
from models.ci_relation import CIRelation
|
||||
from models.ci_type import CITypeAttribute
|
||||
|
@ -28,7 +29,6 @@ from lib.query_sql import QUERY_HOSTS_NUM_BY_BU
|
|||
from lib.query_sql import QUERY_HOSTS_NUM_BY_PROJECT
|
||||
from lib.query_sql import QUERY_CIS_BY_IDS
|
||||
from lib.query_sql import QUERY_CIS_BY_VALUE_TABLE
|
||||
from lib.utils import rd
|
||||
from tasks.cmdb import ci_cache
|
||||
from tasks.cmdb import ci_delete
|
||||
|
||||
|
|
21
lib/utils.py
21
lib/utils.py
|
@ -7,8 +7,12 @@ from flask import current_app
|
|||
|
||||
|
||||
class RedisHandler(object):
|
||||
def __init__(self):
|
||||
config = current_app.config
|
||||
def __init__(self, flask_app=None):
|
||||
self.flask_app = flask_app
|
||||
|
||||
def init_app(self, app):
|
||||
self.flask_app = app
|
||||
config = self.flask_app.config
|
||||
try:
|
||||
pool = redis.ConnectionPool(
|
||||
max_connections=config.get("REDIS_MAX_CONN"),
|
||||
|
@ -17,14 +21,13 @@ class RedisHandler(object):
|
|||
db=config.get("REDIS_DB"))
|
||||
self.r = redis.Redis(connection_pool=pool)
|
||||
except Exception as e:
|
||||
print e
|
||||
current_app.logger.error("init redis connection failed")
|
||||
|
||||
@classmethod
|
||||
def instance(cls):
|
||||
if not hasattr(cls, "_instance"):
|
||||
cls._instance = cls()
|
||||
return cls._instance
|
||||
# @classmethod
|
||||
# def instance(cls):
|
||||
# if not hasattr(cls, "_instance"):
|
||||
# cls._instance = cls()
|
||||
# return cls._instance
|
||||
|
||||
def get(self, ci_ids, key="CMDB_CI"):
|
||||
try:
|
||||
|
@ -51,8 +54,6 @@ class RedisHandler(object):
|
|||
except Exception as e:
|
||||
current_app.logger.error("delete redis key error, %s" % str(e))
|
||||
|
||||
rd = RedisHandler.instance()
|
||||
|
||||
|
||||
def get_page(page):
|
||||
try:
|
||||
|
|
|
@ -8,7 +8,7 @@ from flask import current_app
|
|||
|
||||
from extensions import celery
|
||||
from extensions import db
|
||||
from lib.utils import rd
|
||||
from extensions import rd
|
||||
import lib.ci
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue