55use Closure ;
66use Exception ;
77use Laravel \SerializableClosure \SerializableClosure ;
8- use Symfony \Component \Process \Process as SymfonyProcess ;
98
109class Process
1110{
1211 private const MAX_INT = 2147483647 ;
1312
14- public function __construct (private int $ key = 0 )
13+ public function __construct (private int $ shmopKey = 0 )
1514 {
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
1817 }
1918 }
2019
@@ -23,30 +22,24 @@ public function __construct(private int $key = 0)
2322 */
2423 public function send (Closure $ asyncFunction ): void
2524 {
26- $ separator = DIRECTORY_SEPARATOR ;
25+ $ dirSlash = DIRECTORY_SEPARATOR ;
2726 $ 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 );
3029
3130 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 );
3332 }
3433
3534 $ bytesWritten = shmop_write ($ shmopInstance , $ serialized , 0 );
3635
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 );
4039 }
4140
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 ();
5144 }
5245}
0 commit comments