forked from alexdemers/csharp-resque
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResque.cs
86 lines (73 loc) · 2.69 KB
/
Resque.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using ServiceStack.Redis;
namespace Resque
{
public class NoQueueError : Exception { }
public class NoClassError : Exception { }
public class Resque
{
public const double Version = 1.0;
public static Dictionary<string, Type> RegisteredJobs = new Dictionary<string, Type>();
public static PooledRedisClientManager PooledRedisClientManager;
public static void SetRedis(string hostname = "localhost", int port = 6379, long database = 0)
{
PooledRedisClientManager = new PooledRedisClientManager(new[] {hostname + ":" + port},
new[] {hostname + ":" + port}, database);
}
public static void Push(string queue, JObject item)
{
using (var redis = PooledRedisClientManager.GetClient())
{
redis.AddItemToSet("resque:queue", queue);
redis.PushItemToList("resque:queue:" + queue, item.ToString());
}
}
public static JObject Pop(string queue)
{
using (var redis = PooledRedisClientManager.GetClient())
{
var data = redis.PopItemFromList("resque:queue:" + queue);
if (data == null) return null;
return JsonConvert.DeserializeObject<JObject>(data);
}
}
public static long Size(string queue)
{
using (var redis = PooledRedisClientManager.GetClient())
{
return redis.GetListCount("resque:queue:" + queue);
}
}
public static bool Enqueue(string queue, string className, JObject arguments, bool trackStatus = false)
{
var argumentsArray = new JArray
{
arguments
};
var result = Job.Create(queue, className, argumentsArray, trackStatus);
if (result)
{
Event.OnAfterEnqueue(className, arguments, queue, EventArgs.Empty);
}
return result;
}
public static Job Reserve(string queue)
{
return Job.Reserve(queue);
}
public static void AddJob(string className, Type type)
{
RegisteredJobs.Add(className, type);
}
public static HashSet<string> Queues()
{
using (var redis = PooledRedisClientManager.GetClient())
{
return redis.GetAllItemsFromSet("resque:queues");
}
}
}
}