Example #1
0
        private async Task Connect()
        {
            if (_client.IsLoggedIn)
            {
                return;
            }

            await RetryHelper.RetryOnExceptionAsync(
                async() =>
            {
                if (_client.IsLoggedIn)
                {
                    return;
                }
                if (_loginToken != null)
                {
                    await _client.LoginAsync(_loginToken);
                }
                else if (_authInfos != null)
                {
                    _loginToken = await _client.LoginAsync(_authInfos);
                }
                else
                {
                    await _client.LoginAnonymousAsync();
                }
            }, 2, TimeSpan.FromSeconds(2), CancellationToken.None);
        }
Example #2
0
 public async Task <string> GetMovieAsync(int serviceId, string movieId)
 {
     return(await RetryHelper.RetryOnExceptionAsync(
                this.maxRetryAttempts, this.pauseBetweenFailures, async() =>
                await this.GetMovieOperationAsync(serviceId, movieId)
                ));
 }
Example #3
0
 public async Task Connect()
 {
     if (!_client.IsLoggedIn)
     {
         await RetryHelper.RetryOnExceptionAsync(async() => await _client.LoginAnonymousAsync(), 2, TimeSpan.FromSeconds(1), CancellationToken.None);
     }
 }
Example #4
0
        public async Task <List <DTO.OrderProcessProducts> > GetDodgyDealers()
        {
            List <DTO.OrderProcessProducts> products = new List <DTO.OrderProcessProducts>();

            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(Vendors.DodgyDealers);
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                HttpResponseMessage response = new HttpResponseMessage();

                await RetryHelper.RetryOnExceptionAsync <HttpRequestException>
                    (maxRetryAttempts, pauseBetweenFailures, async() => {
                    response = await client.GetAsync("api/product");
                    response.EnsureSuccessStatusCode();
                });

                if (response.IsSuccessStatusCode)
                {
                    List <Product> returnData = new List <Product>();

                    string result = await response.Content.ReadAsStringAsync();

                    returnData = JsonConvert.DeserializeObject <List <Product> >(result);

                    foreach (Product prod in returnData)
                    {
                        products.Add(Mappers.MapProduct(prod));
                    }
                }
            }

            return(products);
        }
Example #5
0
        protected async Task RemindersRange(int iterations = 1000)
        {
            await Task.WhenAll(Enumerable.Range(1, iterations).Select(async i =>
            {
                GrainReference grainRef = MakeTestGrainReference();

                await RetryHelper.RetryOnExceptionAsync <Task>(10, RetryOperation.Sigmoid, async() =>
                {
                    await remindersTable.UpsertRow(CreateReminder(grainRef, i.ToString()));
                    return(Task.CompletedTask);
                });
            }));

            var rows = await remindersTable.ReadRows(0, uint.MaxValue);

            Assert.Equal(rows.Reminders.Count, iterations);

            rows = await remindersTable.ReadRows(0, 0);

            Assert.Equal(rows.Reminders.Count, iterations);

            var remindersHashes = rows.Reminders.Select(r => r.GrainRef.GetUniformHashCode()).ToArray();

            SafeRandom random = new SafeRandom();

            await Task.WhenAll(Enumerable.Range(0, iterations).Select(i =>
                                                                      TestRemindersHashInterval(remindersTable, (uint)random.Next(), (uint)random.Next(),
                                                                                                remindersHashes)));
        }
