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