@@ -52,12 +52,9 @@ function addWorkerInclude( $phpFilePath ) {
52
52
$ this ->workerIncludes [] = $ phpFilePath ;
53
53
}
54
54
55
- function getResults ( $ jobCallback = null ) {
55
+ function getOrderedResults ( ) {
56
56
57
- if ( $ jobCallback && !is_callable ( $ jobCallback ) )
58
- throw new \InvalidArgumentException ( '$jobCallback is not callable ' );
59
-
60
- $ this ->jobCallback = $ jobCallback ;
57
+ $ this ->jobCallback = null ;
61
58
62
59
$ loop = new EventLoop ( $ this ->serverSocketAddr );
63
60
$ loop ->listen ( $ this ->workerTimeout );
@@ -75,6 +72,23 @@ function getResults( $jobCallback = null ) {
75
72
return $ results ;
76
73
}
77
74
75
+ function getResults ( $ jobCallback ) {
76
+
77
+ if ( !is_callable ( $ jobCallback ) )
78
+ throw new \InvalidArgumentException ( '$jobCallback is not callable ' );
79
+
80
+ $ this ->jobCallback = $ jobCallback ;
81
+
82
+ $ loop = new EventLoop ( $ this ->serverSocketAddr );
83
+ $ loop ->listen ( $ this ->workerTimeout );
84
+ $ loop ->subscribe ( array ( $ this , '_messageCallback ' ) );
85
+
86
+ if ( !$ this ->workerProcs )
87
+ $ this ->createWorkerProcs ( $ this ->workerCount );
88
+
89
+ $ loop ->run ();
90
+ }
91
+
78
92
// FIXME: workerTimeout isn't working yet. A sleep()ing worker does _not_
79
93
// cause a timeout. Check why.
80
94
function setWorkerTimeout ( $ timeout ) {
@@ -110,7 +124,10 @@ function _messageCallback( Message $message, EventLoop $loop, $socket ) {
110
124
if ( $ headers [ 'cmd ' ] === 'job-result ' ) {
111
125
112
126
$ jobNumber = $ headers [ 'job-num ' ];
113
- $ this ->results [ $ jobNumber ] = $ message ->body ;
127
+ // Only store the results if the client called getOrderedResults()
128
+ $ this ->results [ $ jobNumber ] = ( $ this ->jobCallback )
129
+ ? true
130
+ : $ message ->body ;
114
131
115
132
if ( $ this ->jobCallback ) {
116
133
call_user_func ( $ this ->jobCallback , $ message ->body , count ( $ this ->results ),
0 commit comments