Пример #1
0
        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));
            }
        }
Пример #2
0
        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));
            }
        }
Пример #3
0
        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);
        }
Пример #4
0
        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
            });
        }
Пример #5
0
        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));
            }
        }