5
5
use Closure ;
6
6
use Exception ;
7
7
use Laravel \SerializableClosure \SerializableClosure ;
8
- use Symfony \Component \Process \Process as SymfonyProcess ;
9
8
10
9
class Process
11
10
{
12
11
private const MAX_INT = 2147483647 ;
13
12
14
- public function __construct (private int $ key = 0 )
13
+ public function __construct (private int $ shmopKey = 0 )
15
14
{
16
- if (!$ this ->key ) {
17
- $ this ->key = mt_rand (0 , self ::MAX_INT ); // communication key
15
+ if (!$ this ->shmopKey ) {
16
+ $ this ->shmopKey = mt_rand (0 , self ::MAX_INT ); // communication key
18
17
}
19
18
}
20
19
@@ -23,30 +22,24 @@ public function __construct(private int $key = 0)
23
22
*/
24
23
public function send (Closure $ asyncFunction ): void
25
24
{
26
- $ separator = DIRECTORY_SEPARATOR ;
25
+ $ dirSlash = DIRECTORY_SEPARATOR ;
27
26
$ serialized = serialize (new SerializableClosure ($ asyncFunction ));
28
- $ serializedLength = strlen ($ serialized );
29
- $ shmopInstance = shmop_open ($ this ->key , 'c ' , 0660 , $ serializedLength );
27
+ $ compressedLength = mb_strlen ($ serialized );
28
+ $ shmopInstance = shmop_open ($ this ->shmopKey , 'c ' , 0660 , $ compressedLength );
30
29
31
30
if (!$ shmopInstance ) {
32
- throw new Exception ('Could not create shmop instance with key: ' . $ this ->key );
31
+ throw new Exception ('Could not create shmop instance with key: ' . $ this ->shmopKey );
33
32
}
34
33
35
34
$ bytesWritten = shmop_write ($ shmopInstance , $ serialized , 0 );
36
35
37
- if ($ bytesWritten != $ serializedLength ) {
38
- throw new Exception ('Could not write the entire data to shared memory with length: ' .
39
- $ serializedLength . '. Bytes written: ' . $ bytesWritten );
36
+ if ($ bytesWritten < $ compressedLength ) {
37
+ throw new Exception ('Error: Could not write the entire data to shared memory with length: ' .
38
+ $ compressedLength . '. Bytes written: ' . $ bytesWritten . PHP_EOL );
40
39
}
41
40
42
- if (PHP_OS_FAMILY === 'Windows ' ) {
43
- $ arg = ['start ' , '"" ' , '/B ' , PHP_BINARY , __DIR__ . $ separator . 'background_processor.php ' , $ this ->key ];
44
- $ process = new SymfonyProcess ($ arg );
45
- $ process ->start ();
46
- return ;
47
- }
48
-
49
- exec (PHP_BINARY . ' ' . __DIR__ . $ separator . 'background_processor.php ' . $ this ->key .
50
- ' > /dev/null 2>&1 & ' );
41
+ $ fileWithPath = __DIR__ . $ dirSlash . 'background_processor.php ' ;
42
+ $ file = new PhpFile ($ fileWithPath , [$ this ->shmopKey ]);
43
+ $ file ->run ();
51
44
}
52
45
}
0 commit comments