Skip to content

Commit c292e60

Browse files
authored
Merge pull request #2206 from Ten0/app_track_callback_change
Allow app to track callback change
2 parents 5b35318 + 2d18175 commit c292e60

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

appdaemon/adbase.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ def __init__(self, ad: "AppDaemon", config_model: "AppConfig"):
107107

108108
self.namespace = "default"
109109
self.dashboard_dir = None
110+
self.callback_counter = 0
110111

111112
if self.AD.http is not None:
112113
self.dashboard_dir = self.AD.http.dashboard_dir

appdaemon/threads.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,6 +1002,7 @@ async def async_worker(self, args): # noqa: C901
10021002
async def safe_callback():
10031003
"""Wraps actually calling the function for the callback with logic to transform exceptions based
10041004
on the callback type"""
1005+
increment_callback_counter(app, name)
10051006
try:
10061007
await funcref()
10071008
except Exception as exc:
@@ -1095,6 +1096,7 @@ def worker(self): # noqa: C901
10951096
def safe_callback():
10961097
"""Wraps actually calling the function for the callback with logic to transform exceptions based
10971098
on the callback type"""
1099+
increment_callback_counter(app, name)
10981100
try:
10991101
funcref()
11001102
except Exception as exc:
@@ -1195,3 +1197,17 @@ def report_callback_sig(self, name, type, funcref, args):
11951197
"Logged an error to %s",
11961198
self.AD.logging.get_filename("error_log"),
11971199
)
1200+
1201+
def increment_callback_counter(app, name):
1202+
try:
1203+
# This function may be called concurrently and the GIL won't protect us against
1204+
# races during app.callback_counter += 1 so we need to use a lock. We'll just use that of the app.
1205+
with app.lock:
1206+
app.callback_counter += 1
1207+
except Exception:
1208+
error_logger = logging.getLogger("Error.{}".format(name))
1209+
error_logger.warning("-" * 60)
1210+
error_logger.warning("Unexpected error in worker for App %s:", name)
1211+
error_logger.warning("-" * 60)
1212+
error_logger.warning(traceback.format_exc())
1213+
error_logger.warning("-" * 60)

0 commit comments

Comments
 (0)