-
-
Notifications
You must be signed in to change notification settings - Fork 171
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Child processes #389
Child processes #389
Conversation
- ChildProcess instances can be used to interact with a process - get, all and restart are piped up
Co-authored-by: Simon Hamp <[email protected]>
child process - sanity tests and tweaks
Hey @simonhamp, I was following this conversation and wasn't able to deduce whether you guys managed to come up with a proper inter-process communication mechanism. My use case is that I'm listening to the #[On('native:'.SomeCustomEvent::class)] final class SomeCustomEvent implements ShouldBroadcastNow
{
use Dispatchable;
use InteractsWithSockets;
public function broadcastOn(): array
{
return [
new Channel('nativephp'),
];
}
} This works great when the event is dispatched from a queue worker ( I do see that you've had to implement some custom JavaScript to fire the (4?) pre-defined events dedicated to child processes: proc.stdout.on('data', (data) => {
notifyLaravel('events', {
event: 'Native\\Laravel\\Events\\ChildProcess\\MessageReceived',
payload: {
alias,
data: data.toString(),
}
});
});
proc.stderr.on('data', (data) => {
console.error('Error received from process [' + alias + ']:', data.toString());
notifyLaravel('events', {
event: 'Native\\Laravel\\Events\\ChildProcess\\ErrorReceived',
payload: {
alias,
data: data.toString(),
}
});
});
proc.on('spawn', () => {
console.log('Process [' + alias + '] spawned!');
state.processes[alias] = {
pid: proc.pid,
proc,
settings
};
notifyLaravel('events', {
event: 'Native\\Laravel\\Events\\ChildProcess\\ProcessSpawned',
payload: [alias, proc.pid]
});
});
proc.on('exit', (code) => {
console.log(`Process [${alias}] exited with code [${code}].`);
notifyLaravel('events', {
event: 'Native\\Laravel\\Events\\ChildProcess\\ProcessExited',
payload: {
alias,
code,
}
});
const settings = Object.assign({}, getSettings(alias));
delete state.processes[alias];
if (settings.persistent) {
console.log('Process [' + alias + '] wathchdog restarting...');
startProcess(settings);
}
}); So clearly, there is a manual process involved in making the parent PHP process aware of the child events. Btw, these work with Livewire! Is there a way you can accomodate for firing custom events on the |
Hey @XbNz! You're correct, the only two ways to have inter-process communication are:
@gwleuverink just finished the fix, so it should be merged soon enough 😉 |
I completely neglected that as a possible solution. It's even described in the docs lol. In that case, it'll be possible to create a long-running worker using something like ReactPHP's WritableStreamInterface to implement a loop that handles a JSON-encoded workload and sends the response to Thanks for the quick help. |
The Laravel side to the long-running process feature
Electron side: NativePHP/electron#115
Docs: NativePHP/nativephp.com#53