Skip to content

Commit 4dc7f4d

Browse files
committed
- using TimeSpan where appropriate in API signatures
- implemented Release
1 parent 715b78e commit 4dc7f4d

File tree

5 files changed

+61
-20
lines changed

5 files changed

+61
-20
lines changed

Beanstalk.Client/BeanstalkClient.cs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public string CurrentTube {
114114
}
115115
}
116116

117-
public PutResponse Put(uint priority, uint delay, uint timeToRun, Stream request, long length) {
117+
public PutResponse Put(uint priority, TimeSpan delay, TimeSpan timeToRun, Stream request, long length) {
118118
var response = Exec(Request.Create(RequestCommand.Put)
119119
.AppendArgument(priority)
120120
.AppendArgument(delay)
@@ -142,8 +142,19 @@ public Job Reserve() {
142142
throw new ShouldNeverHappenException();
143143
}
144144

145-
public Job Reserve(uint timeout) {
146-
throw new NotImplementedException();
145+
public Job Reserve(TimeSpan timeout) {
146+
var response = Exec(Request.Create(RequestCommand.Reserve)
147+
.AppendArgument(timeout)
148+
.ExpectStatuses(ResponseStatus.DeadlineSoon | ResponseStatus.TimedOut| ResponseStatus.Reserved));
149+
switch(response.Status) {
150+
case ResponseStatus.Reserved:
151+
return new Job(uint.Parse(response.Arguments[0]), response.Data, long.Parse(response.Arguments[1]));
152+
case ResponseStatus.TimedOut:
153+
throw new TimedoutException();
154+
case ResponseStatus.DeadlineSoon:
155+
throw new DeadlineSoonException();
156+
}
157+
throw new ShouldNeverHappenException();
147158
}
148159

149160
public bool Delete(uint jobId) {
@@ -153,8 +164,13 @@ public bool Delete(uint jobId) {
153164
return response.Status == ResponseStatus.Deleted;
154165
}
155166

156-
public ReleaseStatus Release(uint jobId, uint priority, uint delay) {
157-
throw new NotImplementedException();
167+
public ReleaseStatus Release(uint jobId, uint priority, TimeSpan delay) {
168+
var response = Exec(Request.Create(RequestCommand.Release)
169+
.AppendArgument(jobId)
170+
.AppendArgument(priority)
171+
.AppendArgument(delay)
172+
.ExpectStatuses(ResponseStatus.Released | ResponseStatus.Buried | ResponseStatus.NotFound));
173+
return response.Status.ToReleaseStatus();
158174
}
159175

160176
public bool Bury(uint jobId, uint priority) {

Beanstalk.Client/BeanstalkClientExceptions.cs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@
2121
using Droog.Beanstalk.Client.Protocol;
2222

2323
namespace Droog.Beanstalk.Client {
24-
public abstract class BeanstalkClientExceptions : Exception {
25-
protected BeanstalkClientExceptions() { }
26-
protected BeanstalkClientExceptions(string message) : base(message) { }
27-
protected BeanstalkClientExceptions(string message, Exception exception) : base(message, exception) { }
24+
public abstract class BeanstalkClientException : Exception {
25+
protected BeanstalkClientException() { }
26+
protected BeanstalkClientException(string message) : base(message) { }
27+
protected BeanstalkClientException(string message, Exception exception) : base(message, exception) { }
2828
}
2929

3030
public class EmptyResponseException : ConnectionException { }
31-
public abstract class ConnectionException : BeanstalkClientExceptions {
31+
public abstract class ConnectionException : BeanstalkClientException {
3232
protected ConnectionException() { }
3333
protected ConnectionException(string message) : base(message) { }
3434

@@ -37,24 +37,25 @@ public class ReadException : ConnectionException {
3737
public ReadException(string message) : base(message) { }
3838
}
3939
public class WriteException : ConnectionException { }
40-
public class DeadlineSoonException : BeanstalkClientExceptions { }
41-
public class ShouldNeverHappenException : BeanstalkClientExceptions { }
40+
public class TimedoutException : BeanstalkClientException { }
41+
public class DeadlineSoonException : BeanstalkClientException { }
42+
public class ShouldNeverHappenException : BeanstalkClientException { }
4243

43-
public class ConnectException : BeanstalkClientExceptions {
44+
public class ConnectException : BeanstalkClientException {
4445
public ConnectException(Exception exception)
4546
: base("Unable to Connect to Beanstalk server", exception) {
4647
}
4748
}
4849

49-
public class UnknowResponseException : BeanstalkClientExceptions {
50+
public class UnknowResponseException : BeanstalkClientException {
5051
public readonly string Response;
5152
public UnknowResponseException(string response)
5253
: base(string.Format("Response '{0}' is not supported by this client", response)) {
5354
Response = response;
5455
}
5556
}
5657

57-
public class InvalidStatusException : BeanstalkClientExceptions {
58+
public class InvalidStatusException : BeanstalkClientException {
5859
public readonly RequestCommand Command;
5960
public readonly ResponseStatus Status;
6061

@@ -65,12 +66,20 @@ public InvalidStatusException(RequestCommand command, ResponseStatus status)
6566
}
6667
}
6768

68-
public class PutFailedException : BeanstalkClientExceptions {
69+
public class PutFailedException : BeanstalkClientException {
6970
public readonly ResponseStatus Status;
7071

7172
public PutFailedException(ResponseStatus status)
7273
: base(string.Format("Put failed with response '{0}'", status)) {
7374
Status = status;
7475
}
7576
}
77+
78+
public class InvalidReleaseStatusException : BeanstalkClientException {
79+
public readonly ResponseStatus Status;
80+
public InvalidReleaseStatusException(ResponseStatus status)
81+
: base(string.Format("Unable to convert response status '{0}' to ReleaseStatus", status)) {
82+
Status = status;
83+
}
84+
}
7685
}

Beanstalk.Client/IBeanstalkClient.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ public interface IBeanstalkClient : IDisposable
3535
// Consumer related
3636
string CurrentTube { get; set; }
3737

38-
PutResponse Put(uint priority, uint delay, uint timeToRun, Stream request, long length);
38+
PutResponse Put(uint priority, TimeSpan delay, TimeSpan timeToRun, Stream request, long length);
3939

4040
// Producer related
4141
ICollection<string> WatchedTubes { get; }
4242

4343
Job Reserve();
44-
Job Reserve(uint timeout);
44+
Job Reserve(TimeSpan timeout);
4545
bool Delete(uint jobId);
46-
ReleaseStatus Release(uint jobId, uint priority, uint delay);
46+
ReleaseStatus Release(uint jobId, uint priority, TimeSpan delay);
4747
bool Bury(uint jobId, uint priority);
4848
bool Touch(uint jobId);
4949

Beanstalk.Client/Protocol/Extensions.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public static void SendRequest(this ISocket socket, Request request, byte[] buff
4848
private static void SendBuffer(this ISocket socket, byte[] buffer, int count) {
4949
var offset = 0;
5050
while(count > 0) {
51-
Console.WriteLine(Encoding.ASCII.GetString(buffer,offset,count));
51+
Console.WriteLine(Encoding.ASCII.GetString(buffer, offset, count));
5252
var sent = socket.Send(buffer, offset, count);
5353
offset += sent;
5454
count -= sent;
@@ -126,5 +126,14 @@ public static Response ReceiveResponse(this ISocket socket, byte[] buffer) {
126126
}
127127
return response;
128128
}
129+
130+
public static ReleaseStatus ToReleaseStatus(this ResponseStatus status) {
131+
try {
132+
Enum.Parse(typeof(ReleaseStatus), status.ToString());
133+
} catch {
134+
throw new InvalidReleaseStatusException(status);
135+
}
136+
}
129137
}
138+
130139
}

Beanstalk.Client/Protocol/Request.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,13 @@ public Request AppendArgument(uint arg) {
8282
return this;
8383
}
8484

85+
public Request AppendArgument(TimeSpan arg) {
86+
ThrowIfDone();
87+
_request.WriteByte((byte)' ');
88+
_request.Write(Encoding.ASCII.GetBytes(((uint)arg.TotalSeconds).ToString()));
89+
return this;
90+
}
91+
8592
public Request WithData(Stream data, long dataLength) {
8693
ThrowIfDone();
8794
_data = data;

0 commit comments

Comments
 (0)