@@ -19,6 +19,23 @@ class Resque
19
19
*/
20
20
public static $ redis = null ;
21
21
22
+ /**
23
+ * @var mixed Host/port conbination separated by a colon, or a nested
24
+ * array of server swith host/port pairs
25
+ */
26
+ protected static $ redisServer = null ;
27
+
28
+ /**
29
+ * @var int ID of Redis database to select.
30
+ */
31
+ protected static $ redisDatabase = 0 ;
32
+
33
+ /**
34
+ * @var int PID of current process. Used to detect changes when forking
35
+ * and implement "thread" safety to avoid race conditions.
36
+ */
37
+ protected static $ pid = null ;
38
+
22
39
/**
23
40
* Given a host/port combination separated by a colon, set it as
24
41
* the redis server that Resque will talk to.
@@ -28,17 +45,9 @@ class Resque
28
45
*/
29
46
public static function setBackend ($ server , $ database = 0 )
30
47
{
31
- if (is_array ($ server )) {
32
- require_once dirname (__FILE__ ) . '/Resque/RedisCluster.php ' ;
33
- self ::$ redis = new Resque_RedisCluster ($ server );
34
- }
35
- else {
36
- list ($ host , $ port ) = explode (': ' , $ server );
37
- require_once dirname (__FILE__ ) . '/Resque/Redis.php ' ;
38
- self ::$ redis = new Resque_Redis ($ host , $ port );
39
- }
40
-
41
- self ::redis ()->select ($ database );
48
+ self ::$ redisServer = $ server ;
49
+ self ::$ redisDatabase = $ database ;
50
+ self ::$ redis = null ;
42
51
}
43
52
44
53
/**
@@ -48,10 +57,34 @@ public static function setBackend($server, $database = 0)
48
57
*/
49
58
public static function redis ()
50
59
{
51
- if (is_null (self ::$ redis )) {
52
- self ::setBackend ('localhost:6379 ' );
60
+ // Detect when the PID of the current process has changed (from a fork, etc)
61
+ // and force a reconnect to redis.
62
+ $ pid = getmypid ();
63
+ if (self ::$ pid !== $ pid ) {
64
+ self ::$ redis = null ;
65
+ self ::$ pid = $ pid ;
66
+ }
67
+
68
+ if (!is_null (self ::$ redis )) {
69
+ return self ::$ redis ;
70
+ }
71
+
72
+ $ server = self ::$ redisServer ;
73
+ if (empty ($ server )) {
74
+ $ server = 'localhost:6379 ' ;
75
+ }
76
+
77
+ if (is_array ($ server )) {
78
+ require_once dirname (__FILE__ ) . '/Resque/RedisCluster.php ' ;
79
+ self ::$ redis = new Resque_RedisCluster ($ server );
80
+ }
81
+ else {
82
+ list ($ host , $ port ) = explode (': ' , $ server );
83
+ require_once dirname (__FILE__ ) . '/Resque/Redis.php ' ;
84
+ self ::$ redis = new Resque_Redis ($ host , $ port );
53
85
}
54
86
87
+ self ::$ redis ->select (self ::$ redisDatabase );
55
88
return self ::$ redis ;
56
89
}
57
90
0 commit comments