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

Handle start before DHCP resolution #583

Merged
merged 2 commits into from
Jan 5, 2024
Merged

Handle start before DHCP resolution #583

merged 2 commits into from
Jan 5, 2024

Conversation

linknum23
Copy link
Contributor

@linknum23 linknum23 commented Dec 21, 2023

Fixes #432

Checklist

  • Have you checked to ensure there aren't other open Pull Requests for the same update/change?
  • Update the CHANGELOG
  • Does your submission pass linting & tests? You can test on localhost using ./scripts/test

Also try and include #579

@linknum23
Copy link
Contributor Author

Note: this was copied from #433

@codecov-commenter
Copy link

codecov-commenter commented Dec 22, 2023

Codecov Report

Attention: 76 lines in your changes are missing coverage. Please review.

Comparison is base (f1a39a6) 51.50% compared to head (377cc35) 50.90%.

Files Patch % Lines
amplipi/zeroconf.py 21.11% 71 Missing ⚠️
amplipi/asgi.py 0.00% 5 Missing ⚠️

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #583      +/-   ##
==========================================
- Coverage   51.50%   50.90%   -0.60%     
==========================================
  Files          24       25       +1     
  Lines        5677     5663      -14     
==========================================
- Hits         2924     2883      -41     
- Misses       2753     2780      +27     
Flag Coverage Δ
unittests 50.90% <24.00%> (-0.60%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@linknum23
Copy link
Contributor Author

@Lohrer I believe #579 is solved as well.

As for testing:

This will need some mileage before merging. Specifically looking at logs after a restart, logs after yanking the ethernet interface, other similar tests.

Copy link
Collaborator

@Lohrer Lohrer left a comment

Choose a reason for hiding this comment

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

I'd like to test a thing or two on a real AmpliPi but long story short my AmpliPi is down due to not being able to build the web app. This looks like a good improvement so far though. Unfortunately this does not solve #579 as-is.

amplipi/app.py Outdated Show resolved Hide resolved
amplipi/app.py Outdated Show resolved Hide resolved
@linknum23 linknum23 marked this pull request as ready for review December 29, 2023 20:41
@linknum23
Copy link
Contributor Author

Alright I think this thing is ready!

Here's the new changes:

  • _find_best_iface() now prefers the interface default gateway
  • MDNS advertisement was migrated to its own file (it was completely separate from app so this was not a big deal)
  • built-in python logging was added as a test, we will likely use this throughout AmpliPi in the future, for now it works as a drop in replacement

This was tested on the following sequence:

  1. Boot AmpliPi connected to the main network
  2. Disconnect
  3. Connect again
  4. Disconnect
  5. Connect over laptop to link local connection
  6. Disconnect
  7. Connect to main network again
  8. Disconnect
  9. Reboot
  10. Wait for system to start +1min
  11. Connect to main network again

On each new connection the advertisement was able to identify its change of state and advertise its new ip address (if it had one) if it didnt have one it failed and waited 30 seconds to retry

@linknum23 linknum23 requested a review from Lohrer December 29, 2023 21:07
@linknum23 linknum23 linked an issue Dec 29, 2023 that may be closed by this pull request
amplipi/mdns.py Outdated Show resolved Hide resolved
@linknum23 linknum23 requested a review from rtertiaer January 2, 2024 16:26
@linknum23
Copy link
Contributor Author

@rtertiaer Can you check out how I used logging here? Also make sure the interface resolution make sense?

amplipi/mdns.py Outdated
if ok():
new_ip_addr, _, new_iface = _find_best_iface(ok)
if new_ip_addr != ip_addr:
log.info(f'IP address changed from {ip_addr} ({good_iface}) to {new_ip_addr} ({new_iface})')
Copy link
Collaborator

Choose a reason for hiding this comment

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

pylint is warning about f-strings in logging. Seems like the TLDR is it's more performant to pass log.info('Message %s', variable) since then if the logging level is changed the f-string doesn't have to be evaluated. It also means if an error occurs during evaluation of the f-string it gets logged. I suspect these aren't good enough reasons for us to have a second method of string formatting to be remembered, so I added disable=logging-fstring-interpolation to our .pylintrc. Feel free to remove this override and fix the logging strings if you feel otherwise. See here for some of the discussion on this: pylint-dev/pylint#1788

Copy link
Contributor

Choose a reason for hiding this comment

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

so I added disable[...]

👏

amplipi/mdns.py Outdated
Comment on lines 33 to 38
# TEST: use logging in this module
log = logging.getLogger(__name__)
log.setLevel(logging.INFO)
sh = logging.StreamHandler(sys.stderr)
sh.setFormatter(logging.Formatter('%(name)s: %(levelname)s - %(message)s'))
log.addHandler(sh)
Copy link
Contributor

Choose a reason for hiding this comment

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

😎

amplipi/mdns.py Outdated
if ok():
new_ip_addr, _, new_iface = _find_best_iface(ok)
if new_ip_addr != ip_addr:
log.info(f'IP address changed from {ip_addr} ({good_iface}) to {new_ip_addr} ({new_iface})')
Copy link
Contributor

Choose a reason for hiding this comment

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

so I added disable[...]

👏


# TEST: use logging in this module
log = logging.getLogger(__name__)
log.setLevel(logging.INFO)
Copy link
Contributor

@rtertiaer rtertiaer Jan 2, 2024

Choose a reason for hiding this comment

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

nitpick: I think about the only thing I might change about the logging would be to either:

  1. not set the log level at all. this defaults to logging.WARNING, but permits us to set the log level elsewhere/at the root logger (notably during app startup). Should we fully migrate to ussing logging at some point, we'll probably poke this then, which is why this is a nitpick, OR
  2. use an environment variable to expose this straight away and set a default to logging.INFO when it is unset. I like this less, but gives us some flexibility straight away.

personally? I'd probably just leave this as-is for now til we migrate all print statements, and then opt for option 1.

@Lohrer Lohrer force-pushed the issue-432 branch 2 times, most recently from fbd42b9 to 9d18f30 Compare January 2, 2024 18:55
@linknum23 linknum23 requested a review from Lohrer January 2, 2024 19:01
- use the interface connected to the default gateway

This avoids hardcoding the pi's interfaces and should make the MDNS advertisement test work more reliably on our test computers
@linknum23 linknum23 merged commit 8625571 into main Jan 5, 2024
3 checks passed
@linknum23 linknum23 deleted the issue-432 branch January 5, 2024 15:05
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.

Fix service advertisement interface selection Handle start before DHCP resolution
4 participants