This repository has been archived by the owner on May 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathResque.cs
111 lines (93 loc) · 3.54 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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
{
private const string RESQUE_QUEUES_KEY = "resque:queues";
private const string RESQUE_QUEUE_KEY_PREFIX = "resque:queue:";
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, string password = "")
{
string pwPrefix = "";
if (password.Length > 0)
{
pwPrefix = password + "@";
}
PooledRedisClientManager = new PooledRedisClientManager(new[] {pwPrefix + hostname + ":" + port},
new[] {pwPrefix + hostname + ":" + port}, database);
}
public static void Push(string queue, JObject item)
{
using (var redis = PooledRedisClientManager.GetClient())
{
redis.AddItemToSet(RESQUE_QUEUES_KEY, queue);
redis.PushItemToList(RESQUE_QUEUE_KEY_PREFIX + queue, item.ToString(Formatting.None));
}
}
public static JObject Pop(string queue)
{
using (var redis = PooledRedisClientManager.GetClient())
{
var data = redis.RemoveStartFromList(RESQUE_QUEUE_KEY_PREFIX + 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_KEY_PREFIX + 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 bool Enqueue(string queue, string className, JArray arguments, bool trackStatus = false)
{
var argumentsObject = new JObject
{
{ "Values", arguments }
};
var result = Job.Create(queue, className, arguments, trackStatus);
if (result)
{
Event.OnAfterEnqueue(className, argumentsObject, 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_KEY);
}
}
}
}