-
-
Notifications
You must be signed in to change notification settings - Fork 32
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
Kill other processes is port is taken #211
Open
xepozz
wants to merge
17
commits into
master
Choose a base branch
from
kill
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
c26fbb4
Kill other processes is port is taken
xepozz 266899e
Apply fixes from StyleCI
StyleCIBot 0077c14
Add changelog
xepozz 6729a7b
Merge remote-tracking branch 'origin/kill' into kill
xepozz 8e07e01
Change arguments ordering
xepozz f3ded17
Add early returning
xepozz 8789df6
Apply fixes from StyleCI
StyleCIBot 997e046
Add early returning for windows
xepozz b2f45d1
Fix linux command
xepozz 5a19634
Fix tests
xepozz 96db915
Apply fixes from StyleCI
StyleCIBot 0bd1070
Suppress psalm errors
xepozz 4a6a74e
Unpack port from address
xepozz 7ce45b0
Fix port
xepozz c62bd30
Fix psalm
xepozz d00acad
Apply fixes from StyleCI
StyleCIBot 1e16ede
Merge branch 'master' into kill
xepozz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -79,7 +79,7 @@ public function configure(): void | |||||||||||||||
$this->defaultWorkers | ||||||||||||||||
) | ||||||||||||||||
->addOption('env', 'e', InputOption::VALUE_OPTIONAL, 'It is only used for testing.') | ||||||||||||||||
->addOption('open', 'o', InputOption::VALUE_OPTIONAL, 'Opens the serving server in the default browser.') | ||||||||||||||||
->addOption('open', 'o', InputOption::VALUE_OPTIONAL, 'Opens the serving server in the default browser.', false) | ||||||||||||||||
->addOption('xdebug', 'x', InputOption::VALUE_OPTIONAL, 'Enables XDEBUG session.', false); | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
|
@@ -124,6 +124,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int | |||||||||||||||
|
||||||||||||||||
if (!str_contains($address, ':')) { | ||||||||||||||||
$address .= ':' . $port; | ||||||||||||||||
} else { | ||||||||||||||||
$port = explode(':', $address)[1]; | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
if (!is_dir($documentRoot)) { | ||||||||||||||||
|
@@ -132,8 +134,54 @@ protected function execute(InputInterface $input, OutputInterface $output): int | |||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
if ($this->isAddressTaken($address)) { | ||||||||||||||||
$io->error("http://$address is taken by another process."); | ||||||||||||||||
return self::EXIT_CODE_ADDRESS_TAKEN_BY_ANOTHER_PROCESS; | ||||||||||||||||
if ($this->isWindows()) { | ||||||||||||||||
$io->error("Port {$port} is taken by another process."); | ||||||||||||||||
return self::EXIT_CODE_ADDRESS_TAKEN_BY_ANOTHER_PROCESS; | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
/** @psalm-suppress ForbiddenCode */ | ||||||||||||||||
$runningCommandPIDs = trim((string) shell_exec('lsof -ti :8080 -s TCP:LISTEN')); | ||||||||||||||||
if (empty($runningCommandPIDs)) { | ||||||||||||||||
$io->error("Port {$port} is taken by another process."); | ||||||||||||||||
return self::EXIT_CODE_ADDRESS_TAKEN_BY_ANOTHER_PROCESS; | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
$runningCommandPIDs = array_filter(explode("\n", $runningCommandPIDs)); | ||||||||||||||||
sort($runningCommandPIDs); | ||||||||||||||||
|
||||||||||||||||
$io->block( | ||||||||||||||||
[ | ||||||||||||||||
"Port {$port} is taken by the processes:", | ||||||||||||||||
...array_map( | ||||||||||||||||
/** @psalm-suppress ForbiddenCode, PossiblyNullArgument */ | ||||||||||||||||
fn (string $pid) => sprintf( | ||||||||||||||||
'#%s: %s', | ||||||||||||||||
$pid, | ||||||||||||||||
shell_exec("ps -o command= -p {$pid}"), | ||||||||||||||||
), | ||||||||||||||||
$runningCommandPIDs, | ||||||||||||||||
), | ||||||||||||||||
], | ||||||||||||||||
'ERROR', | ||||||||||||||||
'error', | ||||||||||||||||
); | ||||||||||||||||
if (!$io->confirm('Kill the process', true)) { | ||||||||||||||||
return self::EXIT_CODE_ADDRESS_TAKEN_BY_ANOTHER_PROCESS; | ||||||||||||||||
} | ||||||||||||||||
$io->info([ | ||||||||||||||||
'Stopping the processes...', | ||||||||||||||||
]); | ||||||||||||||||
$out = array_filter( | ||||||||||||||||
array_map( | ||||||||||||||||
/** @psalm-suppress ForbiddenCode */ | ||||||||||||||||
fn (string $pid) => shell_exec("kill -9 {$pid}"), | ||||||||||||||||
$runningCommandPIDs, | ||||||||||||||||
) | ||||||||||||||||
); | ||||||||||||||||
if (!empty($out)) { | ||||||||||||||||
$io->error($out); | ||||||||||||||||
return self::EXIT_CODE_ADDRESS_TAKEN_BY_ANOTHER_PROCESS; | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
if ($router !== null && !file_exists($router)) { | ||||||||||||||||
|
@@ -150,7 +198,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int | |||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
$xDebugInstalled = extension_loaded('xdebug'); | ||||||||||||||||
$xDebugEnabled = $isLinux && $xDebugInstalled && $input->hasOption('xdebug') && $input->getOption('xdebug') === null; | ||||||||||||||||
$xDebugEnabled = $isLinux && $xDebugInstalled && $input->hasOption('xdebug') && $input->getOption( | ||||||||||||||||
'xdebug' | ||||||||||||||||
) === null; | ||||||||||||||||
Comment on lines
+201
to
+203
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||
|
||||||||||||||||
if ($xDebugEnabled) { | ||||||||||||||||
$command[] = 'XDEBUG_MODE=debug XDEBUG_TRIGGER=yes'; | ||||||||||||||||
|
@@ -223,4 +273,9 @@ private function getRootPath(): string | |||||||||||||||
|
||||||||||||||||
return getcwd(); | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
private function isWindows(): bool | ||||||||||||||||
{ | ||||||||||||||||
return stripos(PHP_OS, 'Win') === 0; | ||||||||||||||||
} | ||||||||||||||||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.