Skip to content

Commit 17a2b5b

Browse files
committed
fleshing out stats classes
1 parent bc943f2 commit 17a2b5b

File tree

7 files changed

+161
-3
lines changed

7 files changed

+161
-3
lines changed

Beanstalk.Client.IntegrationTest/BeanstalkClientTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ public void Can_get_stats() {
110110
using(var client = CreateClient()) {
111111
var stats = client.GetServerStats();
112112
Assert.IsNotNull(stats["version"]);
113+
Assert.AreEqual(stats.Version, stats["version"]);
114+
Assert.Greater(stats.Uptime,TimeSpan.Zero);
113115
}
114116
}
115117

Beanstalk.Client/Beanstalk.Client.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
<Compile Include="BeanstalkClientExceptions.cs" />
5050
<Compile Include="BeanstalkDefaults.cs" />
5151
<Compile Include="BeanstalkExtensions.cs" />
52+
<Compile Include="JobState.cs" />
5253
<Compile Include="Net\ConnectionPool.cs" />
5354
<Compile Include="Net\IConnectionPool.cs" />
5455
<Compile Include="Net\ISocket.cs" />
@@ -77,6 +78,7 @@
7778
<Compile Include="Protocol\Response.cs" />
7879
<Compile Include="Protocol\ResponseInfo.cs" />
7980
<Compile Include="Protocol\ResponseStatus.cs" />
81+
<Compile Include="Util\TypeConverter.cs" />
8082
</ItemGroup>
8183
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
8284
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.

Beanstalk.Client/JobState.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* libBeanstalk.NET
3+
* Copyright (C) 2010 Arne F. Claassen
4+
* geekblog [at] claassen [dot] net
5+
* http://github.com/sdether/libBeanstalk.NET
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
namespace Droog.Beanstalk.Client {
21+
public enum JobState {
22+
Ready,
23+
Delayed,
24+
Reserved,
25+
Buried
26+
}
27+
}

Beanstalk.Client/JobStats.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,24 @@
1717
* limitations under the License.
1818
*/
1919

20+
using System;
2021
using System.Collections.Generic;
2122
using Droog.Beanstalk.Client.Protocol;
23+
using Droog.Beanstalk.Client.Util;
2224

2325
namespace Droog.Beanstalk.Client {
2426
public class JobStats : StatsBase {
2527
public JobStats(IDictionary<string, string> dictionary) : base(dictionary) { }
28+
29+
public uint Id { get { return this["id"].As<uint>(); } }
30+
public string Tube { get { return this["tube"]; } }
31+
public JobState State { get { return this["state"].As<JobState>(); } }
32+
public TimeSpan Age { get { return TimeSpan.FromSeconds(this["age"].As<uint>()); } }
33+
public uint Priority { get { return this["pri"].As<uint>(); } }
34+
public TimeSpan TimeLeft { get { return TimeSpan.FromSeconds(this["time-left"].As<uint>()); } }
35+
public uint Timeouts { get { return this["timeouts"].As<uint>(); } }
36+
public uint Releases { get { return this["releases"].As<uint>(); } }
37+
public uint Buries { get { return this["buries"].As<uint>(); } }
38+
public uint Kicks { get { return this["kicks"].As<uint>(); } }
2639
}
2740
}

Beanstalk.Client/ServerStats.cs

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,55 @@
1717
* limitations under the License.
1818
*/
1919

20+
using System;
2021
using System.Collections.Generic;
2122
using Droog.Beanstalk.Client.Protocol;
23+
using Droog.Beanstalk.Client.Util;
2224