Example #6
0
        public static async Task <List <Route> > GetRoutes(string alias, CancellationToken ct)
        {
            Uri uri = new Uri($"https://homework.appulate.com/api/Route/outgoing?airport={alias}");
            var maxRetryAttempts     = 3;
            var pauseBetweenFailures = TimeSpan.FromSeconds(3);
            var content = new List <Route>();

            try
            {
                using (var client = new HttpClient())
                    using (var request = new HttpRequestMessage(HttpMethod.Get, uri))
                    {
                        await RetryHelper.RetryOnExceptionAsync <WebException>
                            (maxRetryAttempts, pauseBetweenFailures, async() =>
                        {
                            var response = await client.SendAsync(request, ct).ConfigureAwait(false);
                            content      = JsonConvert.DeserializeObject <List <Route> >(await response.Content.ReadAsStringAsync());
                        });
                    }
            }
            catch (WebException wex)
            {
                Console.WriteLine(wex.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            return(content);
        }
        internal async Task PersistenceStorage_WriteReadWriteReadStatesInParallel(string prefix = nameof(this.PersistenceStorage_WriteReadWriteReadStatesInParallel), int countOfGrains = 100)
        {
            //As data is written and read the Version numbers (ETags) are as checked for correctness (they change).
            //Additionally the Store_WriteRead tests does its validation.
            var    grainTypeName   = GrainTypeGenerator.GetGrainType <Guid>();
            int    StartOfRange    = 33900;
            int    CountOfRange    = countOfGrains;
            string grainIdTemplate = $"{prefix}-{{0}}";

            //Since the version is NULL, storage provider tries to insert this data
            //as new state. If there is already data with this class, the writing fails
            //and the storage provider throws. Essentially it means either this range
            //is ill chosen or the test failed due to another problem.
            var grainStates = Enumerable.Range(StartOfRange, CountOfRange).Select(i => this.GetTestReferenceAndState(string.Format(grainIdTemplate, i), null)).ToList();

            // Avoid parallelization of the first write to not stress out the system with deadlocks
            // on INSERT
            foreach (var grainData in grainStates)
            {
                //A sanity checker that the first version really has null as its state. Then it is stored
                //to the database and a new version is acquired.
                var firstVersion = grainData.Item2.ETag;
                Assert.Null(firstVersion);

                await Store_WriteRead(grainTypeName, grainData.Item1, grainData.Item2).ConfigureAwait(false);

                var secondVersion = grainData.Item2.ETag;
                Assert.NotEqual(firstVersion, secondVersion);
            }
            ;

            int MaxNumberOfThreads = Environment.ProcessorCount * 3;

            // The purpose of Parallel.ForEach is to ensure the storage provider will be tested from
            // multiple threads concurrently, as would happen in running system also. Nevertheless
            // limit the degree of parallelization (concurrent threads) to avoid unnecessarily
            // starving and growing the thread pool (which is very slow) if a few threads coupled
            // with parallelization via tasks can force most concurrency scenarios.

            Parallel.ForEach(grainStates, new ParallelOptions {
                MaxDegreeOfParallelism = MaxNumberOfThreads
            }, async grainData =>
            {
                // This loop writes the state consecutive times to the database to make sure its
                // version is updated appropriately.
                for (int k = 0; k < 10; ++k)
                {
                    var versionBefore = grainData.Item2.ETag;
                    await RetryHelper.RetryOnExceptionAsync(5, RetryOperation.Sigmoid, async() =>
                    {
                        await Store_WriteRead(grainTypeName, grainData.Item1, grainData.Item2);
                        return(Task.CompletedTask);
                    });

                    var versionAfter = grainData.Item2.ETag;
                    Assert.NotEqual(versionBefore, versionAfter);
                }
            });
        }
Example #8
0
        private async Task <bool> UpdateSingleItem(IGrouping <string, Tuple <UpdateInfo, IUpdateItem> > task)
        {
            var firstItem = task.First().Item2;
            var progress  = new Progress <double>(d => labelPercent.Text = $"Downloaded {d:F1}% of {firstItem.ItemSize}.  Overall: {_completedSize} / {_overallSize}.");

            SetStatus($"Updating {firstItem.TargetPath.Name}");
            SetStatus($"Updating {InstallDirectoryHelper.GetRelativePath(firstItem.TargetPath)}", false, true);

            // todo move logging to update methods
            if (firstItem.TargetPath.Exists)
            {
                SetStatus($"Deleting old file {firstItem.TargetPath.FullName}", false, true);
                firstItem.TargetPath.Delete();
            }

            var sourcesToAttempt = task.Where(x => !_badUpdateSources.Contains(x.Item1)).OrderByDescending(x => x.Item1.SourcePriority).ToList();

            if (sourcesToAttempt.Count == 0)
            {
                Console.WriteLine("There are no working sources to download from. Check the log for reasons why the sources failed.");

                _failedItems.Add(task);
                return(false);
            }

            Exception ex = null;

            foreach (var source in sourcesToAttempt)
            {
                try
                {
                    SetStatus($"Attempting download from source {source.Item1.Origin}", false, true);

                    await RetryHelper.RetryOnExceptionAsync(() => source.Item2.Update(progress, _cancelToken.Token), 3, TimeSpan.FromSeconds(3), _cancelToken.Token);

                    ex = null;
                    break;
                }
                catch (Exception e)
                {
                    Console.WriteLine($"Marking source {source.Item1.Origin} as broken because of exception: {e}");

                    ex = e;
                    _badUpdateSources.Add(source.Item1);
                }
            }
            // Check if all sources failed
            if (ex != null)
            {
                Console.WriteLine("There are no working sources to download from. Check the log for reasons why the sources failed.");

                _failedItems.Add(task);
                _failedExceptions.Add(ex);
                return(false);
            }

            SetStatus($"Download OK {firstItem.ItemSize}", false, true);
            return(true);
        }
Example #9
0
        private async Task <List <INode> > GetNodesFromLinkAsync(CancellationToken cancellationToken)
        {
            await Connect();

            await RetryHelper.RetryOnExceptionAsync(async() => _allNodes = (await _client.GetNodesFromLinkAsync(CurrentFolderLink)).ToList(), 2, TimeSpan.FromSeconds(1), cancellationToken);

            return(_allNodes);
        }
        public async Task RetryOnExceptionAsync_WhenOperationSucceed_ReturnsTrue()
        {
            // Arrange & Act
            var result = await RetryHelper.RetryOnExceptionAsync(1, TimeSpan.Zero, () => { return(Task.CompletedTask); });

            // Assert
            result.Should().BeTrue();
        }
        public async Task RetryOnExceptionAsync_WhenGivingZeroTimesRetry_ReturnsFalse()
        {
            // Arrange & Act
            var result = await RetryHelper.RetryOnExceptionAsync(0, TimeSpan.Zero, () => { return(Task.CompletedTask); });

            // Assert
            result.Should().BeFalse();
        }
Example #12
0
        public override async Task <List <UpdateTask> > GetUpdateItems(CancellationToken cancellationToken)
        {
            await Connect(false);

            await RetryHelper.RetryOnExceptionAsync(async() => _allNodes = (await _client.GetNodesFromLinkAsync(_currentFolderLink)).ToList(), 2, TimeSpan.FromSeconds(1), cancellationToken);

            return(await base.GetUpdateItems(cancellationToken));
        }
Example #13
0
        private async Task <IList <SideloaderUpdateItem> > CollectTasksAsync(List <INode> nodes, CancellationToken cancellationToken)
        {
            IList <SideloaderUpdateItem> results = null;

            await RetryHelper.RetryOnExceptionAsync(async() =>
            {
                results = await Task.Run(
                    () => CollectTasks(nodes, cancellationToken).ToList(),
                    cancellationToken);
            }, 2, TimeSpan.FromSeconds(1), cancellationToken);

            return(results);
        }
Example #14
0
        public static async Task <List <UpdateTask> > GetUpdates(CancellationToken cancellationToken, IUpdateSource[] updateSources, string[] filterByGuids = null)
        {
            var results = new ConcurrentBag <UpdateTask>();

            // First start all of the sources, then wait until they all finish
            var concurrentTasks = updateSources.Select(source => RetryHelper.RetryOnExceptionAsync(
                                                           async() =>
            {
                foreach (var task in await source.GetUpdateItems(cancellationToken))
                {
                    // todo move further inside or decouple getting update tasks and actually processing them
                    if (filterByGuids != null && filterByGuids.Length > 0 && !filterByGuids.Contains(task.Info.GUID))
                    {
                        continue;
                    }

                    task.Items.RemoveAll(x => x.UpToDate);
                    results.Add(task);
                }
            },
                                                           3, TimeSpan.FromSeconds(3), cancellationToken)).ToList();

            foreach (var task in concurrentTasks)
            {
                try
                {
                    await task;
                }
                catch (Exception e)
                {
                    Console.WriteLine("[ERROR] Unexpected error while collecting updates from one of the sources, skipping the source. " + e);
                }
            }

            cancellationToken.ThrowIfCancellationRequested();

            var filteredTasks = new List <UpdateTask>();

            foreach (var modGroup in results.GroupBy(x => x.Info.GUID))
            {
                var ordered = modGroup.OrderByDescending(x => x.ModifiedTime ?? DateTime.MinValue).ToList();
                if (ordered.Count > 1)
                {
                    ordered[0].AlternativeSources.AddRange(ordered.Skip(1));
                    Console.WriteLine($"Found {ordered.Count} entries for mod GUID {modGroup.Key} - latest is from {ordered[0].Info.Origin}");
                }
                filteredTasks.Add(ordered[0]);
            }
            return(filteredTasks);
        }
Example #15
0
        protected async Task RemindersParallelUpsert()
        {
            var upserts = await Task.WhenAll(Enumerable.Range(0, 5).Select(i =>
            {
                var reminder = CreateReminder(MakeTestGrainReference(), i.ToString());
                return(Task.WhenAll(Enumerable.Range(1, 5).Select(j =>
                {
                    return RetryHelper.RetryOnExceptionAsync <string>(5, RetryOperation.Sigmoid, async() =>
                    {
                        return await remindersTable.UpsertRow(reminder);
                    });
                })));
            }));

            Assert.DoesNotContain(upserts, i => i.Distinct().Count() != 5);
        }
Example #16
0
        public async Task <HttpResponse> ExecuteAsBinaryAsync(HttpRequest request)
        {
            HttpResponse task = null;

            if (request.HttpMethod == HttpMethod.GET)
            {
                await RetryHelper.RetryOnExceptionAsync(Retries, RetryInterval, async() =>
                {
                    task = await Task.Factory.StartNew(() => ExecuteAsString(request)).ConfigureAwait(false);
                }).ConfigureAwait(false);
            }
            else
            {
                task = await Task.Factory.StartNew(() => ExecuteAsString(request)).ConfigureAwait(false);
            }
            return(task);
        }
        public async Task RetryOnExceptionAsync_WhenFailsTwiceWithAFiveSecondDelay_ReturnsFalseAndCompletesInTenSeconds()
        {
            // Arrange
            var fiveSecondsInMilliseconds = 5000;
            var delay     = TimeSpan.FromMilliseconds(fiveSecondsInMilliseconds);
            var timeWatch = new System.Diagnostics.Stopwatch();

            // Act
            timeWatch.Start();
            var result = await RetryHelper.RetryOnExceptionAsync(2, delay, () => { throw new Exception(); });

            timeWatch.Stop();

            // Assert
            result.Should().BeFalse();
            timeWatch.ElapsedMilliseconds.Should().BeInRange(
                (fiveSecondsInMilliseconds * 2) - 500,
                (fiveSecondsInMilliseconds * 2) + 500);
        }
Example #18
0
        public async Task DownloadNodeAsync(SideloaderUpdateItem task, Progress <double> progress, CancellationToken cancellationToken)
        {
            await Connect();

            await RetryHelper.RetryOnExceptionAsync(async() =>
            {
                task.LocalFile.Delete();
                try
                {
                    await _client.DownloadFileAsync(task.RemoteFile, task.LocalFile.FullName, progress, cancellationToken);
                }
                catch (Exception)
                {
                    // Needed to avoid partially downloaded files causing issues
                    task.LocalFile.Delete();
                    throw;
                }
            }, 2, TimeSpan.FromSeconds(1), cancellationToken);
        }
Example #19
0
    private string FleetId             = "YOUR_FLEET_ID"; // TODO: probably don't hardcode this, use alias or something

    async private void CreatePlayerSession(GameSession gameSession)
    {
        PlayerSession playerSession = null;

        var maxRetryAttempts = 3;
        await RetryHelper.RetryOnExceptionAsync <Exception>
            (maxRetryAttempts, async() =>
        {
            playerSession = await CreatePlayerSessionAsync(gameSession);
        });

        if (playerSession != null)
        {
            // created a player session in there
            Debug.Log("Player session created.");
            Debug.Log($"CLIENT CONNECT INFO: {playerSession.IpAddress}, {playerSession.Port}, {playerSession.PlayerSessionId} ");

            // establish connection with server
            _badNetworkClient.ConnectToServer(playerSession.IpAddress, playerSession.Port, playerSession.PlayerSessionId);
        }
    }
Example #20
0
    async private void setup()
    {
        Debug.Log("setup");

        _badNetworkClient = GameObject.FindObjectOfType <BADNetworkClient>();

        CreateGameLiftClient();

        // Mock game session queries aren't implemented for local GameLift server testing, so just return null to create new one
        GameSession gameSession = IsArgFlagPresent(IsProdArg) ? await SearchGameSessionsAsync() : null;

        if (gameSession == null)
        {
            // create one game session
            var maxRetryAttempts = 3;
            await RetryHelper.RetryOnExceptionAsync <Exception>
                (maxRetryAttempts, async() =>
            {
                gameSession = await CreateGameSessionAsync();
            });

            if (gameSession != null)
            {
                Debug.Log("Game session created.");
                CreatePlayerSession(gameSession);
            }
            else
            {
                Debug.LogWarning("FAILED to create new game session.");
            }
        }
        else
        {
            Debug.Log("Game session found.");

            // game session found, create player session and connect to server
            CreatePlayerSession(gameSession);
        }
    }
Example #21
0
        public async Task <HttpResponseMessage> OrderDodgyDealers(DTO.Order Order)
        {
            List <DTO.OrderProcessProducts> products = new List <DTO.OrderProcessProducts>();

            using (HttpClient client = new HttpClient())
            {
                HttpResponseMessage response = new HttpResponseMessage();
                string returnData            = "";

                #pragma warning disable CS0618 // Type or member is obsolete
                var json = await JsonConvert.SerializeObjectAsync(Order);

                #pragma warning restore CS0618 // Type or member is obsolete

                var httpClient = new HttpClient();
                httpClient.BaseAddress = new Uri(Vendors.DodgyDealers);
                StringContent content = new StringContent(json, Encoding.UTF8, "application/json");

                await RetryHelper.RetryOnExceptionAsync <HttpRequestException>
                    (maxRetryAttempts, pauseBetweenFailures, async() => {
                    response = await httpClient.PostAsync("api/Order", content);
                    //response.EnsureSuccessStatusCode();
                    returnData = await response.Content.ReadAsStringAsync();
                });

                if (!response.IsSuccessStatusCode)
                {
                    return(new HttpResponseMessage(HttpStatusCode.BadRequest)
                    {
                        Content = new StringContent(returnData, System.Text.Encoding.UTF8, "application/json")
                    });
                }

                return(new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new StringContent(returnData, System.Text.Encoding.UTF8, "application/json")
                });
            }
        }
Example #22
0
 private async Task Connect(bool askToLogin)
 {
     await RetryHelper.RetryOnExceptionAsync(async() => await ConnectImpl(askToLogin), 2, TimeSpan.FromSeconds(2), CancellationToken.None);
 }
Example #23
0
        public static async Task <List <UpdateTask> > GetUpdates(CancellationToken cancellationToken, UpdateSourceBase[] updateSources, string[] filterByGuids = null)
        {
            Console.WriteLine("Starting update search...");
            var results = new ConcurrentBag <UpdateTask>();

            var ignoreListPath = "ignorelist.txt";
            var ignoreList     = File.Exists(ignoreListPath) ? File.ReadAllLines(ignoreListPath) : new string[0];

            var anySuccessful = false;

            Exception criticalException = null;

            // First start all of the sources, then wait until they all finish
            var concurrentTasks = updateSources.Select(source => new
            {
                task = RetryHelper.RetryOnExceptionAsync(
                    async() =>
                {
                    try
                    {
                        foreach (var task in await source.GetUpdateItems(cancellationToken))
                        {
                            anySuccessful = true;

                            if (cancellationToken.IsCancellationRequested || criticalException != null)
                            {
                                break;
                            }

                            // todo move further inside or decouple getting update tasks and actually processing them
                            if (filterByGuids != null && filterByGuids.Length > 0 &&
                                !filterByGuids.Contains(task.Info.GUID))
                            {
                                continue;
                            }

                            task.Items.RemoveAll(x =>
                                                 x.UpToDate || (x.RemoteFile != null && ignoreList.Any(x.RemoteFile.Name.Contains)));
                            results.Add(task);
                        }
                    }
                    catch (OutdatedVersionException ex)
                    {
                        criticalException = ex;
                    }
                },
                    3, TimeSpan.FromSeconds(3), cancellationToken),
                source
            }).ToList();

            foreach (var task in concurrentTasks)
            {
                try
                {
                    await task.task;
                }
                catch (OperationCanceledException)
                {
                }
                catch (Exception e)
                {
                    Console.WriteLine($"[ERROR] Unexpected error while collecting updates from source {task.source.Origin} - skipping the source. Error: {e.ToStringDemystified()}");
                }
            }

            cancellationToken.ThrowIfCancellationRequested();

            if (criticalException != null)
            {
                throw criticalException;
            }

            if (!anySuccessful)
            {
                throw new InvalidDataException("No valid update sources were found. Either the online update source list could not be accessed, your UpdateSources file is corrupted or in an old format, or KK Manager is outdated. Make sure that you are connected to the internet and not behind a firewall (try using a VPN) and check for KK Manager updates.");
            }

            var filteredTasks = new List <UpdateTask>();

            foreach (var modGroup in results.GroupBy(x => x.Info.GUID))
            {
                var ordered = modGroup.OrderByDescending(x => x.ModifiedTime ?? DateTime.MinValue).ThenByDescending(x => x.Info.Source.DiscoveryPriority).ToList();
                if (ordered.Count > 1)
                {
                    ordered[0].AlternativeSources.AddRange(ordered.Skip(1));
                    Console.WriteLine($"Found {ordered.Count} sources for mod GUID {modGroup.Key} - choosing {ordered[0].Info.Source.Origin} as latest");
                }
                filteredTasks.Add(ordered[0]);
            }

            Console.WriteLine($"Update search finished. Found {filteredTasks.Count} update tasks.");
            return(filteredTasks);
        }
Example #24
0
        public static async Task <List <UpdateTask> > GetUpdates(CancellationToken cancellationToken, UpdateSourceBase[] updateSources, string[] filterByGuids = null)
        {
            Console.WriteLine("Starting update search...");
            var results = new ConcurrentBag <UpdateTask>();

            var ignoreListPath = "ignorelist.txt";
            var ignoreList     = File.Exists(ignoreListPath) ? File.ReadAllLines(ignoreListPath) : new string[0];

            // First start all of the sources, then wait until they all finish
            var concurrentTasks = updateSources.Select(source => new
            {
                task = RetryHelper.RetryOnExceptionAsync(
                    async() =>
                {
                    foreach (var task in await source.GetUpdateItems(cancellationToken))
                    {
                        // todo move further inside or decouple getting update tasks and actually processing them
                        if (filterByGuids != null && filterByGuids.Length > 0 && !filterByGuids.Contains(task.Info.GUID))
                        {
                            continue;
                        }

                        task.Items.RemoveAll(x => x.UpToDate || (x.RemoteFile != null && ignoreList.Any(x.RemoteFile.Name.Contains)));
                        results.Add(task);
                    }
                },
                    3, TimeSpan.FromSeconds(3), cancellationToken),
                source
            }).ToList();

            foreach (var task in concurrentTasks)
            {
                try
                {
                    await task.task;
                }
                catch (OperationCanceledException)
                {
                }
                catch (Exception e)
                {
                    Console.WriteLine($"[ERROR] Unexpected error while collecting updates from source {task.source.Origin} - skipping the source. Error: {e.ToStringDemystified()}");
                }
            }

            cancellationToken.ThrowIfCancellationRequested();

            var filteredTasks = new List <UpdateTask>();

            foreach (var modGroup in results.GroupBy(x => x.Info.GUID))
            {
                var ordered = modGroup.OrderByDescending(x => x.Info.Source.DiscoveryPriority).ThenByDescending(x => x.ModifiedTime ?? DateTime.MinValue).ToList();
                if (ordered.Count > 1)
                {
                    ordered[0].AlternativeSources.AddRange(ordered.Skip(1));
                    Console.WriteLine($"Found {ordered.Count} sources for mod GUID {modGroup.Key} - choosing {ordered[0].Info.Source.Origin} as latest");
                }
                filteredTasks.Add(ordered[0]);
            }

            Console.WriteLine($"Update search finished. Found {filteredTasks.Count} update tasks.");
            return(filteredTasks);
        }
Example #25
0
        public static async Task <List <ServerSideEvent> > GetEvents(string eventLogPath)
        {
            var events = new List <ServerSideEvent>();

            if (!File.Exists(eventLogPath))
            {
                return(events);
            }

            InitializeKnownResourceTypes();

            string fileText = string.Empty;
            await RetryHelper.RetryOnExceptionAsync <IOException>(10, TimeSpan.FromSeconds(1), async() =>
            {
                fileText = await FileSystemHelpers.ReadAllTextFromFileAsync(eventLogPath);
            });

            if (string.IsNullOrWhiteSpace(fileText))
            {
                return(events);
            }

            XmlDocument dom = new XmlDocument();

            dom.LoadXml(fileText);

            XmlNodeList xmlList = dom.SelectNodes("/Events/Event");

            for (int i = (xmlList.Count - 1); i >= 0; i--)
            {
                var     serverSideEvent = new ServerSideEvent();
                XmlNode eventNode       = xmlList[i];
                var     systemNode      = eventNode.SelectSingleNode("System");
                var     eventDataNode   = eventNode.SelectSingleNode("EventData");

                string strProvider = systemNode["Provider"].GetAttribute("Name");
                serverSideEvent.Source = strProvider;
                string dateTimeString = systemNode["TimeCreated"].GetAttribute("SystemTime");

                bool booValidDateFound = false;

                if (dateTimeString.Contains("T") && dateTimeString.Contains("Z"))
                {
                    if (DateTime.TryParse(dateTimeString, out DateTime resultDateTime))
                    {
                        serverSideEvent.DateAndTime = resultDateTime;
                        booValidDateFound           = true;
                    }
                    else
                    {
                        if (DateTime.TryParse(systemNode["TimeCreated"].GetAttribute("SystemTime"), out resultDateTime))
                        {
                            serverSideEvent.DateAndTime = resultDateTime;
                            booValidDateFound           = true;
                        }
                    }
                }
                else
                {
                    if (DateTime.TryParse(systemNode["TimeCreated"].GetAttribute("SystemTime"), out DateTime resultDateTime))
                    {
                        serverSideEvent.DateAndTime = resultDateTime;
                        booValidDateFound           = true;
                    }
                }

                serverSideEvent.EventID       = systemNode["EventID"].InnerText;
                serverSideEvent.TaskCategory  = systemNode["Task"].InnerText;
                serverSideEvent.EventRecordID = systemNode["EventRecordID"].InnerText;
                serverSideEvent.Computer      = systemNode["Computer"].InnerText;

                List <string> arrayOfdata = new List <string>();

                foreach (XmlNode datanode in eventDataNode.ChildNodes)
                {
                    arrayOfdata.Add(datanode.InnerText);
                }

                string[] args    = arrayOfdata.ToArray();
                int      eventId = Convert.ToInt32(systemNode["EventID"].InnerText);

                string strLevel = systemNode["Level"].InnerText;

                int intLevel = -1;
                int.TryParse(strLevel, out intLevel);


                if (strProvider.StartsWith("ASP.NET"))
                {
                    int level = MapEventTypeToWebEngineEventType(intLevel);
                    var aspnetResourceType = ResourceTypes.Values.Where(x => x.SourceStartsWith.StartsWith("ASP.NET"));
                    if (aspnetResourceType != null)
                    {
                        serverSideEvent.Description = GetDescriptionForEvent(aspnetResourceType.FirstOrDefault().PtrHandle, eventId, intLevel, eventDataNode, args);
                    }
                    if (!booValidDateFound)
                    {
                        if (ExtractDateTimeFromFormattedEvent(serverSideEvent.Description, out DateTime parsedDateTime))
                        {
                            serverSideEvent.DateAndTime = parsedDateTime;
                        }
                        else
                        {
                            if (DateTime.TryParse(systemNode["TimeCreated"].GetAttribute("SystemTime"), out DateTime resultDateTime))
                            {
                                serverSideEvent.DateAndTime = resultDateTime;
                            }
                        }
                    }
                }
                else
                {
                    foreach (var item in ResourceTypes)
                    {
                        if (strProvider.StartsWith(item.Value.SourceStartsWith))
                        {
                            serverSideEvent.Description  = GetDescriptionForEvent(item.Value.PtrHandle, eventId, intLevel, eventDataNode, args);
                            serverSideEvent.Description += Environment.NewLine + string.Join(Environment.NewLine, args);
                            break;
                        }
                    }
                }

                if (string.IsNullOrWhiteSpace(serverSideEvent.Description))
                {
                    serverSideEvent.Description = string.Join(Environment.NewLine, args);
                }

                serverSideEvent.Level = systemNode["Level"].InnerText;
                events.Add(serverSideEvent);
            }



            return(events);
        }
Example #26
0
        public async Task <ActionResult> UploadCsv(int userId, [FromForm] FileDescriptionDto fileDescriptionDto)
        {
            List <AnonymousUser> anonymousUsers = new List <AnonymousUser>();
            var  serviceDbContext = HttpContext.RequestServices.GetRequiredService <ApplicationDbContext>();
            long fileSize         = 0;

            using (var transaction = serviceDbContext.Database.BeginTransaction())
            {
                try
                {
                    using (var fileStream = fileDescriptionDto.File.OpenReadStream())
                        using (var reader = new StreamReader(fileStream, Encoding.Default))
                            using (var csv = new CsvReader(reader, System.Globalization.CultureInfo.CurrentCulture))
                            {
                                //getting filename and contentType
                                var fileName = ContentDispositionHeaderValue.Parse(fileDescriptionDto.File?.ContentDisposition).FileName.ToString().Trim('"');
                                fileDescriptionDto.FileName    = fileDescriptionDto.Description = fileName;
                                fileDescriptionDto.ContentType = fileDescriptionDto.File?.ContentType;
                                fileSize = fileDescriptionDto.File.Length;
                                //mapping csv file to class table schema
                                csv.Configuration.RegisterClassMap <UserDtoCsvMap>();
                                var records = csv.GetRecords <AnonymousUser>().ToList();

                                //adding import file
                                var userFromRepo = await _repository.GetUser(userId);

                                var importFileMap = _mapper.Map <ImportFileDescription>(fileDescriptionDto);
                                userFromRepo.ImportFileDescriptions.Add(importFileMap);

                                if (await _repository.SaveAll())
                                {
                                    //import file processing...
                                    var importedFiledescFromRepo = await _repository.GetImportedFileDescription(importFileMap.Id);

                                    foreach (var record in records)
                                    {
                                        record.ImportFileDescription = importedFiledescFromRepo;
                                    }
                                    anonymousUsers = records;
                                    _repository.AddFileRange <AnonymousUser>(records, fileSize);
                                    if (await _repository.SaveAll())
                                    {
                                        var fileForReturn = _mapper.Map <FileDescriptionForResultDto>(importedFiledescFromRepo);
                                        await transaction.CommitAsync();

                                        return(CreatedAtRoute("GetFileDesc", new { userId = importedFiledescFromRepo.UserId, id = importedFiledescFromRepo.Id }, fileForReturn));
                                    }
                                }
                            }
                }
                catch (Exception ex) when(ex.Message.Equals("The operation has timed out."))
                {
                    // [To Do]implement retry mechanism if fails to send the stream
                    var retryResult = await RetryHelper.RetryOnExceptionAsync(SD.Retry, TimeSpan.FromSeconds(10), async() =>
                    {
                        return(await SaveRetry(anonymousUsers));
                    });

                    if (retryResult)
                    {
                        await transaction.CommitAsync();

                        return(Ok("data has been imported via retry mechanism"));
                    }

                    await transaction.RollbackAsync();

                    return(BadRequest(ex.Message));
                }
                await transaction.RollbackAsync();
            }

            return(BadRequest("Error occured while dumping the data into data base"));
        }
Example #27
0
        /// <exception cref="IOException">Failed to apply the update.</exception>
        public async Task Update(Progress <double> progressCallback, CancellationToken cancellationToken)
        {
            var downloadTarget = await GetTempDownloadFilename();

            // Need to store the filename because MoveTo changes it to the new filename
            var downloadFilename = downloadTarget.FullName;

            if (RemoteFile != null)
            {
                Console.WriteLine($"Attempting download of {TargetPath.Name} from source {RemoteFile.Source.Origin}");
                await RetryHelper.RetryOnExceptionAsync(async() => await RemoteFile.Download(downloadTarget, progressCallback, cancellationToken), 2, TimeSpan.FromSeconds(10), cancellationToken);

                downloadTarget.Refresh();
                if (!downloadTarget.Exists || downloadTarget.Length != RemoteFile.ItemSize)
                {
                    throw new IOException($"Failed to download the update file {RemoteFile.Name} - the downloaded file doesn't exist or is corrupted");
                }

                Console.WriteLine($"Downloaded {downloadTarget.Length} bytes successfully");
            }

retryDelete:
            try
            {
                Directory.CreateDirectory(Path.GetDirectoryName(TargetPath.FullName));
                try
                {
                    if (TargetPath.Exists)
                    {
                        Console.WriteLine($"Deleting old file {TargetPath.FullName}");
                        // Prevent issues removing readonly files
                        TargetPath.Attributes = FileAttributes.Normal;
                        TargetPath.Delete();
                        // Make sure the file gets deleted before continuing
                        await Task.Delay(200, cancellationToken);
                    }

                    if (RemoteFile != null)
                    {
                        downloadTarget.MoveTo(TargetPath.FullName);
                    }
                }
                catch (IOException)
                {
                    if (RemoteFile != null)
                    {
                        await Task.Delay(1000, cancellationToken);

                        downloadTarget.Replace(TargetPath.FullName, TargetPath.FullName + ".old", true);
                        await Task.Delay(1000, cancellationToken);

                        File.Delete(TargetPath.FullName + ".old");
                    }
                    else
                    {
                        throw;
                    }
                }
            }
            catch (IOException ex)
            {
                if (await ProcessWaiter.CheckForProcessesBlockingKoiDir() != true)
                {
                    throw RetryHelper.DoNotAttemptToRetry(new IOException($"Failed to apply update {TargetPath.FullName} because of an IO issue - {ex.Message}", ex));
                }

                goto retryDelete;
            }
            catch (SecurityException ex)
            {
                if (MessageBox.Show($"Failed to apply update {TargetPath.FullName} because of a security issue - {ex.Message}\n\nDo you want KK Manager to attempt to fix the issue? Click cancel if you want to abort.", "Could not apply update", MessageBoxButtons.OKCancel, MessageBoxIcon.Error) != DialogResult.OK)
                {
                    throw;
                }

                var fixPermissions = ProcessTools.FixPermissions(InstallDirectoryHelper.GameDirectory.FullName);
                if (fixPermissions == null)
                {
                    throw RetryHelper.DoNotAttemptToRetry(new IOException($"Failed to create file in directory {TargetPath.FullName} because of a security issue - {ex.Message}", ex));
                }
                fixPermissions.WaitForExit();
                goto retryDelete;
            }
            finally
            {
                try { File.Delete(downloadFilename); }
                catch (SystemException ex) { Console.WriteLine(ex); }
            }
        }
        /// <summary>
        /// Thead handling a single server.
        /// It looks for updates that can be downloaded from that server and picks what it can download.
        /// When no more work is available the task finishes.
        /// </summary>
        private async Task UpdateThread(DownloadSourceInfo updateSource, CancellationToken cancellationToken)
        {
            Exception failReason = null;

            try
            {
                // Exit early if the source keeps failing
                var failCount = 0;
                while (!cancellationToken.IsCancellationRequested)
                {
                    UpdateDownloadItem currentDownloadItem  = null;
                    UpdateItem         currentlyDownloading = null;
                    lock (_updateItems)
                    {
                        currentDownloadItem = _updateItems.FirstOrDefault(x =>
                                                                          x.Status == UpdateDownloadStatus.Waiting &&
                                                                          x.DownloadSources.ContainsKey(updateSource.Source));
                        if (currentDownloadItem != null)
                        {
                            currentDownloadItem.Status = UpdateDownloadStatus.Downloading;
                            currentlyDownloading       = currentDownloadItem.DownloadSources[updateSource.Source];
                        }
                    }

                    if (currentlyDownloading == null || cancellationToken.IsCancellationRequested)
                    {
                        Console.WriteLine($"Closing source downloader {updateSource.Source.Origin}");
                        return;
                    }

                    var progress = new Progress <double>(percent => currentDownloadItem.FinishPercent = percent);

                    try
                    {
                        currentDownloadItem.FinishPercent = 0;

                        await RetryHelper.RetryOnExceptionAsync(
                            () => currentlyDownloading.Update(progress, cancellationToken), 3, TimeSpan.FromSeconds(3),
                            cancellationToken);

                        currentDownloadItem.FinishPercent = 100;
                        currentDownloadItem.Status        = UpdateDownloadStatus.Finished;

                        failCount = 0;
                    }
                    catch (Exception e)
                    {
                        if (e is OperationCanceledException)
                        {
                            currentDownloadItem.MarkAsCancelled(e);

                            if (cancellationToken.IsCancellationRequested)
                            {
                                return;
                            }
                            else
                            {
                                continue;
                            }
                        }

                        Console.WriteLine(
                            $"Marking source {updateSource.Source.Origin} as broken because of exception: {e.ToStringDemystified()}");

                        lock (_updateItems)
                        {
                            currentDownloadItem.TryMarkSourceAsFailed(updateSource.Source, e);
                            currentDownloadItem.FinishPercent = 0;
                        }

                        // Give the source a couple of chances before ditching it
                        if (++failCount >= 2)
                        {
                            failReason = e;
                            return;
                        }
                    }
                }
            }
            finally
            {
                if (failReason != null)
                {
                    lock (_updateItems)
                    {
                        var e = new DownloadSourceCrashedException("Update source " + updateSource.Source.Origin + " closed early because of other issues", updateSource.Source, failReason);
                        foreach (var updateTask in _updateItems)
                        {
                            updateTask.TryMarkSourceAsFailed(updateSource.Source, e);
                        }
                    }
                }
            }
        }
        private async Task <bool> UpdateSingleItem(IGrouping <string, Tuple <UpdateInfo, UpdateItem> > task)
        {
            var firstItem = task.First().Item2;
            var itemSize  = firstItem.GetDownloadSize();

            var lastTimestamp        = DateTime.UtcNow;
            var lastDownloadedKBytes = 0l;

            var progress = new Progress <double>(thisPercent =>
            {
                var timeNow = DateTime.UtcNow;
                var secondsSinceLastUpdate = (timeNow - lastTimestamp).TotalSeconds;

                if (secondsSinceLastUpdate < 1 && thisPercent < 100)
                {
                    return;
                }

                //This item: 70% done (1MB / 20MB)
                //Overall: 50% done (111MB / 1221MB)
                //Speed: 1234KB/s (average 1111KB/s)

                var downloadedKBytes    = (long)(itemSize.GetRawSize() * (thisPercent / 100d));
                var downloadedSize      = FileSize.FromKilobytes(downloadedKBytes);
                var totalDownloadedSize = _completedSize + downloadedSize;
                var totalPercent        = ((double)totalDownloadedSize.GetRawSize() / (double)_overallSize.GetRawSize()) * 100d;

                var speed = (downloadedKBytes - lastDownloadedKBytes) / secondsSinceLastUpdate;
                if (double.IsNaN(speed))
                {
                    speed = 0;
                }
                lastDownloadedKBytes = downloadedKBytes;
                lastTimestamp        = timeNow;

                labelPercent.Text =
                    $@"This item: {thisPercent:F1}% done ({downloadedSize} / {itemSize})
Overall: {totalPercent:F1}% done ({totalDownloadedSize} / {_overallSize})
Speed: {speed:F1}KB/s";

                progressBar1.Value = Math.Min((int)(totalPercent * 10), progressBar1.Maximum);
            });

            SetStatus($"Updating {firstItem.TargetPath.Name}");
            SetStatus($"Updating {InstallDirectoryHelper.GetRelativePath(firstItem.TargetPath)}", false, true);

            var sourcesToAttempt = task.Where(x => !_badUpdateSources.Contains(x.Item1)).OrderBy(x => GetPing(x.Item1)).ToList();

            if (sourcesToAttempt.Count == 0)
            {
                Console.WriteLine("There are no working sources to download from. Check the log for reasons why the sources failed.");

                _failedItems.Add(task);
                return(false);
            }

            Exception ex = null;

            foreach (var source in sourcesToAttempt)
            {
                try
                {
                    // Needed because ZipUpdater doesn't support progress
                    if (source.Item2.RemoteFile is ZipUpdater.ArchiveItem)
                    {
                        labelPercent.Text = $"Extracting... Overall progress: {_completedSize} / {_overallSize}.";
                    }

                    await RetryHelper.RetryOnExceptionAsync(() => source.Item2.Update(progress, _cancelToken.Token), 3,
                                                            TimeSpan.FromSeconds(3), _cancelToken.Token);

                    _completedSize += source.Item2.GetDownloadSize();
                    ex              = null;
                    break;
                }
                catch (OperationCanceledException)
                {
                    throw;
                }
                catch (Exception e)
                {
                    Console.WriteLine($"Marking source {source.Item1.Source.Origin} as broken because of exception: {e.ToStringDemystified()}");

                    ex = e;
                    _badUpdateSources.Add(source.Item1);
                }
            }
            // Check if all sources failed
            if (ex != null)
            {
                Console.WriteLine("There are no working sources to download from. Check the log for reasons why the sources failed.");

                _failedItems.Add(task);
                _failedExceptions.Add(ex);
                return(false);
            }

            return(true);
        }
 /// <summary>
 ///
 /// </summary>
 /// <typeparam name="TException">e.g. HttpRequestException</typeparam>
 /// <param name="operation"></param>
 /// <param name="maxRetryAttempts"></param>
 /// <returns></returns>
 public async Task PerformRetry <TException>(Func <Task> operation, int maxRetryAttempts = 3) where TException : Exception
 {
     var pauseBetweenFailures = TimeSpan.FromSeconds(2);
     await RetryHelper.RetryOnExceptionAsync <TException>(maxRetryAttempts, pauseBetweenFailures, operation);
 }