Skip to content

Commit 1d40876

Browse files
committed
Fixed a bug where folder storage would throw an exception and suspend processing while processing the queue if the queue folder didn't exist.
Also improved storage logging
1 parent 7ac306f commit 1d40876

File tree

8 files changed

+85
-39
lines changed

8 files changed

+85
-39
lines changed

src/Exceptionless/Queue/DefaultEventQueue.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,13 @@ public Task Process() {
6868
Task resultTask;
6969
try {
7070
_log.Trace(typeof(DefaultEventQueue), "Processing queue...");
71-
_storage.CleanupQueueFiles(_config.GetQueueName(), _config.QueueMaxAge, _config.QueueMaxAttempts);
72-
_storage.ReleaseStaleLocks(_config.GetQueueName());
71+
string queueName = _config.GetQueueName();
72+
_storage.CleanupQueueFiles(queueName, _config.QueueMaxAge, _config.QueueMaxAttempts);
73+
_storage.ReleaseStaleLocks(queueName);
7374

7475
DateTime maxCreatedDate = DateTime.Now;
7576
int batchSize = _config.SubmissionBatchSize;
76-
var batch = _storage.GetEventBatch(_config.GetQueueName(), _serializer, batchSize, maxCreatedDate);
77+
var batch = _storage.GetEventBatch(queueName, _serializer, batchSize, maxCreatedDate);
7778
while (batch.Any()) {
7879
bool deleteBatch = true;
7980

@@ -132,7 +133,7 @@ public Task Process() {
132133
if (!deleteBatch || IsQueueProcessingSuspended)
133134
break;
134135

135-
batch = _storage.GetEventBatch(_config.GetQueueName(), _serializer, batchSize, maxCreatedDate);
136+
batch = _storage.GetEventBatch(queueName, _serializer, batchSize, maxCreatedDate);
136137
}
137138
} catch (Exception ex) {
138139
_log.Error(typeof(DefaultEventQueue), ex, String.Concat("An error occurred while processing the queue: ", ex.Message));
@@ -187,11 +188,11 @@ protected virtual void OnEventsPosted(EventsPostedEventArgs e) {
187188
}
188189
}
189190

190-
private bool IsQueueProcessingSuspended {
191+
internal bool IsQueueProcessingSuspended {
191192
get { return _suspendProcessingUntil.HasValue && _suspendProcessingUntil.Value > DateTime.Now; }
192193
}
193194

194-
private bool AreQueuedItemsDiscarded {
195+
internal bool AreQueuedItemsDiscarded {
195196
get { return _discardQueuedItemsUntil.HasValue && _discardQueuedItemsUntil.Value > DateTime.Now; }
196197
}
197198

src/Exceptionless/Storage/FolderObjectStorage.cs

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,14 @@ public FolderObjectStorage(IDependencyResolver resolver, string folder) {
3131
public string Folder { get; set; }
3232

3333
public T GetObject<T>(string path) where T : class {
34-
if (String.IsNullOrWhiteSpace(path))
34+
if (String.IsNullOrEmpty(path))
3535
throw new ArgumentNullException("path");
3636

3737
try {
3838
using (var reader = File.OpenRead(Path.Combine(Folder, path))) {
3939
return _resolver.GetStorageSerializer().Deserialize<T>(reader);
4040
}
41-
}
42-
catch (Exception ex) {
41+
} catch (Exception ex) {
4342
_resolver.GetLog().Error(ex.Message, exception: ex);
4443
return null;
4544
}
@@ -62,14 +61,14 @@ public bool Exists(string path) {
6261
}
6362

6463
public bool SaveObject<T>(string path, T value) where T : class {
65-
if (String.IsNullOrWhiteSpace(path))
64+
if (String.IsNullOrEmpty(path))
6665
throw new ArgumentNullException("path");
6766

68-
string directory = Path.GetDirectoryName(Path.Combine(Folder, path));
69-
if (!Directory.Exists(directory))
70-
Directory.CreateDirectory(directory);
71-
7267
try {
68+
string directory = Path.GetDirectoryName(Path.Combine(Folder, path));
69+
if (!Directory.Exists(directory))
70+
Directory.CreateDirectory(directory);
71+
7372
using (var writer = File.OpenWrite(Path.Combine(Folder, path))) {
7473
_resolver.GetStorageSerializer().Serialize(value, writer);
7574
}
@@ -82,29 +81,31 @@ public bool SaveObject<T>(string path, T value) where T : class {
8281
}
8382

8483
public bool RenameObject(string oldpath, string newpath) {
85-
if (String.IsNullOrWhiteSpace(oldpath))
84+
if (String.IsNullOrEmpty(oldpath))
8685
throw new ArgumentNullException("oldpath");
87-
if (String.IsNullOrWhiteSpace(newpath))
86+
if (String.IsNullOrEmpty(newpath))
8887
throw new ArgumentNullException("newpath");
8988

9089
try {
9190
lock (_lockObject) {
9291
File.Move(Path.Combine(Folder, oldpath), Path.Combine(Folder, newpath));
9392
}
94-
} catch (Exception) {
93+
} catch (Exception ex) {
94+
_resolver.GetLog().Error(ex.Message, exception: ex);
9595
return false;
9696
}
9797

9898
return true;
9999
}
100100

101101
public bool DeleteObject(string path) {
102-
if (String.IsNullOrWhiteSpace(path))
102+
if (String.IsNullOrEmpty(path))
103103
throw new ArgumentNullException("path");
104104

105105
try {
106106
File.Delete(Path.Combine(Folder, path));
107-
} catch (Exception) {
107+
} catch (Exception ex) {
108+
_resolver.GetLog().Error(ex.Message, exception: ex);
108109
return false;
109110
}
110111

@@ -120,19 +121,24 @@ public IEnumerable<ObjectInfo> GetObjectList(string searchPattern = null, int? l
120121

121122
var list = new List<ObjectInfo>();
122123

123-
foreach (var path in Directory.EnumerateFiles(Folder, searchPattern, SearchOption.AllDirectories)) {
124-
var info = new System.IO.FileInfo(path);
125-
if (!info.Exists || info.CreationTime > maxCreatedDate)
126-
continue;
127-
128-
list.Add(new ObjectInfo {
129-
Path = path.Replace(Folder, String.Empty),
130-
Created = info.CreationTime,
131-
Modified = info.LastWriteTime
132-
});
133-
134-
if (list.Count == limit)
135-
break;
124+
try {
125+
foreach (var path in Directory.EnumerateFiles(Folder, searchPattern, SearchOption.AllDirectories)) {
126+
var info = new System.IO.FileInfo(path);
127+
if (!info.Exists || info.CreationTime > maxCreatedDate)
128+
continue;
129+
130+
list.Add(new ObjectInfo {
131+
Path = path.Replace(Folder, String.Empty),
132+
Created = info.CreationTime,
133+
Modified = info.LastWriteTime
134+
});
135+
136+
if (list.Count == limit)
137+
break;
138+
}
139+
} catch (DirectoryNotFoundException) {
140+
} catch (Exception ex) {
141+
_resolver.GetLog().Error(ex.Message, exception: ex);
136142
}
137143

138144
return list;

src/Exceptionless/Storage/IsolatedStorageObjectStorage.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public bool Exists(string path) {
9696
}
9797

9898
public T GetObject<T>(string path) where T : class {
99-
if (String.IsNullOrWhiteSpace(path))
99+
if (String.IsNullOrEmpty(path))
100100
throw new ArgumentNullException("path");
101101

102102
try {
@@ -121,7 +121,7 @@ public T GetObject<T>(string path) where T : class {
121121
}
122122

123123
public bool SaveObject<T>(string path, T value) where T : class {
124-
if (String.IsNullOrWhiteSpace(path))
124+
if (String.IsNullOrEmpty(path))
125125
throw new ArgumentNullException("path");
126126

127127
EnsureDirectory(path);
@@ -155,9 +155,9 @@ public bool SaveObject<T>(string path, T value) where T : class {
155155
}
156156

157157
public bool RenameObject(string oldpath, string newpath) {
158-
if (String.IsNullOrWhiteSpace(oldpath))
158+
if (String.IsNullOrEmpty(oldpath))
159159
throw new ArgumentNullException("oldpath");
160-
if (String.IsNullOrWhiteSpace(newpath))
160+
if (String.IsNullOrEmpty(newpath))
161161
throw new ArgumentNullException("newpath");
162162

163163
try {
@@ -175,7 +175,7 @@ public bool RenameObject(string oldpath, string newpath) {
175175
}
176176

177177
public bool DeleteObject(string path) {
178-
if (String.IsNullOrWhiteSpace(path))
178+
if (String.IsNullOrEmpty(path))
179179
throw new ArgumentNullException("path");
180180

181181
try {

test/Exceptionless.Tests/ExceptionlessClientTests.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Diagnostics;
43
using System.Linq;
5-
using System.Threading;
64
using Exceptionless.Dependency;
75
using Exceptionless.Logging;
86
using Exceptionless.Models;

test/Exceptionless.Tests/Storage/FileStorageTestsBase.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,48 @@
55
using System.Linq;
66
using System.Threading;
77
using System.Threading.Tasks;
8+
using Exceptionless.Dependency;
89
using Exceptionless.Extensions;
10+
using Exceptionless.Logging;
911
using Exceptionless.Models;
12+
using Exceptionless.Queue;
1013
using Exceptionless.Serializer;
1114
using Exceptionless.Storage;
15+
using Exceptionless.Tests.Log;
16+
using Exceptionless.Tests.Utility;
1217
using Xunit;
18+
using Xunit.Abstractions;
1319

1420
namespace Exceptionless.Tests.Storage {
1521
public abstract class FileStorageTestsBase {
22+
private readonly TestOutputWriter _writer;
23+
public FileStorageTestsBase(ITestOutputHelper output) {
24+
_writer = new TestOutputWriter(output);
25+
}
26+
1627
protected abstract IObjectStorage GetStorage();
1728

29+
[Fact]
30+
public void CanProcessQueueWithUninitializedStorage() {
31+
var client = new ExceptionlessClient(c => {
32+
c.UseLogger(new XunitExceptionlessLog(_writer) { MinimumLogLevel = LogLevel.Trace });
33+
c.ReadFromAttributes();
34+
c.Resolver.Register(typeof(IObjectStorage), GetStorage);
35+
c.UserAgent = "testclient/1.0.0.0";
36+
37+
// Disable updating settings.
38+
c.UpdateSettingsWhenIdleInterval = TimeSpan.Zero;
39+
});
40+
41+
client.Startup();
42+
client.ProcessQueue();
43+
var queue = client.Configuration.Resolver.GetEventQueue() as DefaultEventQueue;
44+
Assert.NotNull(queue);
45+
Assert.False(queue.IsQueueProcessingSuspended);
46+
Assert.False(queue.AreQueuedItemsDiscarded);
47+
client.Shutdown();
48+
}
49+
1850
[Fact]
1951
public void CanManageFiles() {
2052
Reset();

test/Exceptionless.Tests/Storage/FolderFileStorageTests.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@
44
using Exceptionless.Dependency;
55
using Exceptionless.Serializer;
66
using Exceptionless.Storage;
7+
using Xunit.Abstractions;
78

89
namespace Exceptionless.Tests.Storage {
910
public class FolderFileStorageTests : FileStorageTestsBase {
1011
private const string DATA_DIRECTORY_QUEUE_FOLDER = @"|DataDirectory|\Queue";
1112

13+
public FolderFileStorageTests(ITestOutputHelper output) : base(output) { }
14+
1215
protected override IObjectStorage GetStorage() {
1316
var resolver = new DefaultDependencyResolver();
1417
resolver.Register<IJsonSerializer, DefaultJsonSerializer>();

test/Exceptionless.Tests/Storage/InMemoryFileStorageTests.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
using System;
22
using Exceptionless.Storage;
3+
using Xunit.Abstractions;
34

45
namespace Exceptionless.Tests.Storage {
56
public class InMemoryFileStorageTests : FileStorageTestsBase {
7+
public InMemoryFileStorageTests(ITestOutputHelper output) : base(output) { }
8+
69
protected override IObjectStorage GetStorage() {
710
return new InMemoryObjectStorage();
811
}

test/Exceptionless.Tests/Storage/IsolatedStorageFileStorageTests.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
using Exceptionless.Dependency;
33
using Exceptionless.Serializer;
44
using Exceptionless.Storage;
5+
using Xunit.Abstractions;
56

67
namespace Exceptionless.Tests.Storage {
78
public class IsolatedStorageFileStorageTests : FileStorageTestsBase {
9+
public IsolatedStorageFileStorageTests(ITestOutputHelper output) : base(output) { }
10+
811
protected override IObjectStorage GetStorage() {
912
var resolver = new DefaultDependencyResolver();
1013
resolver.Register<IJsonSerializer, DefaultJsonSerializer>();

0 commit comments

Comments
 (0)