public List <MigrationTask> GetTasks() { List <MigrationTask> migrationEntities = new List <MigrationTask>(); if (XmlDocument != null) { XmlNodeList migrationEntityNodes = XmlDocument.SelectNodes("TransferData/Tasks/MigrationTask"); foreach (XmlNode migrationEntityNode in migrationEntityNodes) { MigrationTask migrationEntity = new MigrationTask(); try { migrationEntity.ExternalId = Convert.ToInt32(GetInnerText(migrationEntityNode.SelectSingleNode("ExternalId"))); //migrationEntity.Salutation = Convert.ToString(GetInnerText(migrationEntityNode.SelectSingleNode("Salutation"))); migrationEntities.Add(migrationEntity); } catch (Exception e) { Console.WriteLine("Error in reading XML: " + e.Message, "xmlError"); } } } return(migrationEntities); }
public Instance[] AnalyseInstance(JArray objs, JObject inherited, string path) { List <Instance> instances = new List <Instance>(); for (int i = 0; i < objs.Count; i++) { MigrationInstance ins = new MigrationInstance(); JObject obj = objs[i] as JObject; JArray dbs = obj["dbs"] as JArray; List <MigrationTask> tasks = new List <MigrationTask>(); for (int j = 0; j < dbs.Count; j++) { MigrationTask task = new MigrationTask(); AnalyseTask(obj, inherited, dbs[j].ToString(), task, path); tasks.Add(task); } ins.Tasks = tasks.ToArray(); if (tasks.Count > 0) { ins.Name = $"{tasks[0].Source.Server}:{tasks[0].Source.Port} -> {tasks[0].Dest.Server}:{tasks[0].Dest.Port}"; instances.Add(ins); } } return(instances.ToArray()); }
private void AnalyseTask(JObject obj, JObject inherited, string db, MigrationTask task, string path) { task.ReadPages = uint.Parse(GetJValue(obj, inherited, "readPages")); task.Threads = uint.Parse(GetJValue(obj, inherited, "threads")); task.Progress = 0; task.Total = 0; task.Status = DataStates.Idle; task.StartTick = 0; if (!(task.ReadPages > 0)) { throw new Exception("每次读取数据页数必须大于零(readPages)。"); } if (!(task.Threads > 0)) { throw new Exception("并发迁移表数必须大于零(threads)。"); } AnalyseDatabase(obj["source"] as JObject, inherited, task.Source, "source"); AnalyseDatabase(obj["dest"] as JObject, inherited, task.Dest, "dest"); AnalyseDatabase(db, task); AnalyseTable($"{path}\\{GetJValue(obj, inherited, "tables")}", task); task.Name = $"{task.Source.DB} -> {task.Dest.DB}"; }
/// <summary> /// This method contains the logic performed when the context menu is selected. /// </summary> /// <param name="objectToExecuteOn">TCObject on which the context menu task is performed.</param> /// <param name="taskContext">Task Context of the AddOn Task.</param> /// <returns>TCObject Instance</returns> public override TCObject Execute(TCObject objectToExecuteOn, TCAddOnTaskContext taskContext) { TCProject rootFolder = objectToExecuteOn as TCProject; //Opens the File Upload dialog. string filepath = taskContext.GetFilePath("Upload Selenium TestCase Excel"); //Instantiation of this object is mandatory. This class contains the necessary methods for migration. ToscaObjectDefinition toscaObjects = new ToscaObjectDefinition(); try { //Instantiates the MigrationTask class that contains the business logic of migration. MigrationTask objectImporter = new MigrationTask(toscaObjects, Engine.Html); //Entry point of MigrationTask class. objectImporter.ProcessTestScriptFile(filepath); //Calling this method is mandatory. It outputs the file containing the migrated object information. string xtcFilePath = toscaObjects.FinishObjectDefinitionTask(); //Imports the output file from MigrationTask. rootFolder?.ImportExternalObjects(xtcFilePath); //Cleans the migration metafiles. Directory.Delete(toscaObjects.MigrationFolderPath, true); } catch (Exception e) { //Pops-up the error message in case of any error in Migration. taskContext.ShowErrorMessage("Exception occured", e.Message); } return(null); }
/// <summary> /// Resets the Background mgrator to make it ready for a new migration /// </summary> public static void Reset() { if (MessageStack != null) { MessageStack = null; } if (MigrationTask != null) { MigrationTask.Dispose(); MigrationTask = null; } }
static void Main(string[] args) { int i = 0; while (i < 10) { try { using (MigrationTask task = new MigrationTask()) { Console.WriteLine("URL Format is"); Console.WriteLine("\tscheme://host:port?username=username&password=password&root-folder=INBOX."); Console.WriteLine("\t\t\tscheme = imap or imap-ssl"); Console.WriteLine("\t\t\tport is optional"); Console.WriteLine("\t\t\troot-folder is optional"); string url = Prompt("SourceURL"); task.SourceServer = new ServerAddress(url); url = Prompt("DestinationURL"); task.DestinationServer = new ServerAddress(url); task.CheckIfMessageExists = true; task.Migrate(); //Console.ReadLine(); } break; } catch (Exception ex) { System.IO.File.AppendAllText("errors.txt", DateTimeOffset.Now.ToString() + "\r\n" + ex.ToString()); i++; continue; } } }
private void MigrateTable(MigrationTask task, MigrationTable table, out string reason) { reason = "取消操作"; if (status.Stopped) { return; } if (Connect(task, task.Source, out IDBMSReader source, task.Dest, out IDBMSWriter dest)) { Dictionary <string, object> parms = new Dictionary <string, object>(); dest.QueryParam(task.Params, parms); dest.BeginTransaction(); try { // 迁移数据 MigrateTableWithScript(task, table, parms, source, dest, out reason); if (table.Status != DataStates.Error && !status.Stopped) { dest.CommitTransaction(); table.Status = DataStates.Done; } else { dest.RollbackTransaction(); table.Status = DataStates.Error; } } catch (Exception ex) { dest.RollbackTransaction(); table.Status = DataStates.Error; reason = ex.Message; } source.Close(); dest.Close(); }
private void AnalyseDatabase(string db, MigrationTask task) { string[] dbs = db.Split(','); string dbName = Database.AnalyseDB(dbs[0]); string schema = Database.AnalyseSchema(dbs[0]); task.Source.DB = dbName; if (!string.IsNullOrEmpty(schema)) { task.Source.Schema = schema; } if (dbs.Length > 1) { db = dbs[1]; dbName = Database.AnalyseDB(db); schema = Database.AnalyseSchema(db); } task.Dest.DB = dbName; if (!string.IsNullOrEmpty(schema)) { task.Dest.Schema = schema; } }
private static void UpdateMigrationGridListItem(MigrationTask migrationTask) { // If (MoRef == null) something went wrong. Try update this item by name and exit the function. var item = new GridRow(); if (migrationTask.MoRef == null) { item = MigrationGridList.FirstOrDefault(x => x.Name == migrationTask.EntityName); } else { item = MigrationGridList.FirstOrDefault(x => x.MoRef == migrationTask.MoRef); } if (migrationTask.State != null) { item.State = migrationTask.State; } item.StateReason = migrationTask.StateReason; item.Progress = migrationTask.Progress; item.Start = migrationTask.Start; item.Finish = migrationTask.Finish; MigrationLogger.Trace("{@value0}", item); }
public void Add(MigrationTask migrationTask) => _jobs.Add(migrationTask);
private void AnalyseTable(string file, MigrationTask task) { JObject obj = LoadAndDeserialize(file); JArray tables = obj["tables"] as JArray; List <MigrationTable> buf = new List <MigrationTable>(); task.Params = obj["params"].ToString(); // #1: 获取所有表 for (int i = 0; i < tables.Count; i++) { JObject o = tables[i] as JObject; string[] names = o["name"].ToString().Split(','); MigrationTable table = new MigrationTable() { SourceName = Table.AnalyseName(names[0]), SourceSchema = Table.AnalyseSchema(names[0]), DestName = Table.AnalyseName(names.Length > 1 ? names[1] : names[0]), DestSchema = Table.AnalyseSchema(names.Length > 1 ? names[1] : names[0]), Order = int.Parse(o["order"].ToString()), OrderSQL = o["orderSQL"].ToString(), WhereSQL = o["whereSQL"].ToString(), PageSize = uint.Parse(o["pageSize"].ToString()), WriteMode = "UPDATE".Equals(o["mode"].ToString().ToUpper()) ? WriteModes.Update : WriteModes.Append, KeyFields = o["keyFields"].ToString().Split(','), SkipFields = o["skipFields"].ToString().Split(','), Filter = o["filter"].ToString(), KeepIdentity = true, DestFields = new string[] { }, References = o.ContainsKey("references") ? o["references"].ToString().Split(',') : new string[] { }, Total = 0, Progress = 0, Status = DataStates.Idle }; buf.Add(table); if (table.WriteMode == WriteModes.Update && "".Equals(table.KeyFields[0])) { throw new Exception($"表 {table.SourceName} 配置有误!更新模式必须指定主键字段(keyFields)。"); } if ("".Equals(table.OrderSQL)) { throw new Exception($"表 {table.SourceName} 配置有误!必须指定稳定的排序规则(orderSQL)。"); } if (table.PageSize <= 0) { throw new Exception($"表 {table.SourceName} 配置有误!批量记录数必须大于零(pageSize)。"); } } // #2: 生成结构 if (buf.Count > 0) { buf.Sort(new TableComparer()); int order = buf[0].Order; List <MigrationTable> tmpBuf = new List <MigrationTable>() { buf[0] }; List <List <MigrationTable> > rstBuf = new List <List <MigrationTable> >() { tmpBuf }; for (int i = 1; i < buf.Count; i++) { if (buf[i].Order != order) { tmpBuf = new List <MigrationTable>() { buf[i] }; rstBuf.Add(tmpBuf); order = buf[i].Order; } else { tmpBuf.Add(buf[i]); } } task.Tables = new MigrationTable[rstBuf.Count][]; for (int i = 0; i < task.Tables.Length; i++) { task.Tables[i] = rstBuf[i].ToArray(); } } else { task.Tables = new MigrationTable[][] { } }; }
private async void RunMigration(GridRow migrationVM) { var progressHanlder = new Progress <MigrationTask>(value => { UpdateMigrationGridListItem(value); SortGridCollection(); }); var migration = progressHanlder as IProgress <MigrationTask>; var result = await Task.Run(() => { var thisTask = new MigrationTask(); try { migrationVM.MoRef = vapiConnection.MigrateVirtualMachine(migrationVM); } catch (Exception e) { // this is horrible and hacky but good enough for now. // polish this! => add error message to MigrationTask for inclusion in datagrid/logs/output? Task.Run(() => { MessageBoxResult exceptionBox = MessageBox.Show(e.Source + " Says:\n\n" + e.Message + "\n\n" + e.StackTrace, "Problem Migrating " + migrationVM.Name.ToUpper(), MessageBoxButton.OK); }); thisTask.State = "error"; thisTask.Start = DateTime.Now; thisTask.EntityName = migrationVM.Name; thisTask.DestinationCompute = migrationVM.DestinationCompute; thisTask.DestinationStorage = migrationVM.DestinationStorage; thisTask.Progress = migrationVM.Progress; return(thisTask); } if (migrationVM.MoRef == null) { MigrationLogger.Error("{0}: MigrateVirtualMachine() returned null", migrationVM.Name); // task failed to start. Return what we know. thisTask.State = "error"; thisTask.Start = DateTime.Now; thisTask.EntityName = migrationVM.Name; thisTask.DestinationCompute = migrationVM.DestinationCompute; thisTask.DestinationStorage = migrationVM.DestinationStorage; thisTask.Progress = migrationVM.Progress; return(thisTask); } else if (migrationVM.MoRef == "skipped-verifyfailed") { MigrationLogger.Info("{0}: Skipping VM", migrationVM.Name); // task failed to start. Return what we know. thisTask.State = "error"; thisTask.Start = DateTime.Now; thisTask.EntityName = migrationVM.Name; thisTask.DestinationCompute = migrationVM.DestinationCompute; thisTask.DestinationStorage = migrationVM.DestinationStorage; thisTask.Progress = migrationVM.Progress; return(thisTask); } bool running = true; do { thisTask = vapiConnection.GetTask(migrationVM.MoRef); if (migration != null) { migration.Report(thisTask); } if (thisTask.State != "running") { running = false; } Thread.Sleep(500); } while (running); return(vapiConnection.GetTask(migrationVM.MoRef)); }); UpdateMigrationGridListItem(result); SortGridCollection(); }