This action will force synchronization from 小圈圈/Archery, which will overwrite any changes that you have made since you forked the repository, and can not be recovered!!!
Synchronous operation will process in the background and will refresh the page when finishing processing. Please be patient.
# -*- coding: UTF-8 -*-from django.db import close_old_connections, connection, transactionfrom django_redis import get_redis_connectionfrom common.utils.const import WorkflowDictfrom sql.engines.models import ReviewResult, ReviewSetfrom sql.models import SqlWorkflowfrom sql.notify import notify_for_executefrom sql.utils.workflow_audit import Auditfrom sql.engines import get_enginedef execute(workflow_id, user=None):"""为延时或异步任务准备的execute, 传入工单ID和执行人信息"""# 使用当前读防止重复执行with transaction.atomic():workflow_detail = SqlWorkflow.objects.select_for_update().get(id=workflow_id)# 只有排队中和定时执行的数据才可以继续执行,否则直接抛错if workflow_detail.status not in ['workflow_queuing', 'workflow_timingtask']:raise Exception('工单状态不正确,禁止执行!')# 将工单状态修改为执行中else:SqlWorkflow(id=workflow_id, status='workflow_executing').save(update_fields=['status'])# 增加执行日志audit_id = Audit.detail_by_workflow_id(workflow_id=workflow_id,workflow_type=WorkflowDict.workflow_type['sqlreview']).audit_idAudit.add_log(audit_id=audit_id,operation_type=5,operation_type_desc='执行工单',operation_info='工单开始执行' if user else '系统定时执行工单',operator=user.username if user else '',operator_display=user.display if user else '系统')execute_engine = get_engine(instance=workflow_detail.instance)return execute_engine.execute_workflow(workflow=workflow_detail)def execute_callback(task):"""异步任务的回调, 将结果填入数据库等等使用django-q的hook, 传入参数为整个tasktask.result 是真正的结果"""# https://stackoverflow.com/questions/7835272/django-operationalerror-2006-mysql-server-has-gone-awayif connection.connection and not connection.is_usable():close_old_connections()workflow_id = task.args[0]# 判断工单状态,如果不是执行中的,不允许更新信息,直接抛错记录日志with transaction.atomic():workflow = SqlWorkflow.objects.get(id=workflow_id)if workflow.status != 'workflow_executing':raise Exception(f'工单{workflow.id}状态不正确,禁止重复更新执行结果!')workflow.finish_time = task.stoppedif not task.success:# 不成功会返回错误堆栈信息,构造一个错误信息workflow.status = 'workflow_exception'execute_result = ReviewSet(full_sql=workflow.sqlworkflowcontent.sql_content)execute_result.rows = [ReviewResult(stage='Execute failed',errlevel=2,stagestatus='异常终止',errormessage=task.result,sql=workflow.sqlworkflowcontent.sql_content)]elif task.result.warning or task.result.error:execute_result = task.resultworkflow.status = 'workflow_exception'else:execute_result = task.resultworkflow.status = 'workflow_finish'# 保存执行结果workflow.sqlworkflowcontent.execute_result = execute_result.json()workflow.sqlworkflowcontent.save()workflow.save()# 增加工单日志audit_id = Audit.detail_by_workflow_id(workflow_id=workflow_id,workflow_type=WorkflowDict.workflow_type['sqlreview']).audit_idAudit.add_log(audit_id=audit_id,operation_type=6,operation_type_desc='执行结束',operation_info='执行结果:{}'.format(workflow.get_status_display()),operator='',operator_display='系统')# DDL工单结束后清空实例资源缓存if workflow.syntax_type == 1:r = get_redis_connection("default")for key in r.scan_iter(match='*insRes*', count=2000):r.delete(key)# 发送消息notify_for_execute(workflow)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。