Search code examples
phpvagrantdaemonbackground-processhhvm

Creating a background process under HHVM


I have a php script that I'd like to run in the background after the user submits a form. The simplest solution would seem to be to use a fork like the following:

$pid = pcntl_fork();
if ($pid) {
    // Parent process. Do nothing.
} else {
    // Child process. Put background script in this block.
}

This works well from the command line, but has been "disallowed" for browsers (HHVM can't use pcntl_fork).


The next simplest solution is to use

exec( 'php ' . $path . ' > /dev/null &' );

where $path would point to my background script. Again, this works perfectly on the command line, but when run from the browser it would produce the error

"Failed to initialize central HHBC repository: Failed to open file /var/www/.hhvm.hhbc"

I didn't even have a /var/www/ folder, so out of desperation I created one and symlinked the .hhvm.hhbc from /home/vagrant/.hhvm.hhbc into the new directory. I then got hit with a

"Failed to initialize central HHBC repository: Failed to initialize schema in /var/www/.hhvm.hhbc:"

I went a little crazy with chmod 777's throughout my html folder and on the original .hhvm.hhbc, but it did me no good.


It's a really bad user experience to run my background script in the foreground, because then there's a ~15 second pause after the submit button is clicked before the user can see the page again. Does anyone have an idea of how to do this?


Solution

  • Try using "pagelets". This lets you spin up a new, background PHP request. Docs here: https://github.com/facebook/hhvm/blob/master/hphp/doc/threading