def __init__(self, env, queue_name=None, report_id=None, **kwargs): _config = load_config(env.upper()) self.user_pwd = pika.PlainCredentials(_config.RABBIT_USERNAME, _config.RABBIT_PWD) self.connection = pika.BlockingConnection( pika.ConnectionParameters( host=_config.RABBIT_HOST, port=_config.RABBIT_PORT, credentials=self.user_pwd, virtual_host=_config.RABBIT_V_HOST)) # 创建连接 self.channel = self.connection.channel() # 连接上创建一个频道 if report_id: # 调试用例日志 self.report_id = report_id self.run_case_log_dir = _config.RUN_CASE_LOG_DIR if queue_name: self.queue_name = queue_name else: self.queue_name = 'report-id-{0}-{1}'.format( report_id, get_current_timestamp()) else: # 任务运行用例日志 self.is_task = True self.run_task_id = kwargs.get('run_task_id') self.intf_id = kwargs.get('intf_id') self.testcase_id = kwargs.get('testcase_id') self.is_main = kwargs.get('is_main') self.log_dir = kwargs.get('log_dir') if queue_name: self.queue_name = queue_name else: self.queue_name = 'task-run-log-{0}-{1}'.format( self.run_task_id, get_current_timestamp()) # durable 表示是否持久化, # exclusive是否排他,如果为True则只允许创建这个队列的消费者使用, # auto_delete 表示消费完是否删除队列 self.channel.queue_declare(queue=self.queue_name, durable=arguments['durable'], exclusive=arguments['exclusive'], auto_delete=arguments['auto_delete'], arguments=arguments_ex) self.arguments = copy(arguments_ex) self.arguments.update(copy(arguments))
# -*- coding:utf-8 -*- import json import redis from atp.config.load_config import load_config from atp.env import RUNNING_ENV _config = load_config(RUNNING_ENV) class RedisManager(object): """for ATP""" def __init__(self): pool = redis.ConnectionPool(host=_config.REDIS_HOST, port=_config.REDIS_PORT, db=_config.REDIS_DB_18, password=_config.REDIS_PASSWORD, decode_responses=True) self.conn = redis.Redis(connection_pool=pool) def __new__(cls, *args, **kwargs): if not hasattr(RedisManager, "_instance"): # 反射 RedisManager._instance = object.__new__(cls) return RedisManager._instance def set_user_map(self, username, user_map): self.conn.hmset('user:%s' % username, user_map) def set_token(self, token, username, expire_time=3600):
# -*- coding:utf-8 -*- import flask from flask_sqlalchemy import SQLAlchemy from celery import Celery from kombu import Exchange, Queue from atp.config.load_config import load_config from atp.env import RUNNING_ENV current_config = load_config(RUNNING_ENV) class FlaskCelery(Celery): def __init__(self, *args, **kwargs): super(FlaskCelery, self).__init__(*args, **kwargs) self.patch_task() if 'app' in kwargs: self.init_app(kwargs['app']) def patch_task(self): TaskBase = self.Task _celery = self class ContextTask(TaskBase): abstract = True def __call__(self, *args, **kwargs): if flask.has_app_context():
def create_app(): app = Flask('atp') current_config = load_config(RUNNING_ENV) app.config.from_object(current_config) # with app.app_context(): db.init_app(app) celery.init_app(app) from .views import ( env, Env, project, Project, file, File, support, Support, user, User, stat, Stat, tag, Tag, run_ui, RunUiTestCase, download, Download, page, Page, pageobject, PageObject, ui_testcase, UiTestCase, api_company, ApiCompany, api_system, ApiSystem, api_intf, ApiIntf, api_testcase, ApiTestcase, api_project, ApiProject, api_run, ApiRun, api_public_variable, ApiPublicVariable, api_report, ApiReport, api_product_line, ApiProductLine, api_testcase_main, ApiTestcaseMain, api_task, ApiTask, call_back, CallBack, api_push_log, ApiPushLog, testcase, Testcase, ) app.register_blueprint(env) app.register_blueprint(project) app.register_blueprint(file) app.register_blueprint(support) app.register_blueprint(user) app.register_blueprint(stat) app.register_blueprint(tag) app.register_blueprint(run_ui) app.register_blueprint(download) app.register_blueprint(page) app.register_blueprint(pageobject) app.register_blueprint(ui_testcase) app.register_blueprint(api_company) app.register_blueprint(api_system) app.register_blueprint(api_intf) app.register_blueprint(api_testcase) app.register_blueprint(api_project) app.register_blueprint(api_run) app.register_blueprint(api_public_variable) app.register_blueprint(api_report) app.register_blueprint(api_product_line) app.register_blueprint(api_testcase_main) app.register_blueprint(api_task) app.register_blueprint(call_back) app.register_blueprint(api_push_log) app.register_blueprint(testcase) view = Api(app) view.add_resource(Env, '/atp/auto/env/<action>') view.add_resource(Project, '/atp/auto/project/<action>') view.add_resource(File, '/atp/auto/file/<action>') view.add_resource(Support, '/atp/auto/support/<action>') # view.add_resource(TestPlan, '/atp/auto/testPlan/<action>') # view.add_resource(Report, '/atp/auto/report/<action>') view.add_resource(User, '/atp/auto/user/<action>') view.add_resource(Stat, '/atp/auto/stat/<action>') view.add_resource(Tag, '/atp/auto/tag/<action>') view.add_resource(RunUiTestCase, '/atp/auto/run_ui') view.add_resource(Download, '/atp/auto/download/<action>') view.add_resource(Page, '/atp/auto/page/<action>') view.add_resource(PageObject, '/atp/auto/pageobject/<action>') view.add_resource(UiTestCase, '/atp/auto/uitestcase/<action>') view.add_resource(ApiCompany, '/atp/auto/apiCompany/<action>') view.add_resource(ApiSystem, '/atp/auto/apiSystem/<action>') view.add_resource(ApiIntf, '/atp/auto/apiIntf/<action>') view.add_resource(ApiTestcase, '/atp/auto/apiTestcase/<action>') view.add_resource(ApiProject, '/atp/auto/apiProject/<action>') view.add_resource(ApiRun, '/atp/auto/apiRun') view.add_resource(ApiPublicVariable, '/atp/auto/apiPublicVariable/<action>') view.add_resource(ApiReport, '/atp/auto/apiReport/<action>') view.add_resource(ApiProductLine, '/atp/auto/apiProductLine/<action>') view.add_resource(ApiTestcaseMain, '/atp/auto/apiTestcaseMain/<action>') view.add_resource(ApiTask, '/atp/auto/apiTask/<action>') view.add_resource(CallBack, '/atp/auto/callBack/<action>') view.add_resource(ApiPushLog, '/atp/auto/apiPushLog/<action>') view.add_resource(Testcase, '/atp/auto/testcase/<action>') # configure/initialize all your extensions # app.app_context().push() return app