Skip to content
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

Detect server start/stop more reliably. #1965

Merged
merged 1 commit into from
Dec 18, 2022

Conversation

janl
Copy link
Contributor

@janl janl commented Dec 18, 2022

This is useful, especially in testing, since your test hosts might not reliabliy start the server within two seconds, so we do a definite check before progressing.

By the same token, after kill $server_pid wait for the pid to be gone from the process list.

Since now the script can end prematurely, I also added a cleanup function to make sure the temporary certs are removed in any case.

n.b. this could also be done with the use of trap 'fn' ERR but that felt like a bit too much magic for this short a script.


📚 Documentation preview 📚: https://datasette--1965.org.readthedocs.build/en/1965/

@janl janl force-pushed the test/more-reliable-wait branch 3 times, most recently from 0dc23e2 to 1fccdd1 Compare December 18, 2022 11:45
This is useful, especially in testing, since your test
hosts might not reliabliy start the server within two
seconds, so we do a definite check before progressing.

By the same token, after `kill $server_pid` wait for
the pid to be gone from the process list.

Since now the script can end prematurely, I also added
a cleanup function to make sure the temporary certs are
removed in any case.

n.b. this could also be done with the use of `trap 'fn'
ERR` but that felt like a bit too much magic for this
short a script.
@simonw
Copy link
Owner

simonw commented Dec 18, 2022

This is great, thank you!

@simonw
Copy link
Owner

simonw commented Dec 18, 2022

Will link to this from my TIL shortly.

@simonw simonw merged commit e03aed0 into simonw:main Dec 18, 2022
@janl janl deleted the test/more-reliable-wait branch December 18, 2022 16:11
simonw added a commit to simonw/til that referenced this pull request Dec 18, 2022
# | | don’t match on the previous grep
# | | | we don’t need the output
# | | | |
until ( ! ps ax | grep $server_pid | grep -v grep > /dev/null ); do
Copy link

Choose a reason for hiding this comment

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

This can be simplified in a few different ways:

until ! ps -p $server_pid > /dev/null; do

until ! [ -e /proc/$server_pid ]; do

until ! kill -0 $server_pid 2>/dev/null; do

These have varying degrees of portability - don't have a Mac to hand, but I'm guessing they all work on all modern systems. They all assume that $server_pid cannot be the empty string, but I think that's safe in this case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants