Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[FrameworkBundle] Additional helper commands to control PHP's built-in web server #11311

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

Merged
merged 1 commit into from
Sep 22, 2014

Conversation

xabbuh
Copy link
Member

@xabbuh xabbuh commented Jul 5, 2014

Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #10827
License MIT
Doc PR symfony/symfony-docs#4005

Basically, both the server:status and server:stop wouldn't be really reliable if you had stopped the web server by, for example, killing the process. But honestly I don't know how to platform-independently determine if a process is still running given its PID. Maybe such a way could be a good improvement for the Process component.

@@ -256,7 +256,7 @@ public function getProcess()
$options = $this->options;

$arguments = array_merge($this->prefix, $this->arguments);
$script = implode(' ', array_map(array(__NAMESPACE__.'\\ProcessUtils', 'escapeArgument'), $arguments));
$script = $arguments[0].implode(' ', array_map(array(__NAMESPACE__.'\\ProcessUtils', 'escapeArgument'), array_slice($arguments, 1)));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please revert this change

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this change I wasn't able to prefix the PHP_BINARY call with exec. Do you have any idea how to handle it then?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@romainneutron To be more precise: The command that should be executed leading to a "Command not found error" actually is something like:

'exec' '/usr/bin/php' ...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either we could implement a new feature in the process builder, either you can build the command manually instead of using the ProcessBuilder, but you can not decide to unescape the first argument.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course, you're right. I first thought that it doesn't make sense to escape the command name. But that's obviously not always true. What do you think about adding a method like useExec to the ProcessBuilder class?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be indeed a nice feature. Please open a new PR for this feature as it would be easier to review this new feature in a separate thread

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created a pull request for this (see #11335).

@xabbuh
Copy link
Member Author

xabbuh commented Jul 6, 2014

Both the server:status and server:stop commands have to rely on the fact that the server:status command terminates the proper process. The usage of exec is required for this to work as expected. Thus, #11335 or a similar solution needs to be merged first (currently, this change is based on the modifications made for #11335).

{
$address = $input->getArgument('address');

if (file_exists($this->getLockFile($address))) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The server process could have crashed. You could check if the server sends HTTP responses.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. That could be done relatively easy.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The lock file is now removed if the server process terminated before.

@xabbuh
Copy link
Member Author

xabbuh commented Jul 8, 2014

I modified this a bit. The pull request is no longer based off #11335. Instead the script that is executed by the start:start command is built manually which was way easier than I had expected.

@xabbuh
Copy link
Member Author

xabbuh commented Aug 15, 2014

Any thoughts on this?

} elseif ($pid > 0) {
$output->writeln(sprintf(
'<info>Server started successfully</info>',
$pid
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This var is unused in the string.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed it.

@fabpot
Copy link
Member

fabpot commented Aug 28, 2014

I've not tried it but it looks good to me.

@fabpot
Copy link
Member

fabpot commented Sep 2, 2014

@romainneutron should give you some feedback soon...

{
list($hostname, $port) = explode(':', $address);

return @fsockopen($hostname, $port, $errno, $errstr, 3000) !== false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3000 seconds is very long timeout, I rather suggest a short one, 1 second should be enough.

Moreover, this method should return a boolean and close the socket resource once it has been created

@romainneutron
Copy link
Contributor

It would be nice to add correct exitcodes (0 on success, other codes in case of failures)

@xabbuh xabbuh force-pushed the issue-10827 branch 2 times, most recently from 4fbf128 to 459a97e Compare September 3, 2014 08:48
@xabbuh
Copy link
Member Author

xabbuh commented Sep 3, 2014

@romainneutron I added 1 where necessary. 0 is the default behaviour. Do you think it makes sense to return 0 explicitly?

@romainneutron
Copy link
Contributor

No need to return 0 in case the command was successful

@lyrixx
Copy link
Member

lyrixx commented Sep 3, 2014

The lock management could/should use #10475 (but it's not yet merged)

@xabbuh
Copy link
Member Author

xabbuh commented Sep 3, 2014

You can then have another look.

{
list($hostname, $port) = explode(':', $address);
$fp = @fsockopen($hostname, $port, $errno, $errstr, 1);
fclose($fp);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it failed, this call would fail. You should use instead:

if (false !== $fp = @fsockopen($hostname, $port, $errno, $errstr, 1)) {
    fclose($fp);

    return true;
}

return false;

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yes, that's true.

@xabbuh xabbuh force-pushed the issue-10827 branch 2 times, most recently from bde1b5e to e796bac Compare September 3, 2014 08:59
$process->start();
$lockFile = $this->getLockFile($input->getArgument('address'));
touch($lockFile);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a check: If the server could not be started, let's display an error

if (!$process->isRunning()) {
    $output->writeln('<error>Unable to start the server process</error>');
    unlink($lockfile);

    return 1;
}


private function isServerRunning($address)
{
list($hostname, $port) = explode(':', $address);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this would trigger an error in case the address is not in the right format (for example randomstring would trigger an Undefined index notice

To avoid that, you can use:

explode(':', $address) + array(8000);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or better, ensure the passed argument matches a regular expression

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really, there wouldn't be any server running with this address. So, there is no lock file and isServerRunning() would never be called (see line 48).

@xabbuh
Copy link
Member Author

xabbuh commented Sep 3, 2014

I addressed your other suggestions.

return 1;
} elseif ($pid > 0) {
$output->writeln(sprintf(
'<info>Server started successfully</info>',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$pid argument does not seem to be used with sprintf

@xabbuh xabbuh force-pushed the issue-10827 branch 2 times, most recently from 28afe29 to 74c993c Compare September 9, 2014 08:16
$pid
));
} else {
$process = $this->createServerProcess(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be good to add a call to posix_setsid() here to avoid zombie process.

    if (posix_setsid() < 0) {
        return 1;
    }

@xabbuh
Copy link
Member Author

xabbuh commented Sep 9, 2014

Thanks for the feedback.

$output->writeln('<info>Server started successfully</info>');
} else {
if (posix_setsid() < 0) {
return 1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might be nice to display a nice message in case it happens like Unable to set the child process as session leader

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@romainneutron
Copy link
Contributor

I'm 👍 on this one (once my last comment would be addressed)

$output->writeln('<error>Unable to start the server process</error>');

return 1;
} elseif ($pid > 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just if as the previous if returns

@xabbuh xabbuh force-pushed the issue-10827 branch 2 times, most recently from 8464681 to bd58bcb Compare September 9, 2014 09:51
@xabbuh
Copy link
Member Author

xabbuh commented Sep 21, 2014

Any other opinion on this?

@fabpot
Copy link
Member

fabpot commented Sep 21, 2014

👍

@fabpot
Copy link
Member

fabpot commented Sep 22, 2014

Thank you @xabbuh.

@fabpot fabpot merged commit b601454 into symfony:master Sep 22, 2014
fabpot added a commit that referenced this pull request Sep 22, 2014
…l PHP's built-in web server (xabbuh)

This PR was merged into the 2.6-dev branch.

Discussion
----------

[FrameworkBundle] Additional helper commands to control PHP's built-in web server

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #10827
| License       | MIT
| Doc PR        | symfony/symfony-docs#4005

Basically, both the ``server:status`` and ``server:stop`` wouldn't be really reliable if you had stopped the web server by, for example, killing the process. But honestly I don't know how to platform-independently determine if a process is still running given its PID. Maybe such a way could be a good improvement for the Process component.

Commits
-------

b601454 new helper commands for PHP's built-in server
@xabbuh xabbuh deleted the issue-10827 branch September 22, 2014 12:45
fabpot added a commit that referenced this pull request Oct 9, 2014
…mands (xabbuh)

This PR was merged into the 2.6-dev branch.

Discussion
----------

[FrameworkBundle] add changelog entry for web server commands

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT
| Doc PR        |

I just noticed that I forgot to add a changelog entry in #11311.

Commits
-------

292088f add changelog entry for web server commands
weaverryan added a commit to symfony/symfony-docs that referenced this pull request Oct 19, 2014
…ilt-in web server in the background (xabbuh)

This PR was merged into the master branch.

Discussion
----------

[Cookbook][Web server] description for running PHP's built-in web server in the background

| Q             | A
| ------------- | ---
| Doc fix?      | no
| New docs?     | yes (symfony/symfony#11311)
| Applies to    | 2.6+
| Fixed tickets |

Commits
-------

3caec21 description for running PHP's built-in web server in the background
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants