Skip to content

Allow app to track callback change #2206

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 5 commits into from
May 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions appdaemon/adbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ def __init__(self, ad: "AppDaemon", config_model: "AppConfig"):

self.namespace = "default"
self.dashboard_dir = None
self.callback_counter = 0

if self.AD.http is not None:
self.dashboard_dir = self.AD.http.dashboard_dir
Expand Down
16 changes: 16 additions & 0 deletions appdaemon/threads.py
Original file line number Diff line number Diff line change
Expand Up @@ -976,6 +976,7 @@ async def async_worker(self, args): # noqa: C901
async def safe_callback():
"""Wraps actually calling the function for the callback with logic to transform exceptions based
on the callback type"""
increment_callback_counter(app, name)
try:
await funcref()
except Exception as exc:
Expand Down Expand Up @@ -1069,6 +1070,7 @@ def worker(self): # noqa: C901
def safe_callback():
"""Wraps actually calling the function for the callback with logic to transform exceptions based
on the callback type"""
increment_callback_counter(app, name)
try:
funcref()
except Exception as exc:
Expand Down Expand Up @@ -1169,3 +1171,17 @@ def report_callback_sig(self, name, type, funcref, args):
"Logged an error to %s",
self.AD.logging.get_filename("error_log"),
)

def increment_callback_counter(app, name):
try:
# This function may be called concurrently and the GIL won't protect us against
# races during app.callback_counter += 1 so we need to use a lock. We'll just use that of the app.
with app.lock:
app.callback_counter += 1
except Exception:
error_logger = logging.getLogger("Error.{}".format(name))
error_logger.warning("-" * 60)
error_logger.warning("Unexpected error in worker for App %s:", name)
error_logger.warning("-" * 60)
error_logger.warning(traceback.format_exc())
error_logger.warning("-" * 60)
Loading