Skip to content

Commit 0ae5845

Browse files
committed
Polish adapter, add cleanup of unix sockets
1 parent 00280ea commit 0ae5845

File tree

5 files changed

+54
-48
lines changed

5 files changed

+54
-48
lines changed

EProcess/Adapter/BaseAdapter.php

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace EProcess\Adapter;
4+
5+
use React\EventLoop\LoopInterface;
6+
7+
abstract class BaseAdapter
8+
{
9+
protected $loop;
10+
protected $node;
11+
12+
public function __construct(LoopInterface $loop)
13+
{
14+
$this->loop = $loop;
15+
$this->node = uniqid('thread_');
16+
}
17+
18+
protected function createUnixSocket()
19+
{
20+
$unixFile = sprintf('tmp/%s.sock', $this->node);
21+
$unix = sprintf('unix://%s', $unixFile);
22+
23+
$cleanup = function() use ($unixFile) {
24+
$this->loop->stop();
25+
@unlink($unixFile);
26+
};
27+
28+
register_shutdown_function($cleanup);
29+
pcntl_signal(SIGINT, $cleanup);
30+
31+
return $unix;
32+
}
33+
34+
protected function getNode()
35+
{
36+
37+
}
38+
39+
abstract public function create($class, array $data = []);
40+
abstract public function kill();
41+
}

EProcess/Adapter/ChildProcess.php

+7-19
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66
use EProcess\MessengerFactory;
77

88
use React\ChildProcess\Process;
9-
use React\EventLoop\LoopInterface;
109
use Symfony\Component\Process\PhpExecutableFinder;
1110

12-
class ChildProcess
11+
class ChildProcess extends BaseAdapter
1312
{
1413
use UniversalSerializer;
1514

@@ -49,26 +48,19 @@ class ChildProcess
4948

5049
private $loop;
5150
private $process;
52-
private $executableFinder;
53-
54-
public function __construct(LoopInterface $loop)
55-
{
56-
$this->loop = $loop;
57-
$this->executableFinder = new PhpExecutableFinder();
58-
}
5951

6052
public function create($class, array $data = [])
6153
{
62-
if (false === $php = $this->executableFinder->find()) {
54+
$executableFinder = new PhpExecutableFinder();
55+
56+
if (false === $php = $executableFinder->find()) {
6357
throw new \RuntimeException('Unable to find the PHP executable.');
6458
}
6559

66-
$node = uniqid('thread_');
67-
$unix = sprintf('unix://tmp/%s.sock', $node);
68-
60+
$unix = $this->createUnixSocket();
6961
$messenger = MessengerFactory::server($unix, $this->loop);
7062

71-
$file = sprintf(__DIR__ . '/../../tmp/%s.php', $node);
63+
$file = sprintf(__DIR__ . '/../../tmp/%s.php', $this->node);
7264

7365
file_put_contents($file, sprintf(
7466
$this->script,
@@ -81,7 +73,7 @@ public function create($class, array $data = [])
8173
$this->process = new Process(sprintf('exec %s %s', $php, realpath($file)));
8274
$this->process->start($this->loop);
8375

84-
$this->loop->addTimer(5, function() use ($file) {
76+
$this->loop->addTimer(3, function() use ($file) {
8577
unlink($file);
8678
});
8779

@@ -93,10 +85,6 @@ public function create($class, array $data = [])
9385
echo $data;
9486
});
9587

96-
register_shutdown_function(function() use ($unix) {
97-
unlink($unix);
98-
});
99-
10088
return $messenger;
10189
}
10290

EProcess/Adapter/PThreads.php

+2-15
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,14 @@
33
namespace EProcess\Adapter;
44

55
use EProcess\MessengerFactory;
6-
use React\EventLoop\LoopInterface;
76

8-
class PThreads
7+
class PThreads extends BaseAdapter
98
{
10-
private $loop;
119
private $process;
1210

13-
public function __construct(LoopInterface $loop)
14-
{
15-
$this->loop = $loop;
16-
}
17-
1811
public function create($class, array $data = [])
1912
{
20-
$node = uniqid('thread_');
21-
$unix = sprintf('unix://tmp/%s.sock', $node);
22-
23-
register_shutdown_function(function() use ($unix) {
24-
unlink($unix);
25-
});
26-
13+
$unix = $this->createUnixSocket();
2714
$messenger = MessengerFactory::server($unix, $this->loop);
2815

2916
$this->process = new Thread($unix, $class, $data);

EProcess/Adapter/SymfonyProcess.php

+2-14
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44

55
use EProcess\Behaviour\UniversalSerializer;
66
use Symfony\Component\Process\PhpProcess;
7-
use React\EventLoop\LoopInterface;
87
use EProcess\MessengerFactory;
98

10-
class SymfonyProcess
9+
class SymfonyProcess extends BaseAdapter
1110
{
1211
use UniversalSerializer;
1312

@@ -48,20 +47,9 @@ class SymfonyProcess
4847
private $loop;
4948
private $process;
5049

51-
public function __construct(LoopInterface $loop)
52-
{
53-
$this->loop = $loop;
54-
}
55-
5650
public function create($class, array $data = [])
5751
{
58-
$node = uniqid('thread_');
59-
$unix = sprintf('unix://app/cache/%s.sock', $node);
60-
61-
register_shutdown_function(function() use ($unix) {
62-
unlink($unix);
63-
});
64-
52+
$unix = $this->createUnixSocket();
6553
$messenger = MessengerFactory::server($unix, $this->loop);
6654

6755
$script = sprintf($this->script, EPROCESS_AUTOLOAD, $unix, $class, base64_encode($this->serialize($data)));

examples/autoload.php

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(ticks = 1);
4+
35
define('EPROCESS_AUTOLOAD', __FILE__);
46

57
$loader = require __DIR__ . '/../vendor/autoload.php';

0 commit comments

Comments
 (0)