@@ -29,14 +29,16 @@ namespace Droog.Beanstalk.Client {
29
29
30
30
// TODO: should query the current and watched tubes at start-up, especially once there are connection pools
31
31
public class BeanstalkClient : IBeanstalkClient , IWatchedTubeClient {
32
+
32
33
public static readonly TimeSpan DefaultConnectTimeout = TimeSpan . FromSeconds ( 10 ) ;
33
34
34
35
private readonly Func < ISocket > _socketFactory ;
36
+ private readonly byte [ ] _buffer = new byte [ 16 * 1024 ] ;
37
+ private readonly TubeCollectionProxy _watchedTubes ;
38
+ private readonly BeanstalkDefaults _defaults = new BeanstalkDefaults ( ) ;
35
39
private ISocket _socket ;
36
40
private bool _isDisposed ;
37
- private readonly byte [ ] _buffer = new byte [ 16 * 1024 ] ;
38
41
private string _currentTube = "default" ;
39
- private readonly TubeCollectionProxy _watchedTubes ;
40
42
41
43
public BeanstalkClient ( IPAddress address , int port )
42
44
: this ( address , port , DefaultConnectTimeout ) {
@@ -127,6 +129,10 @@ public void Connect() {
127
129
VerifyConnection ( ) ;
128
130
}
129
131
132
+ public BeanstalkDefaults Defaults {
133
+ get { return _defaults ; }
134
+ }
135
+
130
136
public PutResponse Put ( uint priority , TimeSpan delay , TimeSpan timeToRun , Stream request , long length ) {
131
137
var response = Exec ( Request . Create ( RequestCommand . Put )
132
138
. AppendArgument ( priority )
@@ -183,39 +189,51 @@ public ReleaseStatus Release(uint jobId, uint priority, TimeSpan delay) {
183
189
}
184
190
185
191
public bool Bury ( uint jobId , uint priority ) {
186
- throw new NotImplementedException ( ) ;
192
+ var response = Exec ( Request . Create ( RequestCommand . Bury )
193
+ . AppendArgument ( jobId )
194
+ . AppendArgument ( priority )
195
+ . ExpectStatuses ( ResponseStatus . Buried | ResponseStatus . NotFound ) ) ;
196
+ return response . Status == ResponseStatus . Buried ;
187
197
}
188
198
189
199
public bool Touch ( uint jobId ) {
190
- throw new NotImplementedException ( ) ;
200
+ var response = Exec ( Request . Create ( RequestCommand . Touch )
201
+ . AppendArgument ( jobId )
202
+ . ExpectStatuses ( ResponseStatus . Touched | ResponseStatus . NotFound ) ) ;
203
+ return response . Status == ResponseStatus . Touched ;
191
204
}
192
205
193
- public PeekResponse Peek ( uint jobId ) {
194
- throw new NotImplementedException ( ) ;
206
+ public Job Peek ( uint jobId ) {
207
+ return Peek ( Request . Create ( RequestCommand . Peek ) . AppendArgument ( jobId ) ) ;
195
208
}
196
209
197
- public PeekResponse PeekReady ( ) {
198
- throw new NotImplementedException ( ) ;
210
+ public Job PeekReady ( ) {
211
+ return Peek ( Request . Create ( RequestCommand . PeekReady ) ) ;
199
212
}
200
213
201
- public PeekResponse PeekDelayed ( ) {
202
- throw new NotImplementedException ( ) ;
214
+ public Job PeekDelayed ( ) {
215
+ return Peek ( Request . Create ( RequestCommand . PeekDelayed ) ) ;
203
216
}
204
217
205
- public PeekResponse PeekBuried ( ) {
206
- throw new NotImplementedException ( ) ;
218
+ public Job PeekBuried ( ) {
219
+ return Peek ( Request . Create ( RequestCommand . PeekBuried ) ) ;
207
220
}
208
221
209
222
public uint Kick ( uint bound ) {
210
- throw new NotImplementedException ( ) ;
223
+ var response = Exec ( Request . Create ( RequestCommand . Touch )
224
+ . AppendArgument ( bound )
225
+ . ExpectStatuses ( ResponseStatus . Kicked ) ) ;
226
+ return uint . Parse ( response . Arguments [ 0 ] ) ;
211
227
}
212
228
213
229
public JobStats GetJobStats ( uint jobId ) {
214
- throw new NotImplementedException ( ) ;
230
+ var response = Exec ( Request . Create ( RequestCommand . StatsJob ) . ExpectStatuses ( ResponseStatus . Ok ) ) ;
231
+ return new JobStats ( MicroYaml . ParseDictionary ( response ) ) ;
215
232
}
216
233
217
234
public TubeStats GetTubeStats ( string tube ) {
218
- throw new NotImplementedException ( ) ;
235
+ var response = Exec ( Request . Create ( RequestCommand . StatsTube ) . ExpectStatuses ( ResponseStatus . Ok ) ) ;
236
+ return new TubeStats ( MicroYaml . ParseDictionary ( response ) ) ;
219
237
}
220
238
221
239
public ServerStats GetServerStats ( ) {
@@ -224,7 +242,8 @@ public ServerStats GetServerStats() {
224
242
}
225
243
226
244
public IEnumerable < string > GetTubes ( ) {
227
- throw new NotImplementedException ( ) ;
245
+ var response = Exec ( Request . Create ( RequestCommand . ListTubes ) . ExpectStatuses ( ResponseStatus . Ok ) ) ;
246
+ return MicroYaml . ParseList ( response ) ;
228
247
}
229
248
230
249
public void Close ( ) {
@@ -253,6 +272,13 @@ IEnumerable<string> IWatchedTubeClient.ListWatchedTubes() {
253
272
return MicroYaml . ParseList ( response ) ;
254
273
}
255
274
275
+ private Job Peek ( Request request ) {
276
+ var response = Exec ( request . ExpectStatuses ( ResponseStatus . Found | ResponseStatus . NotFound ) ) ;
277
+ return response . Status == ResponseStatus . NotFound
278
+ ? null
279
+ : new Job ( uint . Parse ( response . Arguments [ 0 ] ) , response . Data , long . Parse ( response . Arguments [ 1 ] ) ) ;
280
+ }
281
+
256
282
private Response Exec ( Request request ) {
257
283
VerifyConnection ( ) ;
258
284
_socket . SendRequest ( request , _buffer ) ;
0 commit comments