Skip to content

Commit

Permalink
✨ notifier last gasp
Browse files Browse the repository at this point in the history
  • Loading branch information
juftin committed May 31, 2023
1 parent 9eaec51 commit 8619419
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 16 deletions.
19 changes: 15 additions & 4 deletions camply/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,18 @@
import sys
from dataclasses import dataclass
from datetime import date, timedelta
from typing import Any, Container, Dict, List, Optional, Sequence, Set, Tuple, Union
from typing import (
Any,
Container,
Dict,
List,
Optional,
Sequence,
Set,
Tuple,
Type,
Union,
)

import click
from rich import traceback
Expand All @@ -25,7 +36,7 @@
GoingToCampProvider,
RecreationDotGov,
)
from camply.search import CAMPSITE_SEARCH_PROVIDER
from camply.search import CAMPSITE_SEARCH_PROVIDER, BaseCampingSearch
from camply.utils import configure_camply, log_camply, make_list, yaml_utils
from camply.utils.general_utils import days_of_the_week_mapping, handle_search_windows
from camply.utils.logging_utils import log_sorted_response
Expand Down Expand Up @@ -709,8 +720,8 @@ def campsites(
"search_forever": search_forever,
"search_once": search_once,
}
provider_class = CAMPSITE_SEARCH_PROVIDER[provider]
camping_finder = provider_class(**provider_kwargs)
provider_class: Type[BaseCampingSearch] = CAMPSITE_SEARCH_PROVIDER[provider]
camping_finder: BaseCampingSearch = provider_class(**provider_kwargs)
camping_finder.get_matching_campsites(**search_kwargs)


Expand Down
20 changes: 20 additions & 0 deletions camply/notifications/multi_provider_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Default Notifier: Silent + Extras
"""

import datetime
import logging
from typing import Dict, List, Type, Union

Expand Down Expand Up @@ -105,3 +106,22 @@ def log_providers(self) -> None:
f"Only {self.providers[0]} enabled. "
"I hope you're watching these logs."
)

def last_gasp(self, error: Exception) -> None:
"""
Make a `last gasp` notification before exiting
Returns
-------
None
"""
logger.info("Exception encountered, emitting notification last gasp.")
date_string = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
error_string = str(error)
error_message = (
"camply encountered an error and exited 😟 "
f"[{date_string}] - ({error.__class__.__name__}) {error_string}"
)
for provider in self.providers:
provider.send_message(error_message)
raise RuntimeError(error_message) from error
29 changes: 17 additions & 12 deletions camply/search/base_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class BaseCampingSearch(ABC):

campgrounds: List[CampgroundFacility] = []
list_campsites_supported: bool = True
notifier: Optional[MultiNotifierProvider] = None

def __init__(
self,
Expand Down Expand Up @@ -341,11 +342,11 @@ def _continuous_search_retry(
polling_interval_minutes = self._get_polling_minutes(
polling_interval=polling_interval
)
notifier = MultiNotifierProvider(provider=notification_provider)
self.notifier = MultiNotifierProvider(provider=notification_provider)
logger.info(
f"Searching for campsites every {polling_interval_minutes} minutes. "
)
notifier.log_providers()
self.notifier.log_providers()
retryer = tenacity.Retrying(
retry=tenacity.retry_if_exception_type(CampsiteNotFoundError),
wait=tenacity.wait.wait_fixed(int(polling_interval_minutes) * 60),
Expand All @@ -366,7 +367,7 @@ def _continuous_search_retry(
logged_campsites = list(new_campsites)
self._handle_notifications(
retryer=retryer,
notifier=notifier,
notifier=self.notifier,
logged_campsites=logged_campsites,
continuous_search_attempts=continuous_search_attempts,
notify_first_try=notify_first_try,
Expand Down Expand Up @@ -569,15 +570,19 @@ def get_matching_campsites(
List[AvailableCampsite]
"""
if continuous is True or search_once is True:
self._search_campsites_continuous(
log=log,
verbose=verbose,
polling_interval=polling_interval,
notification_provider=notification_provider,
notify_first_try=notify_first_try,
search_forever=search_forever,
search_once=search_once,
)
try:
self._search_campsites_continuous(
log=log,
verbose=verbose,
polling_interval=polling_interval,
notification_provider=notification_provider,
notify_first_try=notify_first_try,
search_forever=search_forever,
search_once=search_once,
)
except Exception as e:
self.notifier.last_gasp(error=e)
raise e
else:
starting_count = len(self.campsites_found)
matching_campsites = self._search_matching_campsites_available(
Expand Down

0 comments on commit 8619419

Please sign in to comment.