Skip to content

Commit

Permalink
Catch some errors (#1280)
Browse files Browse the repository at this point in the history
  • Loading branch information
springfall2008 authored Jul 3, 2024
1 parent f6d8578 commit 2202490
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions apps/predbat/predbat.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import asyncio
import json

THIS_VERSION = "v8.2.3"
THIS_VERSION = "v8.2.4"
PREDBAT_FILES = ["predbat.py", "config.py", "prediction.py", "utils.py", "inverter.py", "ha.py", "download.py", "unit_test.py"]
from download import predbat_update_move, predbat_update_download, check_install

Expand Down Expand Up @@ -790,6 +790,7 @@ def download_solcast_data(self):
self.solcast_data = json.load(f)
except Exception as e:
self.log("Warn: Error loading Solcast cache file {}".format(e))
self.log("Warn: " + traceback.format_exc())
os.remove(cache_file)

if isinstance(api_keys, str):
Expand Down Expand Up @@ -5003,6 +5004,7 @@ def reset(self):
self.metric_inday_adjust_damping = 1.0
self.metric_standing_charge = 0.0
self.metric_self_sufficiency = 0.0
self.iboost_value_scaling = 1.0
self.rate_import = {}
self.rate_export = {}
self.rate_gas = {}
Expand Down Expand Up @@ -5446,7 +5448,7 @@ def launch_run_prediction_single(self, charge_limit, charge_window, discharge_wi
"""
charge_limit = copy.deepcopy(charge_limit)
discharge_limits = copy.deepcopy(discharge_limits)
if self.pool:
if self.pool and self.pool._state == "RUN":
han = self.pool.apply_async(wrapped_run_prediction_single, (charge_limit, charge_window, discharge_window, discharge_limits, pv10, end_record, step))
else:
han = DummyThread(self.prediction.thread_run_prediction_single(charge_limit, charge_window, discharge_window, discharge_limits, pv10, end_record, step))
Expand All @@ -5456,7 +5458,7 @@ def launch_run_prediction_charge(self, loop_soc, window_n, charge_limit, charge_
"""
Launch a thread to run a prediction
"""
if self.pool:
if self.pool and self.pool._state == "RUN":
han = self.pool.apply_async(
wrapped_run_prediction_charge, (loop_soc, window_n, charge_limit, charge_window, discharge_window, discharge_limits, pv10, all_n, end_record)
)
Expand All @@ -5470,7 +5472,7 @@ def launch_run_prediction_discharge(self, this_discharge_limit, start, window_n,
"""
Launch a thread to run a prediction
"""
if self.pool:
if self.pool and self.pool._state == "RUN":
han = self.pool.apply_async(
wrapped_run_prediction_discharge,
(this_discharge_limit, start, window_n, try_charge_limit, charge_window, try_discharge_window, try_discharge, pv10, all_n, end_record),
Expand Down Expand Up @@ -8063,8 +8065,12 @@ def calculate_plan(self, recompute=True):

# Destroy pool
if self.pool:
self.pool.close()
self.pool.join()
try:
self.pool.close()
self.pool.join()
except Exception as e:
self.log("Warn: failed to close thread pool: {}".format(e))
self.log("Warn: " + traceback.format_exc())
self.pool = None
# Return if we recomputed or not
return recompute
Expand Down Expand Up @@ -9824,8 +9830,12 @@ def download_predbat_version(self, version):
self.stop_thread = True
if self.pool:
self.log("Warn: Killing current threads before update...")
self.pool.close()
self.pool.join()
try:
self.pool.close()
self.pool.join()
except Exception as e:
self.log("Warn: Failed to close thread pool: {}".format(e))
self.log("Warn: " + traceback.format_exc())
self.pool = None

# Notify that we are about to update
Expand Down Expand Up @@ -10654,6 +10664,7 @@ def initialize(self):
self.load_user_config(quiet=False, register=True)
except Exception as e:
self.log("Error: Exception raised {}".format(e))
self.log("Error: " + traceback.format_exc())
self.record_status("Error: Exception raised {}".format(e))
raise e

Expand Down Expand Up @@ -10700,8 +10711,12 @@ async def terminate(self):
await asyncio.sleep(0)
if hasattr(self, "pool"):
if self.pool:
self.pool.close()
self.pool.join()
try:
self.pool.close()
self.pool.join()
except Exception as e:
self.log("Warn: Failed to close thread pool {}".format(e))
self.log("Warn: " + traceback.format_exc())
self.pool = None
self.log("Predbat terminated")

Expand Down

0 comments on commit 2202490

Please sign in to comment.