Beispiel #1
0
def async_execute_export(id=None, username=None, sql=None):
    """异步执行导出, 获取当前任务的db信息"""
    config = models.DbOrdersExecuteTasks.objects.filter(pk=id).annotate(
        host=F('order__cid__host'),
        port=F('order__cid__port'),
        charset=F('order__cid__character'),
        rds_type=F('order__cid__rds_type'),
        rds_category=F('order__rds_category')).values('host', 'port',
                                                      'charset', 'rds_type',
                                                      'rds_category').first()

    # 获取当前任务对应的数据库
    database = models.DbOrdersExecuteTasks.objects.filter(pk=id).annotate(
        database=F('order__database')).values('database', 'task_id',
                                              'file_format').first()
    config['id'] = id
    config['username'] = username
    config.update(database)
    config.update(REOMOTE_USER)

    # 执行SQL
    execute_export = ExecuteExport(config=config)
    result = execute_export.run(remove_sql_comment(sql))
    # 更新当前任务的进度
    update_task_progress_to_finish(id=id, result=result)
Beispiel #2
0
 def execute(self, request):
     op = self.validated_data['op']
     obj = models.DbOrdersExecuteTasks.objects.get(
         pk=self.validated_data['id'])
     database = models.DbOrders.objects.get(pk=obj.order_id).database
     sql = libs.remove_sql_comment(obj.sql)
     try:
         syntaxcompile = re.compile(r'^ALTER(\s+)TABLE(\s+)([\S]*)(.*)',
                                    re.I)
         syntaxmatch = syntaxcompile.match(sql)
         # 由于gh-ost不支持反引号,会被解析成命令,因此此处替换掉
         table = syntaxmatch.group(3).replace('`', '')
         # 将schema.table进行处理,这种情况gh-ost不识别,只保留table
         if len(table.split('.')) > 1:
             table = table.split('.')[1]
         sock = f"/tmp/gh-ost.{database}.{table}.sock"
         if os.path.exists(sock):
             """
             echo throttle | nc -U /tmp/gh-ost.test.sample_data_0.sock
             echo no-throttle | nc -U /tmp/gh-ost.test.sample_data_0.sock
             """
             if op == 'pause':
                 pause_cmd = f"echo throttle | nc -U {sock}"
                 p = subprocess.Popen(pause_cmd, shell=True)
                 p.wait()
                 obj.progress = 4
                 obj.execute_time = timezone.now()
                 obj.save()
                 return '暂停操作已执行,请查看输出'
             if op == 'recovery':
                 recovery_cmd = f"echo no-throttle | nc -U {sock}"
                 p = subprocess.Popen(recovery_cmd, shell=True)
                 p.wait()
                 obj.progress = 3
                 obj.execute_time = timezone.now()
                 obj.save()
                 return '恢复操作已执行,请查看输出'
         else:
             return f'操作失败:不能发现sock文件[{sock}]'
     except AttributeError as err:
         return '非有效的ALTER语句,禁止操作'