From 514fd4286c70d4e9722d225c85b712e96bfff7c6 Mon Sep 17 00:00:00 2001 From: Martijn Hoekstra Date: Tue, 28 Jan 2020 11:42:37 +0100 Subject: [PATCH 1/2] fix line endings set all line endings to lf include .editorconfig to enforce lf endings .editorconfig also set to OTBS, but files not (yet) updated as the diff for a cosmetic change would be significant --- .editorconfig | 210 ++++++++++++++++-- Hotsapi.Uploader.Common.Test/ManagerTests.cs | 202 ++++++++--------- Hotsapi.Uploader.Common.Test/MockAnalizer.cs | 14 +- Hotsapi.Uploader.Common.Test/MockStorage.cs | 14 +- Hotsapi.Uploader.Common.Test/MockUploader.cs | 28 +-- .../NoNewFilesMonitor.cs | 20 +- 6 files changed, 336 insertions(+), 152 deletions(-) diff --git a/.editorconfig b/.editorconfig index 0b9db06..885714f 100644 --- a/.editorconfig +++ b/.editorconfig @@ -1,13 +1,197 @@ -root = true -[*.cs] -indent_style = space -indent_size = 4 - -;csharp_new_line_before_open_brace = accessors, anonymous_methods, anonymous_types, control_blocks, events, indexers, lambdas, object_collection -csharp_new_line_before_open_brace = types, methods, properties -csharp_new_line_before_else = false -csharp_new_line_before_catch = true -csharp_new_line_before_finally = true -csharp_new_line_before_members_in_object_initializers = true -csharp_new_line_before_members_in_anonymous_types = true -csharp_new_line_within_query_expression_clauses = true \ No newline at end of file +# Remove the line below if you want to inherit .editorconfig settings from higher directories +root = true + +# C# files +[*.cs] + +#### Core EditorConfig Options #### + +# Indentation and spacing +indent_size = 4 +indent_style = space +tab_width = 4 + +# New line preferences +end_of_line = lf +insert_final_newline = false + +#### .NET Coding Conventions #### + +# Organize usings +dotnet_separate_import_directive_groups = false +dotnet_sort_system_directives_first = false + +# this. and Me. preferences +dotnet_style_qualification_for_event = false:warning +dotnet_style_qualification_for_field = false:warning +dotnet_style_qualification_for_method = false:warning +dotnet_style_qualification_for_property = false:warning + +# Language keywords vs BCL types preferences +dotnet_style_predefined_type_for_locals_parameters_members = true:silent +dotnet_style_predefined_type_for_member_access = false:silent + +# Parentheses preferences +dotnet_style_parentheses_in_arithmetic_binary_operators = always_for_clarity:silent +dotnet_style_parentheses_in_other_binary_operators = always_for_clarity:silent +dotnet_style_parentheses_in_other_operators = never_if_unnecessary:silent +dotnet_style_parentheses_in_relational_binary_operators = always_for_clarity:silent + +# Modifier preferences +dotnet_style_require_accessibility_modifiers = for_non_interface_members:silent + +# Expression-level preferences +dotnet_style_coalesce_expression = true:suggestion +dotnet_style_collection_initializer = true:suggestion +dotnet_style_explicit_tuple_names = false:silent +dotnet_style_null_propagation = true:suggestion +dotnet_style_object_initializer = true:suggestion +dotnet_style_prefer_auto_properties = true:silent +dotnet_style_prefer_compound_assignment = true:suggestion +dotnet_style_prefer_conditional_expression_over_assignment = true:suggestion +dotnet_style_prefer_conditional_expression_over_return = true:suggestion +dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion +dotnet_style_prefer_inferred_tuple_names = true:suggestion +dotnet_style_prefer_is_null_check_over_reference_equality_method = true:suggestion + +# Field preferences +dotnet_style_readonly_field = true:suggestion + +# Parameter preferences +dotnet_code_quality_unused_parameters = all:suggestion + +#### C# Coding Conventions #### + +# var preferences +csharp_style_var_elsewhere = true:silent +csharp_style_var_for_built_in_types = true:silent +csharp_style_var_when_type_is_apparent = true:silent + +# Expression-bodied members +csharp_style_expression_bodied_accessors = true:suggestion +csharp_style_expression_bodied_constructors = true:suggestion +csharp_style_expression_bodied_indexers = true:suggestion +csharp_style_expression_bodied_lambdas = true:suggestion +csharp_style_expression_bodied_local_functions = true:suggestion +csharp_style_expression_bodied_methods = true:suggestion +csharp_style_expression_bodied_operators = true:suggestion +csharp_style_expression_bodied_properties = true:suggestion + +# Pattern matching preferences +csharp_style_pattern_matching_over_as_with_null_check = true:suggestion +csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion +csharp_style_prefer_switch_expression = true:suggestion + +# Null-checking preferences +csharp_style_conditional_delegate_call = true:suggestion + +# Modifier preferences +csharp_prefer_static_local_function = true:suggestion +csharp_preferred_modifier_order = public,private,protected,internal,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,volatile,async + +# Code-block preferences +csharp_prefer_braces = when_multiline:silent +csharp_prefer_simple_using_statement = true:suggestion + +# Expression-level preferences +csharp_prefer_simple_default_expression = true:suggestion +csharp_style_deconstructed_variable_declaration = true:suggestion +csharp_style_inlined_variable_declaration = true:warning +csharp_style_pattern_local_over_anonymous_function = true:suggestion +csharp_style_prefer_index_operator = true:suggestion +csharp_style_prefer_range_operator = true:suggestion +csharp_style_throw_expression = true:suggestion +csharp_style_unused_value_assignment_preference = discard_variable:suggestion +csharp_style_unused_value_expression_statement_preference = discard_variable:suggestion + +# 'using' directive preferences +csharp_using_directive_placement = outside_namespace:silent + +#### C# Formatting Rules #### + +# New line preferences +csharp_new_line_before_catch = true +csharp_new_line_before_else = true +csharp_new_line_before_finally = true +csharp_new_line_before_members_in_anonymous_types = true +csharp_new_line_before_members_in_object_initializers = true +csharp_new_line_before_open_brace = none +csharp_new_line_between_query_expression_clauses = true + +# Indentation preferences +csharp_indent_block_contents = true +csharp_indent_braces = false +csharp_indent_case_contents = true +csharp_indent_case_contents_when_block = true +csharp_indent_labels = one_less_than_current +csharp_indent_switch_labels = true + +# Space preferences +csharp_space_after_cast = false +csharp_space_after_colon_in_inheritance_clause = true +csharp_space_after_comma = true +csharp_space_after_dot = false +csharp_space_after_keywords_in_control_flow_statements = true +csharp_space_after_semicolon_in_for_statement = true +csharp_space_around_binary_operators = before_and_after +csharp_space_around_declaration_statements = false +csharp_space_before_colon_in_inheritance_clause = true +csharp_space_before_comma = false +csharp_space_before_dot = false +csharp_space_before_open_square_brackets = false +csharp_space_before_semicolon_in_for_statement = false +csharp_space_between_empty_square_brackets = false +csharp_space_between_method_call_empty_parameter_list_parentheses = false +csharp_space_between_method_call_name_and_opening_parenthesis = false +csharp_space_between_method_call_parameter_list_parentheses = false +csharp_space_between_method_declaration_empty_parameter_list_parentheses = false +csharp_space_between_method_declaration_name_and_open_parenthesis = false +csharp_space_between_method_declaration_parameter_list_parentheses = false +csharp_space_between_parentheses = false +csharp_space_between_square_brackets = false + +# Wrapping preferences +csharp_preserve_single_line_blocks = true +csharp_preserve_single_line_statements = true + +#### Naming styles #### + +# Naming rules + +dotnet_naming_rule.interface_should_be_begins_with_i.severity = suggestion +dotnet_naming_rule.interface_should_be_begins_with_i.symbols = interface +dotnet_naming_rule.interface_should_be_begins_with_i.style = begins_with_i + +dotnet_naming_rule.types_should_be_pascal_case.severity = suggestion +dotnet_naming_rule.types_should_be_pascal_case.symbols = types +dotnet_naming_rule.types_should_be_pascal_case.style = pascal_case + +dotnet_naming_rule.non_field_members_should_be_pascal_case.severity = suggestion +dotnet_naming_rule.non_field_members_should_be_pascal_case.symbols = non_field_members +dotnet_naming_rule.non_field_members_should_be_pascal_case.style = pascal_case + +# Symbol specifications + +dotnet_naming_symbols.interface.applicable_kinds = interface +dotnet_naming_symbols.interface.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected +dotnet_naming_symbols.interface.required_modifiers = + +dotnet_naming_symbols.types.applicable_kinds = class, struct, interface, enum +dotnet_naming_symbols.types.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected +dotnet_naming_symbols.types.required_modifiers = + +dotnet_naming_symbols.non_field_members.applicable_kinds = property, event, method +dotnet_naming_symbols.non_field_members.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected +dotnet_naming_symbols.non_field_members.required_modifiers = + +# Naming styles + +dotnet_naming_style.pascal_case.required_prefix = +dotnet_naming_style.pascal_case.required_suffix = +dotnet_naming_style.pascal_case.word_separator = +dotnet_naming_style.pascal_case.capitalization = pascal_case + +dotnet_naming_style.begins_with_i.required_prefix = I +dotnet_naming_style.begins_with_i.required_suffix = +dotnet_naming_style.begins_with_i.word_separator = +dotnet_naming_style.begins_with_i.capitalization = pascal_case diff --git a/Hotsapi.Uploader.Common.Test/ManagerTests.cs b/Hotsapi.Uploader.Common.Test/ManagerTests.cs index ff33583..f145eee 100644 --- a/Hotsapi.Uploader.Common.Test/ManagerTests.cs +++ b/Hotsapi.Uploader.Common.Test/ManagerTests.cs @@ -7,103 +7,103 @@ namespace Hotsapi.Uploader.Common.Test { [TestClass] public partial class ManagerTests - { - private Task ShortRandomDelay() - { - var r = new Random(); - var delay = r.Next(100, 200); - return Task.Delay(delay); - } - private static IEnumerable ThreeInOrder - { - get { - var one = new ReplayFile("one") { - Created = new DateTime(2020, 1, 1, 0, 0, 1) - }; - var two = new ReplayFile("two") { - Created = new DateTime(2020, 1, 1, 0, 0, 10) - }; - var three = new ReplayFile("three") { - Created = new DateTime(2020, 1, 1, 0, 0, 20) - }; - var initialFiles = new List() { one, two, three }; - return initialFiles; - } + { + private Task ShortRandomDelay() + { + var r = new Random(); + var delay = r.Next(100, 200); + return Task.Delay(delay); + } + private static IEnumerable ThreeInOrder + { + get { + var one = new ReplayFile("one") { + Created = new DateTime(2020, 1, 1, 0, 0, 1) + }; + var two = new ReplayFile("two") { + Created = new DateTime(2020, 1, 1, 0, 0, 10) + }; + var three = new ReplayFile("three") { + Created = new DateTime(2020, 1, 1, 0, 0, 20) + }; + var initialFiles = new List() { one, two, three }; + return initialFiles; + } } [TestMethod] [Ignore("Known intermittant failure: multiple uploads are started in parallel and don't always start in order")] - public async Task InitialFilesStartInOrder() - { - var initialFiles = ThreeInOrder; - + public async Task InitialFilesStartInOrder() + { + var initialFiles = ThreeInOrder; + var manager = new Manager(new MockStorage(initialFiles)); - var uploadTester = new MockUploader(); - - var promise = new TaskCompletionSource(); - Task done = promise.Task; - - var uploadsSeen = 0; - var l = new object(); - ReplayFile lastUploadStarted = null; - uploadTester.SetUploadCallback(async rf => { - if (lastUploadStarted != null) { - try { - Assert.IsTrue(rf.Created >= lastUploadStarted.Created, $"upload started out of order, {lastUploadStarted} started after {rf}"); - } catch (Exception e) { - promise.TrySetException(e); - } - } - lastUploadStarted = rf; - await ShortRandomDelay(); - var isDone = false; - lock (l) { - uploadsSeen++; - isDone = uploadsSeen >= 3; - } - if (isDone) { - promise.TrySetResult(uploadsSeen); - } + var uploadTester = new MockUploader(); + + var promise = new TaskCompletionSource(); + Task done = promise.Task; + + var uploadsSeen = 0; + var l = new object(); + ReplayFile lastUploadStarted = null; + uploadTester.SetUploadCallback(async rf => { + if (lastUploadStarted != null) { + try { + Assert.IsTrue(rf.Created >= lastUploadStarted.Created, $"upload started out of order, {lastUploadStarted} started after {rf}"); + } catch (Exception e) { + promise.TrySetException(e); + } + } + lastUploadStarted = rf; + await ShortRandomDelay(); + var isDone = false; + lock (l) { + uploadsSeen++; + isDone = uploadsSeen >= 3; + } + if (isDone) { + promise.TrySetResult(uploadsSeen); + } }); manager.Start(new NoNewFilesMonitor(), new MockAnalizer(), uploadTester); await done; - } - - - + } + + + [TestMethod] [Ignore("Known intermittant failure: multiple uploads are started in parallel and don't always end in order")] - public async Task InitialFilesEndInorder() { - var initialFiles = ThreeInOrder; - + public async Task InitialFilesEndInorder() { + var initialFiles = ThreeInOrder; + var manager = new Manager(new MockStorage(initialFiles)); - var uploadTester = new MockUploader(); - var promise = new TaskCompletionSource(); - Task done = promise.Task; - - var uploadsSeen = 0; - var l = new object(); - ReplayFile lastUploadFinished = null; - uploadTester.SetUploadCallback(async rf => { - await ShortRandomDelay(); - if (lastUploadFinished != null) { - try { - Assert.IsTrue(rf.Created >= lastUploadFinished.Created, $"upload completed out of order, {lastUploadFinished} completed after {rf}"); - } - catch (Exception e) { - promise.TrySetException(e); - } - } - lastUploadFinished = rf; - var isDone = false; - lock (l) { - uploadsSeen++; - isDone = uploadsSeen >= 3; - } - if (isDone) { - promise.TrySetResult(uploadsSeen); - } + var uploadTester = new MockUploader(); + var promise = new TaskCompletionSource(); + Task done = promise.Task; + + var uploadsSeen = 0; + var l = new object(); + ReplayFile lastUploadFinished = null; + uploadTester.SetUploadCallback(async rf => { + await ShortRandomDelay(); + if (lastUploadFinished != null) { + try { + Assert.IsTrue(rf.Created >= lastUploadFinished.Created, $"upload completed out of order, {lastUploadFinished} completed after {rf}"); + } + catch (Exception e) { + promise.TrySetException(e); + } + } + lastUploadFinished = rf; + var isDone = false; + lock (l) { + uploadsSeen++; + isDone = uploadsSeen >= 3; + } + if (isDone) { + promise.TrySetResult(uploadsSeen); + } }); manager.Start(new NoNewFilesMonitor(), new MockAnalizer(), uploadTester); @@ -112,23 +112,23 @@ public async Task InitialFilesEndInorder() { [TestMethod] public async Task AllInitialFilesProcessed() - { - var initialFiles = ThreeInOrder; - + { + var initialFiles = ThreeInOrder; + var manager = new Manager(new MockStorage(initialFiles)); - var uploadTester = new MockUploader(); - var done = new TaskCompletionSource(); - - var uploadsSeen = 0; - object l = new object(); - uploadTester.SetUploadCallback(async rf => { - await ShortRandomDelay(); - lock (l) { - uploadsSeen++; - if (uploadsSeen >= 3) { - done.SetResult(uploadsSeen); - } - } + var uploadTester = new MockUploader(); + var done = new TaskCompletionSource(); + + var uploadsSeen = 0; + object l = new object(); + uploadTester.SetUploadCallback(async rf => { + await ShortRandomDelay(); + lock (l) { + uploadsSeen++; + if (uploadsSeen >= 3) { + done.SetResult(uploadsSeen); + } + } }); manager.Start(new NoNewFilesMonitor(), new MockAnalizer(), uploadTester); diff --git a/Hotsapi.Uploader.Common.Test/MockAnalizer.cs b/Hotsapi.Uploader.Common.Test/MockAnalizer.cs index 318a4df..61e4571 100644 --- a/Hotsapi.Uploader.Common.Test/MockAnalizer.cs +++ b/Hotsapi.Uploader.Common.Test/MockAnalizer.cs @@ -1,14 +1,14 @@ -using Heroes.ReplayParser; +using Heroes.ReplayParser; namespace Hotsapi.Uploader.Common.Test -{ +{ public partial class ManagerTests { - private class MockAnalizer : IAnalyzer - { - public int MinimumBuild { get; set; } - public Replay Analyze(ReplayFile file) => new Replay(); - public string GetFingerprint(Replay replay) => "dummy fingerprint"; + private class MockAnalizer : IAnalyzer + { + public int MinimumBuild { get; set; } + public Replay Analyze(ReplayFile file) => new Replay(); + public string GetFingerprint(Replay replay) => "dummy fingerprint"; } } } diff --git a/Hotsapi.Uploader.Common.Test/MockStorage.cs b/Hotsapi.Uploader.Common.Test/MockStorage.cs index a7451c2..19d8248 100644 --- a/Hotsapi.Uploader.Common.Test/MockStorage.cs +++ b/Hotsapi.Uploader.Common.Test/MockStorage.cs @@ -1,15 +1,15 @@ using System.Collections.Generic; namespace Hotsapi.Uploader.Common.Test -{ +{ public partial class ManagerTests { - private class MockStorage : IReplayStorage - { - private IEnumerable InitialFiles { get; } - public MockStorage(IEnumerable initialFiles) => InitialFiles = initialFiles; - public IEnumerable Load() => InitialFiles; - public void Save(IEnumerable files) { } + private class MockStorage : IReplayStorage + { + private IEnumerable InitialFiles { get; } + public MockStorage(IEnumerable initialFiles) => InitialFiles = initialFiles; + public IEnumerable Load() => InitialFiles; + public void Save(IEnumerable files) { } } } } diff --git a/Hotsapi.Uploader.Common.Test/MockUploader.cs b/Hotsapi.Uploader.Common.Test/MockUploader.cs index 332e063..55a551f 100644 --- a/Hotsapi.Uploader.Common.Test/MockUploader.cs +++ b/Hotsapi.Uploader.Common.Test/MockUploader.cs @@ -3,7 +3,7 @@ using System.Threading.Tasks; namespace Hotsapi.Uploader.Common.Test -{ +{ public partial class ManagerTests { private class MockUploader : IUploader @@ -11,22 +11,22 @@ private class MockUploader : IUploader public bool UploadToHotslogs { get; set; } private Func UploadCallback = _ => Task.CompletedTask; - public void SetUploadCallback(Func onUpload) - { - var old = UploadCallback; - - UploadCallback = async (ReplayFile file) => { - await old(file); - await onUpload(file); - }; - } + public void SetUploadCallback(Func onUpload) + { + var old = UploadCallback; + + UploadCallback = async (ReplayFile file) => { + await old(file); + await onUpload(file); + }; + } public Task CheckDuplicate(IEnumerable replays) => Task.CompletedTask; public Task GetMinimumBuild() => Task.FromResult(1); - public Task Upload(ReplayFile file) - { - UploadCallback(file); - return Task.CompletedTask; + public Task Upload(ReplayFile file) + { + UploadCallback(file); + return Task.CompletedTask; } public async Task Upload(string file) { diff --git a/Hotsapi.Uploader.Common.Test/NoNewFilesMonitor.cs b/Hotsapi.Uploader.Common.Test/NoNewFilesMonitor.cs index a6a3f32..b2f1084 100644 --- a/Hotsapi.Uploader.Common.Test/NoNewFilesMonitor.cs +++ b/Hotsapi.Uploader.Common.Test/NoNewFilesMonitor.cs @@ -1,18 +1,18 @@ using System; using System.Collections.Generic; -using System.Linq; +using System.Linq; namespace Hotsapi.Uploader.Common.Test -{ +{ public partial class ManagerTests { - private class NoNewFilesMonitor : IMonitor - { - public event EventHandler> ReplayAdded; - - public IEnumerable ScanReplays() => Enumerable.Empty(); - public void Start() { } - public void Stop() { } - } + private class NoNewFilesMonitor : IMonitor + { + public event EventHandler> ReplayAdded; + + public IEnumerable ScanReplays() => Enumerable.Empty(); + public void Start() { } + public void Stop() { } + } } } From 201d2162ef9c78ed0568c6a889bf02ca878c371a Mon Sep 17 00:00:00 2001 From: Martijn Hoekstra Date: Tue, 28 Jan 2020 11:22:36 +0100 Subject: [PATCH 2/2] don't continue while server is 503 --- Hotsapi.Uploader.Common.Test/MockUploader.cs | 3 ++ Hotsapi.Uploader.Common/IUploader.cs | 5 +++- Hotsapi.Uploader.Common/Manager.cs | 31 +++++++++++++++++++- Hotsapi.Uploader.Common/Uploader.cs | 11 ++++++- 4 files changed, 47 insertions(+), 3 deletions(-) diff --git a/Hotsapi.Uploader.Common.Test/MockUploader.cs b/Hotsapi.Uploader.Common.Test/MockUploader.cs index 55a551f..77c119c 100644 --- a/Hotsapi.Uploader.Common.Test/MockUploader.cs +++ b/Hotsapi.Uploader.Common.Test/MockUploader.cs @@ -11,6 +11,9 @@ private class MockUploader : IUploader public bool UploadToHotslogs { get; set; } private Func UploadCallback = _ => Task.CompletedTask; + + public event Action NotifyServerDownUntil; + public void SetUploadCallback(Func onUpload) { var old = UploadCallback; diff --git a/Hotsapi.Uploader.Common/IUploader.cs b/Hotsapi.Uploader.Common/IUploader.cs index b731770..9ab2aeb 100644 --- a/Hotsapi.Uploader.Common/IUploader.cs +++ b/Hotsapi.Uploader.Common/IUploader.cs @@ -1,14 +1,17 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Threading.Tasks; namespace Hotsapi.Uploader.Common { public interface IUploader { + event Action NotifyServerDownUntil; bool UploadToHotslogs { get; set; } Task CheckDuplicate(IEnumerable replays); Task GetMinimumBuild(); Task Upload(ReplayFile file); Task Upload(string file); } + } \ No newline at end of file diff --git a/Hotsapi.Uploader.Common/Manager.cs b/Hotsapi.Uploader.Common/Manager.cs index d994bd2..6f720d5 100644 --- a/Hotsapi.Uploader.Common/Manager.cs +++ b/Hotsapi.Uploader.Common/Manager.cs @@ -101,6 +101,11 @@ public async void Start(IMonitor monitor, IAnalyzer analyzer, IUploader uploader _analyzer = analyzer; _monitor = monitor; + uploader.NotifyServerDownUntil += dt => { + ServerDownUntil = dt; + RefreshStatusAndAggregates(); + }; + var replays = ScanReplays(); Files.AddRange(replays); replays.Where(x => x.UploadStatus == UploadStatus.None).Reverse().Map(x => processingQueue.Add(x)); @@ -126,19 +131,41 @@ public void Stop() processingQueue.CompleteAdding(); } + private DateTime ServerDownUntil { get; set; } + + private async Task WhenUploaderAvailable() + { + var maxDelay = new TimeSpan(0, 10, 0); + var calculated = ServerDownUntil - DateTime.Now; + var delay = calculated > maxDelay ? maxDelay : calculated; + if (delay.TotalMilliseconds >= 0) { + await Task.Delay(delay); + await WhenUploaderAvailable(); + } + } + + private bool IsUploaderAvailable => ServerDownUntil <= DateTime.Now; + private async Task UploadLoop() { while (await processingQueue.OutputAvailableAsync()) { + await WhenUploaderAvailable(); try { var file = await processingQueue.TakeAsync(); + await WhenUploaderAvailable(); file.UploadStatus = UploadStatus.InProgress; // test if replay is eligible for upload (not AI, PTR, Custom, etc) var replay = _analyzer.Analyze(file); + await WhenUploaderAvailable(); if (file.UploadStatus == UploadStatus.InProgress) { // if it is, upload it await _uploader.Upload(file); + await Task.Yield(); + if (!IsUploaderAvailable) { + await processingQueue.AddAsync(file); + } } SaveReplayList(); if (ShouldDelete(file, replay)) { @@ -153,7 +180,9 @@ private async Task UploadLoop() private void RefreshStatusAndAggregates() { - _status = Files.Any(x => x.UploadStatus == UploadStatus.InProgress) ? "Uploading..." : "Idle"; + _status = !IsUploaderAvailable ? "Server unavailable" : + Files.Any(x => x.UploadStatus == UploadStatus.InProgress) ? "Uploading..." : + "Idle"; _aggregates = Files.GroupBy(x => x.UploadStatus).ToDictionary(x => x.Key, x => x.Count()); PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Status))); PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Aggregates))); diff --git a/Hotsapi.Uploader.Common/Uploader.cs b/Hotsapi.Uploader.Common/Uploader.cs index 130f947..068b102 100644 --- a/Hotsapi.Uploader.Common/Uploader.cs +++ b/Hotsapi.Uploader.Common/Uploader.cs @@ -20,6 +20,8 @@ public class Uploader : IUploader #endif public bool UploadToHotslogs { get; set; } + public event Action NotifyServerDownUntil; + public DateTime AssumeServerDownUntil {get; private set;} = DateTime.Now; /// /// New instance of replay uploader @@ -72,7 +74,14 @@ public async Task Upload(string file) } } catch (WebException ex) { - if (await CheckApiThrottling(ex.Response)) { + if(ex.Response is HttpWebResponse httpWebResponse && httpWebResponse.StatusCode == HttpStatusCode.ServiceUnavailable) { + _log.Warn($"Service unavaiable, response is {httpWebResponse}"); + if(AssumeServerDownUntil < DateTime.Now) { + AssumeServerDownUntil = DateTime.Now.AddMinutes(10); + NotifyServerDownUntil(AssumeServerDownUntil); + } + } + else if (await CheckApiThrottling(ex.Response)) { return await Upload(file); } _log.Warn(ex, $"Error uploading file '{file}'");