def timingtask(request): workflowId = request.POST.get('workflowid') run_date = request.POST.get('run_date') if run_date is None or workflowId is None: context = {'errMsg': '时间不能为空'} return render(request, 'error.html', context) elif run_date < datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'): context = {'errMsg': '时间不能小于当前时间'} return render(request, 'error.html', context) workflowDetail = SqlWorkflow.objects.get(id=workflowId) if can_timingtask(request.user, workflowId) is False: context = {'errMsg': '你无权操作当前工单!'} return render(request, 'error.html', context) run_date = datetime.datetime.strptime(run_date, "%Y-%m-%d %H:%M:%S") url = getDetailUrl(request) + str(workflowId) + '/' job_id = Const.workflowJobprefix['sqlreview'] + '-' + str(workflowId) # 使用事务保持数据一致性 try: with transaction.atomic(): # 将流程状态修改为定时执行 workflowDetail.status = Const.workflowStatus['timingtask'] workflowDetail.save() # 调用添加定时任务 add_sqlcronjob(job_id, run_date, workflowId, url) except Exception as msg: context = {'errMsg': msg} return render(request, 'error.html', context) return HttpResponseRedirect(reverse('sql:detail', args=(workflowId, )))
def execute(request): workflowId = request.POST['workflowid'] if workflowId == '' or workflowId is None: context = {'errMsg': 'workflowId参数为空.'} return render(request, 'error.html', context) workflowId = int(workflowId) workflowDetail = SqlWorkflow.objects.get(id=workflowId) instance_name = workflowDetail.instance_name db_name = workflowDetail.db_name url = getDetailUrl(request) + str(workflowId) + '/' user = request.user if can_execute(request.user, workflowId) is False: context = {'errMsg': '你无权操作当前工单!'} return render(request, 'error.html', context) # 将流程状态修改为执行中,并更新reviewok_time字段 workflowDetail.status = Const.workflowStatus['executing'] workflowDetail.reviewok_time = timezone.now() workflowDetail.save() # 判断是通过inception执行还是直接执行,is_manual=0则通过inception执行,is_manual=1代表inception审核不通过,需要直接执行 if workflowDetail.is_manual == 0: # 执行之前重新split并check一遍,更新SHA1缓存;因为如果在执行中,其他进程去做这一步操作的话,会导致inception core dump挂掉 try: splitReviewResult = InceptionDao().sqlautoReview( workflowDetail.sql_content, workflowDetail.instance_name, db_name, isSplit='yes') except Exception as msg: context = {'errMsg': msg} return render(request, 'error.html', context) workflowDetail.review_content = json.dumps(splitReviewResult) try: workflowDetail.save() except Exception: # 关闭后重新获取连接,防止超时 connection.close() workflowDetail.save() # 采取异步回调的方式执行语句,防止出现持续执行中的异常 t = Thread(target=execute_call_back, args=(workflowId, instance_name, url)) t.start() else: # 采取异步回调的方式执行语句,防止出现持续执行中的异常 t = Thread(target=execute_skipinc_call_back, args=(workflowId, instance_name, db_name, workflowDetail.sql_content, url)) t.start() # 删除定时执行job if workflowDetail.status == Const.workflowStatus['timingtask']: job_id = Const.workflowJobprefix['sqlreview'] + '-' + str(workflowId) del_sqlcronjob(job_id) return HttpResponseRedirect(reverse('sql:detail', args=(workflowId, )))
def timingtask(request): workflowId = request.POST.get('workflowid') run_date = request.POST.get('run_date') if run_date is None or workflowId is None: context = {'errMsg': '时间不能为空'} return render(request, 'error.html', context) elif run_date < datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'): context = {'errMsg': '时间不能小于当前时间'} return render(request, 'error.html', context) workflowDetail = SqlWorkflow.objects.get(id=workflowId) if can_timingtask(request.user, workflowId) is False: context = {'errMsg': '你无权操作当前工单!'} return render(request, 'error.html', context) # 判断是否高危SQL,禁止执行 if SysConfig().sys_config.get('critical_ddl_regex', '') != '': if InceptionDao().criticalDDL(workflowDetail.sql_content): context = {'errMsg': '高危语句,禁止执行!'} return render(request, 'error.html', context) run_date = datetime.datetime.strptime(run_date, "%Y-%m-%d %H:%M:%S") url = getDetailUrl(request, workflowId) job_id = Const.workflowJobprefix['sqlreview'] + '-' + str(workflowId) # 使用事务保持数据一致性 try: with transaction.atomic(): # 将流程状态修改为定时执行 workflowDetail.status = Const.workflowStatus['timingtask'] workflowDetail.save() # 调用添加定时任务 add_sqlcronjob(job_id, run_date, workflowId, url) # 增加工单日志 # 获取audit_id audit_id = Workflow.auditinfobyworkflow_id( workflow_id=workflowId, workflow_type=WorkflowDict.workflow_type['sqlreview']).audit_id workflowOb.add_workflow_log( audit_id=audit_id, operation_type=4, operation_type_desc='定时执行', operation_info="定时执行时间:{}".format(run_date), operator=request.user.username, operator_display=request.user.display) except Exception as msg: logger.error(traceback.format_exc()) context = {'errMsg': msg} return render(request, 'error.html', context) return HttpResponseRedirect(reverse('sql:detail', args=(workflowId, )))
def execute(request): workflowId = request.POST['workflowid'] if workflowId == '' or workflowId is None: context = {'errMsg': 'workflowId参数为空.'} return render(request, 'error.html', context) workflowId = int(workflowId) workflowDetail = SqlWorkflow.objects.get(id=workflowId) instance_name = workflowDetail.instance_name db_name = workflowDetail.db_name url = getDetailUrl(request, workflowId) if can_execute(request.user, workflowId) is False: context = {'errMsg': '你无权操作当前工单!'} return render(request, 'error.html', context) # 判断是否高危SQL,禁止执行 if SysConfig().sys_config.get('critical_ddl_regex', '') != '': if InceptionDao().criticalDDL(workflowDetail.sql_content): context = {'errMsg': '高危语句,禁止执行!'} return render(request, 'error.html', context) # 将流程状态修改为执行中,并更新reviewok_time字段 workflowDetail.status = Const.workflowStatus['executing'] workflowDetail.reviewok_time = timezone.now() workflowDetail.save() # 判断是通过inception执行还是直接执行,is_manual=0则通过inception执行,is_manual=1代表inception审核不通过,需要直接执行 if workflowDetail.is_manual == 0: # 执行之前重新split并check一遍,更新SHA1缓存;因为如果在执行中,其他进程去做这一步操作的话,会导致inception core dump挂掉 try: splitReviewResult = InceptionDao( instance_name=instance_name).sqlautoReview( workflowDetail.sql_content, db_name, isSplit='yes') except Exception as msg: logger.error(traceback.format_exc()) context = {'errMsg': msg} return render(request, 'error.html', context) workflowDetail.review_content = json.dumps(splitReviewResult) try: workflowDetail.save() except Exception: # 关闭后重新获取连接,防止超时 connection.close() workflowDetail.save() # 采取异步回调的方式执行语句,防止出现持续执行中的异常 t = Thread(target=execute_call_back, args=(workflowId, instance_name, url)) t.start() else: # 采取异步回调的方式执行语句,防止出现持续执行中的异常 t = Thread(target=execute_skipinc_call_back, args=(workflowId, instance_name, db_name, workflowDetail.sql_content, url)) t.start() # 删除定时执行job if workflowDetail.status == Const.workflowStatus['timingtask']: job_id = Const.workflowJobprefix['sqlreview'] + '-' + str(workflowId) del_sqlcronjob(job_id) # 增加工单日志 # 获取audit_id audit_id = Workflow.auditinfobyworkflow_id( workflow_id=workflowId, workflow_type=WorkflowDict.workflow_type['sqlreview']).audit_id workflowOb.add_workflow_log(audit_id=audit_id, operation_type=5, operation_type_desc='执行工单', operation_info="人工操作执行", operator=request.user.username, operator_display=request.user.display) return HttpResponseRedirect(reverse('sql:detail', args=(workflowId, )))