Skip to content

Commit 79b1aaf

Browse files
committed
Fix a few peek command issues
1 parent 7321cf1 commit 79b1aaf

File tree

3 files changed

+30
-18
lines changed

3 files changed

+30
-18
lines changed

docs-src/queuing.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,17 +124,17 @@ Queued commands can be viewed in JSON which maybe useful for debugging:
124124
inengine.exe -pInEngine.Core queue:peek --pending --json
125125
```
126126

127-
By default, up to 10 messages will be retrieved, but the number is configurable:
127+
By default, up to the first 10 messages will be retrieved, but the range is configurable:
128128

129129
```bash
130-
inengine.exe -pInEngine.Core queue:peek --pending --limit=100
130+
inengine.exe -pInEngine.Core queue:peek --pending --to=100
131131
```
132132

133-
The a paginated slice of the queue can be retrieved using the offset argument.
133+
A slice of the queue can be retrieved using the from argument.
134134
For example, this queue:peek call retrieves the 100-200 queued commands:
135135

136136
```bash
137-
inengine.exe -pInEngine.Core queue:peek --pending --limit=100 --offset=100
137+
inengine.exe -pInEngine.Core queue:peek --pending --from=100 --to=200
138138
```
139139

140140
## Handling Failed Commands

src/InEngine.Core/Queuing/Clients/FileClient.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public bool Consume()
6262
{
6363
var fileInfo = new DirectoryInfo(PendingQueuePath)
6464
.GetFiles()
65-
.OrderByDescending(x => x.LastWriteTimeUtc)
65+
.OrderBy(x => x.LastWriteTimeUtc)
6666
.FirstOrDefault();
6767
if (fileInfo == null)
6868
return false;
@@ -164,11 +164,18 @@ public List<IMessage> PeekPendingMessages(long from, long to)
164164

165165
public List<IMessage> PeekMessages(string queuePath, long from, long to)
166166
{
167-
return new DirectoryInfo(queuePath)
167+
var maxResults = Convert.ToInt32(to + from);
168+
var files = new DirectoryInfo(queuePath)
168169
.GetFiles()
169-
.ToList()
170-
.GetRange(Convert.ToInt32(from), Convert.ToInt32(to - from))
171-
.Select(x => File.ReadAllText(x.FullName).DeserializeFromJson<IMessage>())
170+
.OrderBy(x => x.LastWriteTimeUtc)
171+
.ToList();
172+
173+
if (files.Count() <= maxResults)
174+
return files.Select(x => File.ReadAllText(x.FullName).DeserializeFromJson<Message>() as IMessage).ToList();
175+
176+
return files
177+
.GetRange(Convert.ToInt32(from), Convert.ToInt32(to))
178+
.Select(x => File.ReadAllText(x.FullName).DeserializeFromJson<Message>() as IMessage)
172179
.ToList();
173180
}
174181
}

src/InEngine.Core/Queuing/Commands/Peek.cs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ namespace InEngine.Core.Queuing.Commands
1111
{
1212
public class Peek : AbstractCommand
1313
{
14-
[Option("offset", DefaultValue = 0, HelpText = "The maximum number of messages to peek.")]
15-
public long Offset { get; set; }
14+
[Option("from", DefaultValue = 0, HelpText = "The first message to peek at (0-indexed).")]
15+
public long From { get; set; }
1616

17-
[Option("limit", DefaultValue = 10, HelpText = "The maximum number of messages to peek.")]
18-
public long Limit { get; set; }
17+
[Option("to", DefaultValue = 10, HelpText = "The last message to peek at.")]
18+
public long To { get; set; }
1919

2020
[Option("json", HelpText = "View the messages as JSON.")]
2121
public bool JsonFormat { get; set; }
@@ -34,19 +34,24 @@ public class Peek : AbstractCommand
3434

3535
public override void Run()
3636
{
37+
if (From < 0)
38+
throw new ArgumentException("--from cannot be negative");
39+
if (To < 0)
40+
throw new ArgumentException("--to cannot be negative");
41+
if (To < From)
42+
throw new ArgumentException("--from cannot be greater than --to");
43+
3744
if (PendingQueue == false && FailedQueue == false && InProgressQueue == false)
3845
throw new CommandFailedException("Must specify at least one queue to peek in. Use -h to see available options.");
3946
var broker = Queue.Make(UseSecondaryQueue);
40-
var from = Offset;
41-
var to = Offset + Limit - 1;
4247
if (PendingQueue) {
43-
PrintMessages(broker.PeekPendingMessages(from, to), "Pending");
48+
PrintMessages(broker.PeekPendingMessages(From, To), "Pending");
4449
}
4550
if (InProgressQueue) {
46-
PrintMessages(broker.PeekInProgressMessages(from, to), "In-progress");
51+
PrintMessages(broker.PeekInProgressMessages(From, To), "In-progress");
4752
}
4853
if (FailedQueue) {
49-
PrintMessages(broker.PeekFailedMessages(from, to), "Failed");
54+
PrintMessages(broker.PeekFailedMessages(From, To), "Failed");
5055
}
5156
}
5257

0 commit comments

Comments
 (0)