Feature request: Working with long running forked processes #371
Replies: 4 comments 5 replies
-
Yep, I love this. Quite a few folks are asking about spinning up processes when their app runs. On the surface, this feels simply like PHP could handle this all... but if you want decent performance and "proper" control and management of the processes that are spawned, you'd need Bummer! Luckily, both Electron and Tauri have pretty good first-party support for cross-platform process management via Node and Rust, respectively. I like your use Native\Laravel\Facades\Process;
/**
* @param string $name A unique name for this process
* @param array $cmd The command to run
* @param string $cwd The working directory to run the command from
* @param array $env Override
*/
Process::start('internal-server', ['php', 'artisan', 'serve'], env: ['CUSTOM_ENV' => 'yes please!']);
// Then other methods can just receive the process name. The Laravel app doesn't necessarily need any concept
// of 'remembering' processes, they can just be referenced by name.
Process::stop('internal-server');
Process::sendMessage('internal-server', 'some message string');
// Maybe a convenience for handling events...
Process::onMessage('internal-server', function ($message) {});
Process::onExit('internal-server', function ($output) {});
// These Could potentially be chained before or after the `start` call too - the order shouldn't matter
Process::create('internal-server', ['php', 'artisan', 'serve'], env: ['CUSTOM_ENV' => 'yes please!'])
->onMessage(function ($message) {})
->onExit(fn ...)
->start(); Then on the Electron side, there would need to be an endpoint that can handle this request and asynchronously turn it into a managed spawned command (I recommend using Electron's I guess we should use a EventsWe should register standardised event handlers for each of the supported events on each process we spawn. These events should fire new NativePHP events - again, very similar to what we do for windows. In terms of where we make event handling available, I'd try to keep things pretty simple to start with: i.e. let's just use the event mechanics we already have rather than inventing new ones in Javascript... i.e. avoid |
Beta Was this translation helpful? Give feedback.
-
Hello there. My use case: What i need:
more complex aproach:
api You guys proveded looks good, would love to see it happen <3 |
Beta Was this translation helpful? Give feedback.
-
For anyone who's following this thread, progress is being made on this in the following PRs: Current status is - it's working 🎉 but there is more to do to make it robust enough for general consumption |
Beta Was this translation helpful? Give feedback.
-
This got released yesterday! @pkristian let me know when if you want to go through this together |
Beta Was this translation helpful? Give feedback.
-
Hi all,
I've got this idea for NativePHP I'd love to get some feedback on. It looks like a fun challenge and could open up NativePHP to cater to a whole new set of new use-cases.
The problem
While working on Phost (the app I use as a nativephp proving ground) I had the need to have a long running php process in the background. In this case I use that to spawn a SMTP server with ReactPHP.
In a Electron app this is done using a forked process. In NativePHP there is no recommended way to do this right now. (as far as I know)
I’ve tried a bunch of approaches, in the end I landed on a scheduled job that keeps the smtp server alive, then continually poll the back-end to check if it’s running.
This works but lacks on a couple of fronts.
It would be very powerful if NativePHP provided a first party method to manage parallel php processes (with ReactPHP for example). The recent addition of the IPC stuff has gotten me thinking this might be the key to building a nice api for this.
A solution?
I'm still workshopping the idea, but I envision something like this:
Then using the new Native helper I imagine we can have a api to perform some basic operations on the process.
Additionally I can see some benefit to dispatch process events internally we can react to in our webview
As to the process communicating messages back to the UI, It could be enough to use the existing Events over the nativephp channel for inter process communication. Something more sophisticated might also be possible, to pass messages from and to the process, but this will do for now not to complicate it too early.
With this concept I could (hopefully) achieve something like this; a way to dynamically update a status indicator & restart the process from the UI. For example using Alpine:
A big asterisk
Right now I have no idea if this is even possible. Just riffing on a possible API.
Love to get your feedback on this idea!
Beta Was this translation helpful? Give feedback.
All reactions