public static async Task <SyncResult> RunSyncSystem(List <SystemSync> list) { list = list.Where(x => x.SyncInfo.DoSync).ToList(); CompanySyncProcess.SetSyncProcess(CompanySyncProcess.NoCompany, EnumSyncType.System, EnumSyncProcess.SystemSyncing, 0); DateTime startTime = DateTime.Now; List <SyncResult> taskResultList = new List <SyncResult>(); foreach (SystemSync sync in list) { var task = sync.Receive(); taskResultList.Add(await task); } if (!taskResultList.Any(x => !x.Successful)) { list.ForEach(x => x.CommitReceive()); CompanySyncProcess.SetSyncProcess(CompanySyncProcess.NoCompany, EnumSyncType.System, EnumSyncProcess.NotSyncing, 0); return(new SyncResult { Successful = true, Message = $"Data sync was successful. ({(int)(DateTime.Now - startTime).TotalSeconds} seconds)" }); } else { list.ForEach(x => x.RollbackReceive()); CompanySyncProcess.SetSyncProcess(CompanySyncProcess.NoCompany, EnumSyncType.System, EnumSyncProcess.NotSyncing, 0); return(taskResultList.First(x => !x.Successful)); } }
public static async Task <SyncResult> RunSyncCompanyLookups(List <ReceiveSync> list) { list = list.Where(x => x.SyncInfo.DoSync).ToList(); Guid token = await RunSyncAuth(); List <SyncResult> taskResultList = new List <SyncResult>(); DateTime startTime = DateTime.Now; CompanySyncProcess.SetSyncProcess(Company.CurrentId, EnumSyncType.Lookup, EnumSyncProcess.LookupSyncing, 0); foreach (ReceiveSync sync in list) { if (sync.SyncInfo.Status != EnumTableSyncStatus.CompleteReceive) { var task = sync.Receive(token); taskResultList.Add(await task); } } if (!taskResultList.Any(x => !x.Successful)) { list.ForEach(x => x.CommitReceive()); CompanySyncProcess.SetSyncProcess(Company.CurrentId, EnumSyncType.Lookup, EnumSyncProcess.NotSyncing, 0); return(new SyncResult { Successful = true, Message = $"Data sync was successful. ({(int)(DateTime.Now - startTime).TotalSeconds} seconds)" }); } else { return(taskResultList.First(x => !x.Successful)); } }
public static void CancelSyncCompanyLookups(List <ReceiveSync> list) { list = list.Where(x => x.SyncInfo.DoSync).ToList(); foreach (ReceiveSync sync in list) { if (new EnumTableSyncStatus[] { EnumTableSyncStatus.Receiving, EnumTableSyncStatus.ErrorInReceive, EnumTableSyncStatus.CompleteReceive }.Contains(sync.SyncInfo.Status)) { sync.RollbackReceive(); } } CompanySyncProcess.SetSyncProcess(Company.CurrentId, EnumSyncType.Lookup, EnumSyncProcess.NotSyncing, 0); }
public override async Task <SyncResult> Receive(Guid token) { try { using (HttpClient client = new HttpClient()) { client.Init(token); var query = HttpUtility.ParseQueryString(string.Empty); query["companyId"] = CompanyId.ToString(); query["syncId"] = CompanySyncProcess.GetSyncProcess(CompanyId, EnumSyncType.Core).SyncId.ToString(); HttpResponseMessage response = await client.GetAsync($"api/FieldPO?{query.ToString()}"); if (response.IsSuccessStatusCode) { UpdateStatus(EnumTableSyncStatus.Receiving); List <string[]> list = await response.Content.ReadAsAsync <List <string[]> >(); list.ForEach(x => { int matchId = int.Parse(x[0]); BeforeReceiveRecord(matchId); SyncCoreMatch.SqlUpdate(TableName, matchId, x[1]); }); UpdateStatus(EnumTableSyncStatus.CompleteReceive); return(new SyncResult { Successful = true }); } throw new Exception($"Response StatusCode={response.StatusCode}"); } } catch (Exception e) { UpdateStatus(EnumTableSyncStatus.ErrorInReceive); return(new SyncResult { Successful = false, Task = TableName, Message = e.Message }); } }
public static async Task <SyncResult> CancelSyncCompanyCoreAsync(List <CoreSync> list) { var curProcess = CompanySyncProcess.GetSyncProcessEnum(Company.CurrentId, EnumSyncType.Core); if (curProcess == EnumSyncProcess.CoreSending) { int syncId = CompanySyncProcess.GetSyncProcess(Company.CurrentId, EnumSyncType.Core).SyncId; if (await FinishSync(syncId, false)) { foreach (CoreSync sync in list) { if (new EnumTableSyncStatus[] { EnumTableSyncStatus.Sending, EnumTableSyncStatus.ErrorInSend, EnumTableSyncStatus.CompleteSend }.Contains(sync.SyncInfo.Status)) { sync.RollbackSend(); } } CompanySyncProcess.SetSyncProcess(Company.CurrentId, EnumSyncType.Core, EnumSyncProcess.NotSyncing, 0); } else { return(new SyncResult { Successful = false, Message = "Cannot cancel the sync process." }); } } else if (curProcess == EnumSyncProcess.CoreReceiving) { foreach (CoreSync sync in list) { if (new EnumTableSyncStatus[] { EnumTableSyncStatus.Receiving, EnumTableSyncStatus.ErrorInReceive, EnumTableSyncStatus.CompleteReceive }.Contains(sync.SyncInfo.Status)) { sync.RollbackReceive(); } } CompanySyncProcess.SetSyncProcess(Company.CurrentId, EnumSyncType.Core, EnumSyncProcess.CoreHalfWay, CompanySyncProcess.GetSyncProcess(Company.CurrentId, EnumSyncType.Core).SyncId); } return(new SyncResult { Successful = true }); }
public override async Task <SyncResult> Receive(Guid token) { try { using (HttpClient client = new HttpClient()) { client.Init(token); var query = HttpUtility.ParseQueryString(string.Empty); query["syncId"] = CompanySyncProcess.GetSyncProcess(CompanyId, EnumSyncType.Core).SyncId.ToString(); HttpResponseMessage response = await client.GetAsync($"api/DeleteHistory?{query.ToString()}"); if (response.IsSuccessStatusCode) { UpdateStatus(EnumTableSyncStatus.Receiving); List <DeleteHistory> list = await response.Content.ReadAsAsync <List <DeleteHistory> >(); list.ForEach(x => { string sql = $"INSERT INTO DeleteHistory(TableName, MatchId, CompanyId, TimeStamp, SyncStatus) " + $"values('{x.TableName}', {x.MatchId}, {x.CompanyId}, '{DateTime.Now}', '{EnumRecordSyncStatus.Receiving}')"; MobileCommon.ExecuteNonQuery(sql); }); UpdateStatus(EnumTableSyncStatus.CompleteReceive); return(new SyncResult { Successful = true }); } throw new Exception($"Response StatusCode={response.StatusCode}"); } } catch (Exception e) { UpdateStatus(EnumTableSyncStatus.ErrorInReceive); return(new SyncResult { Successful = false, Task = TableName, Message = e.Message }); } }
public static void SetSyncProcess(int companyId, EnumSyncType syncType, EnumSyncProcess process, int syncId) { CompanySyncProcess item = GetSyncProcess(companyId, syncType); if (item != null) { item.SyncProcess = process; item.SyncId = syncId; string sql = $"Update CompanySyncProcess set SyncProcess='{process}', SyncId={syncId} where CompanyId={companyId} and SyncType='{syncType}'"; MobileCommon.ExecuteNonQuery(sql); } else { _list.Add(new CompanySyncProcess() { CompanyId = companyId, SyncType = syncType, SyncProcess = process, SyncId = syncId }); string sql = $"insert CompanySyncProcess(CompanyId, SyncType, SyncProcess, SyncId) values({companyId}, '{syncType}', '{process}', {syncId})"; MobileCommon.ExecuteNonQuery(sql); } }
public static async Task <SyncResult> RunSyncCompanyCore(List <CoreSync> list) { Guid token = await RunSyncAuth(); if (token == Guid.Empty) { return(new SyncResult { Successful = false, Message = $"Cannot get a sync token." }); } List <SyncResult> taskResultList = new List <SyncResult>(); DateTime startTime = DateTime.Now; int syncId; bool doItAgain = false; if (new EnumSyncProcess[] { EnumSyncProcess.CoreHalfWay, EnumSyncProcess.CoreReceiving }.Contains(CompanySyncProcess.GetSyncProcessEnum(Company.CurrentId, EnumSyncType.Core))) { syncId = CompanySyncProcess.GetSyncProcess(Company.CurrentId, EnumSyncType.Core).SyncId; doItAgain = true; } else { if (EnumSyncProcess.CoreSending == CompanySyncProcess.GetSyncProcessEnum(Company.CurrentId, EnumSyncType.Core)) { syncId = CompanySyncProcess.GetSyncProcess(Company.CurrentId, EnumSyncType.Core).SyncId; } else { syncId = await GetSyncId(); if (syncId == -1) { return(new SyncResult { Successful = false, Message = $"Cannot get a SyncId." }); } CompanySyncProcess.SetSyncProcess(Company.CurrentId, EnumSyncType.Core, EnumSyncProcess.CoreSending, syncId); } foreach (CoreSync sync in list) { if (sync.SyncInfo.Status != EnumTableSyncStatus.CompleteSend) { var task = sync.Send(token, syncId); taskResultList.Add(await task); } } if (!taskResultList.Any(x => !x.Successful)) { // Commit Sync if (await FinishSync(syncId, true)) { list.ForEach(x => x.CommitSend()); CompanySyncProcess.SetSyncProcess(Company.CurrentId, EnumSyncType.Core, EnumSyncProcess.CoreHalfWay, syncId); taskResultList.Clear(); } else { return(new SyncResult { Successful = false, Message = $"The Web server cannot finish sync process." }); } } else { return(taskResultList.First(x => !x.Successful)); } } CompanySyncProcess.SetSyncProcess(Company.CurrentId, EnumSyncType.Core, EnumSyncProcess.CoreReceiving, syncId); foreach (CoreSync sync in list) { if (sync.SyncInfo.Status != EnumTableSyncStatus.CompleteReceive) { var task = sync.Receive(token); taskResultList.Add(await task); } } if (!taskResultList.Any(x => !x.Successful)) { list.ForEach(x => x.CommitReceive()); CompanySyncProcess.SetSyncProcess(Company.CurrentId, EnumSyncType.Core, EnumSyncProcess.NotSyncing, 0); if (doItAgain) { return(await RunSyncCompanyCore(list)); } return(new SyncResult { Successful = true, Message = $"Data sync was successful. ({(int)(DateTime.Now - startTime).TotalSeconds} seconds)" }); } else { return(taskResultList.First(x => !x.Successful)); } }