6
6
use EProcess \MessengerFactory ;
7
7
8
8
use React \ChildProcess \Process ;
9
- use React \EventLoop \LoopInterface ;
10
9
use Symfony \Component \Process \PhpExecutableFinder ;
11
10
12
- class ChildProcess
11
+ class ChildProcess extends BaseAdapter
13
12
{
14
13
use UniversalSerializer;
15
14
@@ -49,26 +48,19 @@ class ChildProcess
49
48
50
49
private $ loop ;
51
50
private $ process ;
52
- private $ executableFinder ;
53
-
54
- public function __construct (LoopInterface $ loop )
55
- {
56
- $ this ->loop = $ loop ;
57
- $ this ->executableFinder = new PhpExecutableFinder ();
58
- }
59
51
60
52
public function create ($ class , array $ data = [])
61
53
{
62
- if (false === $ php = $ this ->executableFinder ->find ()) {
54
+ $ executableFinder = new PhpExecutableFinder ();
55
+
56
+ if (false === $ php = $ executableFinder ->find ()) {
63
57
throw new \RuntimeException ('Unable to find the PHP executable. ' );
64
58
}
65
59
66
- $ node = uniqid ('thread_ ' );
67
- $ unix = sprintf ('unix://tmp/%s.sock ' , $ node );
68
-
60
+ $ unix = $ this ->createUnixSocket ();
69
61
$ messenger = MessengerFactory::server ($ unix , $ this ->loop );
70
62
71
- $ file = sprintf (__DIR__ . '/../../tmp/%s.php ' , $ node );
63
+ $ file = sprintf (__DIR__ . '/../../tmp/%s.php ' , $ this -> node );
72
64
73
65
file_put_contents ($ file , sprintf (
74
66
$ this ->script ,
@@ -81,7 +73,7 @@ public function create($class, array $data = [])
81
73
$ this ->process = new Process (sprintf ('exec %s %s ' , $ php , realpath ($ file )));
82
74
$ this ->process ->start ($ this ->loop );
83
75
84
- $ this ->loop ->addTimer (5 , function () use ($ file ) {
76
+ $ this ->loop ->addTimer (3 , function () use ($ file ) {
85
77
unlink ($ file );
86
78
});
87
79
@@ -93,10 +85,6 @@ public function create($class, array $data = [])
93
85
echo $ data ;
94
86
});
95
87
96
- register_shutdown_function (function () use ($ unix ) {
97
- unlink ($ unix );
98
- });
99
-
100
88
return $ messenger ;
101
89
}
102
90
0 commit comments