2325
namespace Droog.Beanstalk.Client {
2426
public class ServerStats : StatsBase {
2527

26-
public ServerStats(IDictionary<string, string> dictionary)
27-
: base(dictionary) {
28-
}
28+
public ServerStats(IDictionary<string, string> dictionary) : base(dictionary) { }
29+
30+
public uint CurrentUrgentJobs { get { return this["current-jobs-urgent"].As<uint>(); } }
31+
public uint CurrentReadyJobs { get { return this["current-jobs-ready"].As<uint>(); } }
32+
public uint CurrentReservedJobs { get { return this["current-jobs-reserved"].As<uint>(); } }
33+
public uint CurrentDelayedJobs { get { return this["current-jobs-delayed"].As<uint>(); } }
34+
public uint CurrentBuriedJobs { get { return this["current-jobs-buried"].As<uint>(); } }
35+
public uint PutCommandCount { get { return this["cmd-put"].As<uint>(); } }
36+
public uint PeekCommandCount { get { return this["cmd-peek"].As<uint>(); } }
37+
public uint PeekReadyCommandCount { get { return this["cmd-peek-ready"].As<uint>(); } }
38+
public uint PeekDelayedCommandCount { get { return this["cmd-peek-delayed"].As<uint>(); } }
39+
public uint ReserveCommandCount { get { return this["cmd-reserve"].As<uint>(); } }
40+
public uint UseCommandCount { get { return this["cmd-use"].As<uint>(); } }
41+
public uint WatchCommandCount { get { return this["cmd-watch"].As<uint>(); } }
42+
public uint IgnoreCommandCount { get { return this["cmd-ignore"].As<uint>(); } }
43+
public uint DeleteCommandCount { get { return this["cmd-delete"].As<uint>(); } }
44+
public uint ReleaseCommandCount { get { return this["cmd-release"].As<uint>(); } }
45+
public uint BuryCommandCount { get { return this["cmd-bury"].As<uint>(); } }
46+
public uint KickCommandCount { get { return this["cmd-kick"].As<uint>(); } }
47+
public uint ServerStatsCommandCount { get { return this["cmd-stats"].As<uint>(); } }
48+
public uint JobStatsCommandCount { get { return this["cmd-stats-job"].As<uint>(); } }
49+
public uint TubeStatsCommandCount { get { return this["cmd-stats-tube"].As<uint>(); } }
50+
public uint ListTubesCommandCount { get { return this["cmd-list-tubes"].As<uint>(); } }
51+
public uint ListUsedTubesCommandCount { get { return this["cmd-list-tube-used"].As<uint>(); } }
52+
public uint ListWatchedTubesCommandCount { get { return this["cmd-list-tubes-watched"].As<uint>(); } }
53+
public uint JobTimeouts { get { return this["job-timeouts"].As<uint>(); } }
54+
public uint TotalJobs { get { return this["total-jobs"].As<uint>(); } }
55+
public uint MaxJobSize { get { return this["max-job-size"].As<uint>(); } }
56+
public uint CurrentTubeCount { get { return this["current-tubes"].As<uint>(); } }
57+
public uint CurrentConnectionCount { get { return this["current-connections"].As<uint>(); } }
58+
public uint CurrentProducerCount { get { return this["current-producers"].As<uint>(); } }
59+
public uint CurrentWorkerCount { get { return this["current-workers"].As<uint>(); } }
60+
public uint CurrentWaitingClientCount { get { return this["current-waiting"].As<uint>(); } }
61+
public uint TotalConnections { get { return this["total-connections"].As<uint>(); } }
62+
public uint Pid { get { return this["pid"].As<uint>(); } }
63+
public string Version { get { return this["version"]; } }
64+
public TimeSpan UserUsagetime { get { return TimeSpan.FromSeconds(this["rusage-utime"].As<uint>()); } }
65+
public TimeSpan SystemUsagetime { get { return TimeSpan.FromSeconds(this["rusage-stime"].As<uint>()); } }
66+
public TimeSpan Uptime { get { return TimeSpan.FromSeconds(this["uptime"].As<uint>()); } }
67+
public uint BinlogOldestIndex { get { return this["binlog-oldest-index"].As<uint>(); ; } }
68+
public uint BinlogCurrentIndex { get { return this["binlog-current-index"].As<uint>(); ; } }
69+
public uint BinlogMaxSize { get { return this["binlog-max-size"].As<uint>(); } }
2970
}
3071
}

Beanstalk.Client/TubeStats.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,19 @@
1919

2020
using System.Collections.Generic;
2121
using Droog.Beanstalk.Client.Protocol;
22+
using Droog.Beanstalk.Client.Util;
2223

2324
namespace Droog.Beanstalk.Client {
2425
public class TubeStats : StatsBase {
2526
public TubeStats(IDictionary<string, string> dictionary) : base(dictionary) { }
27+
28+
public string Name { get { return this["name"]; } }
29+
public uint CurrentUrgentJobs { get { return this["current-jobs-urgent"].As<uint>(); } }
30+
public uint CurrentReadyJobs { get { return this["current-jobs-ready"].As<uint>(); } }
31+
public uint CurrentReservedJobs { get { return this["current-jobs-reserved"].As<uint>(); } }
32+
public uint CurrentDelayedJobs { get { return this["current-jobs-delayed"].As<uint>(); } }
33+
public uint CurrentBuriedJobs { get { return this["current-jobs-buried"].As<uint>(); } }
34+
public uint TotalJobs { get { return this["total-jobs"].As<uint>(); } }
35+
public uint CurrentWaiting { get { return this["current-waiting"].As<uint>(); } }
2636
}
2737
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* libBeanstalk.NET
3+
* Copyright (C) 2010 Arne F. Claassen
4+
* geekblog [at] claassen [dot] net
5+
* http://github.com/sdether/libBeanstalk.NET
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
using System;
20+
21+
namespace Droog.Beanstalk.Client.Util {
22+
public static class TypeConverter {
23+
public static T As<T>(this object value) {
24+
var type = typeof(T);
25+
if(type == typeof(object)) {
26+
return (T)value;
27+
}
28+
29+
// check if target type is nullable
30+
var nullableType = Nullable.GetUnderlyingType(type);
31+
if(nullableType != null) {
32+
if(value == null) {
33+
return (T)value;
34+
}
35+
type = nullableType;
36+
}
37+
38+
// check if type is enum and value is a value type
39+
if(type.IsEnum) {
40+
var valueType = value.GetType();
41+
if(valueType.IsValueType && (value is Byte || value is Int32 || value is SByte || value is Int16 || value is Int64 || value is UInt16 || value is UInt32 || value is UInt64)) {
42+
return (T)Enum.ToObject(type, value);
43+
}
44+
}
45+
46+
// check if value is string
47+
if(value is string) {
48+
if(type.IsEnum) {
49+
50+
// target type is enum
51+
try {
52+
return (T)Enum.Parse(type, (string)value, true);
53+
} catch {
54+
throw new InvalidCastException();
55+
}
56+
}
57+
}
58+
59+
// system type converter
60+
return (T)Convert.ChangeType(value, type);
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)