def post(self, request): data = format_request(request) id = data.get('id') obj = IncepMakeExecTask.objects.get(id=id) host = obj.dst_host database = obj.dst_database if obj.exec_status in ('0', '3', '4'): context = {'status': 2, 'msg': '请不要重复操作'} else: # 获取回滚语句 rollback_sql = GetBackupApi({ 'backupdbName': obj.backup_dbname, 'sequence': obj.sequence }).get_rollback_statement() if rollback_sql is None: context = {'status': 2, 'msg': '没有找到备份记录,回滚失败'} else: incep_of_audit = IncepSqlCheck(rollback_sql, obj.dst_host, obj.dst_database, request.user.username) result = incep_of_audit.make_sqlsha1()[1] rollback_sql = result['SQL'] + ';' rollback_sqlsha1 = result['sqlsha1'] # 将任务进度设置为:回滚中 obj.exec_status = 3 obj.rollback_sqlsha1 = rollback_sqlsha1 obj.save() if result['sqlsha1']: # 异步执行SQL任务 r = incep_async_tasks.delay(user=request.user.username, id=id, host=host, database=database, sql=rollback_sql, sqlsha1=rollback_sqlsha1, exec_status=4) task_id = r.task_id # 将celery task_id写入到表 obj.celery_task_id = task_id obj.save() # 获取OSC执行进度 get_osc_percent.delay(task_id=task_id) context = {'status': 0, 'msg': '提交处理,请查看输出'} else: incep_async_tasks.delay(user=request.user.username, id=id, sql=rollback_sql, host=host, database=database, exec_status=4) context = {'status': 0, 'msg': '提交处理,请查看输出'} return HttpResponse(json.dumps(context))
def post(self, request): data = format_request(request) id = data.get('id') obj = IncepMakeExecTask.objects.get(id=id) host = obj.dst_host database = obj.dst_database sql = obj.sql_content + ';' status = '' query = f"select id,group_concat(exec_status) as exec_status from auditsql_incep_tasks " \ f"where taskid={obj.taskid} group by taskid" for row in IncepMakeExecTask.objects.raw(query): status = row.exec_status.split(',') # 每次只能执行一条任务,不可同时执行,避免数据库压力 if '2' in status or '3' in status: context = {'status': 2, 'msg': '请等待当前任务执行完成'} else: # 避免任务重复点击执行 if obj.exec_status != '0': context = {'status': 2, 'msg': '请不要重复操作任务'} else: # 将任务进度设置为:处理中 obj.exec_status = 2 obj.save() # 如果sqlsha1存在,使用pt-online-schema-change执行 if obj.sqlsha1: # 异步执行SQL任务 r = incep_async_tasks.delay(user=request.user.username, id=id, sql=sql, host=host, database=database, sqlsha1=obj.sqlsha1, backup='yes', exec_status=1) task_id = r.task_id # 将celery task_id写入到表 obj.celery_task_id = task_id obj.save() # 获取OSC执行进度 get_osc_percent.delay(task_id=task_id) context = {'status': 0, 'msg': '提交处理,请查看输出'} else: # 当affected_row>2000时,只执行不备份 if obj.affected_row > 2000: incep_async_tasks.delay(user=request.user.username, id=id, sql=sql, host=host, database=database, exec_status=1) else: # 当affected_row<=2000时,执行并备份 incep_async_tasks.delay(user=request.user.username, id=id, backup='yes', sql=sql, host=host, database=database, exec_status=1) context = {'status': 0, 'msg': '提交处理,请查看输出'} return HttpResponse(json.dumps(context))