public static IList <Tuple <ObjectInfo, Event> > GetEventBatch(this IObjectStorage storage, string queueName, IJsonSerializer serializer, int batchSize = 50, DateTime?maxCreatedDate = null) { var events = new List <Tuple <ObjectInfo, Event> >(); lock (_lockObject) { foreach (var file in storage.GetQueueFiles(queueName, batchSize * 5, maxCreatedDate)) { if (!storage.LockFile(file)) { continue; } try { storage.IncrementAttempts(file); } catch {} try { var ev = storage.GetObject <Event>(file.Path); events.Add(Tuple.Create(file, ev)); if (events.Count == batchSize) { break; } } catch {} } return(events); } }
public void CanManageQueue() { Reset(); IObjectStorage storage = GetStorage(); const string queueName = "test"; IJsonSerializer serializer = new DefaultJsonSerializer(); var ev = new Event { Type = Event.KnownTypes.Log, Message = "test" }; storage.Enqueue(queueName, ev); storage.SaveObject("test.txt", "test"); Assert.True(storage.GetObjectList().Any(f => f.Path.StartsWith(queueName + "\\q\\") && f.Path.EndsWith("0.json"))); Assert.Equal(2, storage.GetObjectList().Count()); Assert.True(storage.GetQueueFiles(queueName).All(f => f.Path.EndsWith("0.json"))); Assert.Equal(1, storage.GetQueueFiles(queueName).Count()); storage.DeleteObject("test.txt"); Assert.Equal(1, storage.GetObjectList().Count()); Assert.True(storage.LockFile(storage.GetObjectList().FirstOrDefault())); Assert.True(storage.GetQueueFiles(queueName).All(f => f.Path.EndsWith("0.json.x"))); Assert.True(storage.ReleaseFile(storage.GetObjectList().FirstOrDefault())); var batch = storage.GetEventBatch(queueName, serializer); Assert.Equal(1, batch.Count); Assert.True(storage.GetObjectList().All(f => f.Path.StartsWith(queueName + "\\q\\") && f.Path.EndsWith("1.json.x"))); Assert.Equal(1, storage.GetObjectList().Count()); Assert.Equal(0, storage.GetQueueFiles(queueName).Count()); Assert.Equal(0, storage.GetEventBatch(queueName, serializer).Count()); Assert.False(storage.LockFile(storage.GetObjectList().FirstOrDefault())); storage.ReleaseBatch(batch); Assert.True(storage.GetObjectList().All(f => f.Path.StartsWith(queueName + "\\q\\") && f.Path.EndsWith("1.json"))); Assert.Equal(1, storage.GetObjectList().Count()); Assert.Equal(1, storage.GetQueueFiles(queueName).Count()); var file = storage.GetObjectList().FirstOrDefault(); storage.IncrementAttempts(file); Assert.True(storage.GetObjectList().All(f => f.Path.StartsWith(queueName + "\\q\\") && f.Path.EndsWith("2.json"))); storage.IncrementAttempts(file); Assert.True(storage.GetObjectList().All(f => f.Path.StartsWith(queueName + "\\q\\") && f.Path.EndsWith("3.json"))); Assert.True(storage.LockFile(file)); Assert.NotNull(file); Assert.True(storage.GetObjectList().All(f => f.Path.StartsWith(queueName + "\\q\\") && f.Path.EndsWith("3.json.x"))); Thread.Sleep(TimeSpan.FromMilliseconds(1)); storage.ReleaseStaleLocks(queueName, TimeSpan.Zero); Assert.True(storage.GetObjectList().All(f => f.Path.StartsWith(queueName + "\\q\\") && f.Path.EndsWith("3.json"))); batch = storage.GetEventBatch(queueName, serializer); Assert.Equal(1, batch.Count); Assert.True(storage.GetObjectList().All(f => f.Path.StartsWith(queueName + "\\q\\") && f.Path.EndsWith("4.json.x"))); storage.DeleteBatch(batch); Assert.Equal(0, storage.GetQueueFiles(queueName).Count()); ev = new Event { Type = Event.KnownTypes.Log, Message = "test" }; storage.Enqueue(queueName, ev); file = storage.GetObjectList().FirstOrDefault(); Assert.NotNull(file); Thread.Sleep(TimeSpan.FromMilliseconds(1)); storage.CleanupQueueFiles(queueName, TimeSpan.Zero); Assert.Equal(0, storage.GetQueueFiles(queueName).Count()); }