Skip to content

Commit

Permalink
add log for exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
clavay authored and trombastic committed Jan 30, 2024
1 parent 101a41d commit 6efa7e7
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 42 deletions.
10 changes: 4 additions & 6 deletions pyscada/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ def last_value(self, instance):
except ValueError as e:
return f"ValueError {e} - with timestamp {v.timestamp_old} : {v.prev_value.__str__()} {instance.unit.unit}"
except Variable.DoesNotExist:
pass
return "Variable does not exist"
except TimeoutError:
return "Timeout on value query"
return f" - : NaN {instance.unit.unit}"
Expand Down Expand Up @@ -334,10 +334,8 @@ class DeviceAdmin(admin.ModelAdmin):
app_name__in=settings.INSTALLED_APPS
):
protocol_list.append(protocol.protocol)
except ProgrammingError:
pass
except OperationalError:
pass
except (ProgrammingError, OperationalError) as e:
logger.debug(e)

def formfield_for_foreignkey(self, db_field, request, **kwargs):
# For new device, show all the protocols from the installed apps in settings.py
Expand Down Expand Up @@ -460,7 +458,7 @@ def last_value(self, instance):
+ instance.unit.unit
)
except Variable.DoesNotExist:
pass
return "Variable does not exist"
return " - : NaN " + instance.unit.unit

def color_code(self, instance):
Expand Down
4 changes: 2 additions & 2 deletions pyscada/hmi/migrations/0043_auto_20201201_1411.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ def fullname(o):
Widget.objects.filter(content=wcxy).update(content=wc)
if wcxy is not None:
wcxy.delete()
except:
pass
except Exception as e:
logger.info(e)


def move_xy_chart(apps, schema_editor):
Expand Down
4 changes: 2 additions & 2 deletions pyscada/hmi/migrations/0056_auto_20211210_1608.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ def add_height_width(apps, schema_editor):
item.url_width = int(img.width)
item.save()
count += 1
except:
pass
except Exception as e:
logger.info(e)

logger.info("changed %d ProcessFlowDiagram\n" % count)

Expand Down
8 changes: 4 additions & 4 deletions pyscada/hmi/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ def validate_tempalte(value):
raise ValidationError(
_("Template filename not found."),
)
except TemplateSyntaxError:
pass
except TemplateSyntaxError as e:
logger.info(e)


# raise a ValidationError if value not endswith .html or if template not found
Expand Down Expand Up @@ -228,8 +228,8 @@ def check_all_themes(self):
get_template(theme.base_filename + ".html").render()
except TemplateDoesNotExist:
theme.delete()
except TemplateSyntaxError:
pass
except TemplateSyntaxError as e:
logger.info(e)


class ControlElementOption(models.Model):
Expand Down
40 changes: 26 additions & 14 deletions pyscada/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1905,8 +1905,8 @@ def _update_value(self, value=None, timestamp=None):
"Value read for %s format not supported : %s" % (self, type(value))
)
value = None
except TypeError:
pass
except TypeError as e:
logger.debug(e)

if (
self.scaling is None
Expand Down Expand Up @@ -2278,8 +2278,8 @@ def get_protocol_variable(self):
and getattr(self, v.name).protocol_id == self.device.protocol.id
):
return getattr(self, v.name)
except ProgrammingError:
pass
except (ProgrammingError, OperationalError) as e:
logger.debug(e)
return None


Expand Down Expand Up @@ -2359,8 +2359,8 @@ def create_and_notificate(self, dwts):
AttributeError,
ConnectionRefusedError,
InvalidChannelLayerError,
):
pass
) as e:
logger.debug(e)


class DeviceReadTask(models.Model):
Expand Down Expand Up @@ -2442,8 +2442,8 @@ def create_and_notificate(self, drts):
AttributeError,
ConnectionRefusedError,
InvalidChannelLayerError,
):
pass
) as e:
logger.debug(e)


class RecordedDataOld(models.Model):
Expand Down Expand Up @@ -2498,7 +2498,9 @@ def __init__(self, *args, **kwargs):
kwargs["value_int64"] = int(kwargs.pop("value"))
if kwargs["value_int64"].bit_length() > 64:
# todo throw exeption or do anything
pass
logger.warning(
f"Variable {Variable.objects.get(id=variable_id)} read value bit length is {kwargs['value_int64'].bit_length()} > 64"
)
elif kwargs["variable"].value_class.upper() in [
"WORD",
"UINT",
Expand All @@ -2508,7 +2510,9 @@ def __init__(self, *args, **kwargs):
kwargs["value_int32"] = int(kwargs.pop("value"))
if kwargs["value_int32"].bit_length() > 32:
# todo throw exeption or do anything
pass
logger.warning(
f"Variable {Variable.objects.get(id=variable_id)} read value bit length is {kwargs['value_int32'].bit_length()} > 32"
)
elif kwargs["variable"].value_class.upper() in [
"INT16",
"INT8",
Expand All @@ -2518,7 +2522,9 @@ def __init__(self, *args, **kwargs):
kwargs["value_int16"] = int(kwargs.pop("value"))
if kwargs["value_int16"].bit_length() > 15:
# todo throw exeption or do anything
pass
logger.warning(
f"Variable {Variable.objects.get(id=variable_id)} read value bit length is {kwargs['value_int16'].bit_length()} > 16"
)

elif kwargs["variable"].value_class.upper() in ["BOOL", "BOOLEAN"]:
kwargs["value_boolean"] = bool(kwargs.pop("value"))
Expand Down Expand Up @@ -2640,7 +2646,9 @@ def __init__(self, *args, **kwargs):
kwargs["value_int64"] = int(kwargs.pop("value"))
if kwargs["value_int64"].bit_length() > 64:
# todo throw exeption or do anything
pass
logger.warning(
f"Variable {Variable.objects.get(id=variable_id)} read value bit length is {kwargs['value_int64'].bit_length()} > 64"
)
elif kwargs["variable"].value_class.upper() in [
"WORD",
"UINT",
Expand All @@ -2650,7 +2658,9 @@ def __init__(self, *args, **kwargs):
kwargs["value_int32"] = int(kwargs.pop("value"))
if kwargs["value_int32"].bit_length() > 32:
# todo throw exeption or do anything
pass
logger.warning(
f"Variable {Variable.objects.get(id=variable_id)} read value bit length is {kwargs['value_int32'].bit_length()} > 32"
)
elif kwargs["variable"].value_class.upper() in [
"INT16",
"INT8",
Expand All @@ -2660,7 +2670,9 @@ def __init__(self, *args, **kwargs):
kwargs["value_int16"] = int(kwargs.pop("value"))
if kwargs["value_int16"].bit_length() > 15:
# todo throw exeption or do anything
pass
logger.warning(
f"Variable {Variable.objects.get(id=variable_id)} read value bit length is {kwargs['value_int16'].bit_length()} > 16"
)

elif kwargs["variable"].value_class.upper() in ["BOOL", "BOOLEAN"]:
kwargs["value_boolean"] = bool(kwargs.pop("value"))
Expand Down
37 changes: 23 additions & 14 deletions pyscada/utils/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,8 +371,8 @@ def start(self):
for process in BackgroundProcess.objects.filter(done=False, pid__gt=0):
try:
kill(process.pid, 9)
except:
pass
except Exception as e:
logger.debug(f"{e} for pid {process.pid}")
# Init the DB
if not self.init_db():
logger.debug("Init DB failed\n")
Expand Down Expand Up @@ -456,7 +456,9 @@ def run(self):
raise StopIteration
elif sig == signal.SIGHUP:
# todo handle sighup
pass
logger.debug(
f"Received signal.SIGHUP for {self.label}. Nothing to do."
)
elif sig == signal.SIGUSR1:
# restart all child processes
logger.debug(
Expand All @@ -467,7 +469,9 @@ def run(self):
elif sig == signal.SIGUSR2:
# write the process status to stdout
self.status()
pass
logger.debug(
f"Received signal.SIGUSR2 for {self.label}. Writting the process status to stdout."
)
close_db_connection()
sleep(5)
except StopIteration:
Expand Down Expand Up @@ -509,8 +513,8 @@ def manage_processes(self):
if process.pk not in self.PROCESSES or time() > timeout:
try:
self.kill_process(process.pk, signal.SIGKILL)
except:
pass
except Exception as e:
logger.debug(e)
break
self.kill_process(process.pk)
sleep(1)
Expand Down Expand Up @@ -546,8 +550,8 @@ def manage_processes(self):

try:
self.PROCESSES.pop(process.process_id)
except:
pass
except Exception as e:
logger.debug(e)
# process is dead, delete process
if process.parent_process_id == self.process_id:
p = BackgroundProcess.objects.filter(
Expand Down Expand Up @@ -839,7 +843,7 @@ def run(self):
# raise StopIteration
exec_loop = False
else:
pass
logger.info(f"Unknown {self.label} loop status : {status}")
elif sig is None:
continue
elif sig not in self.SIGNALS:
Expand Down Expand Up @@ -1018,9 +1022,10 @@ def signal(self, signum=None, frame=None):
+ "_ProcessAction_for_"
+ str(self.process_id)
)
pass
except (RuntimeWarning, RuntimeError):
pass
except (RuntimeWarning, RuntimeError) as e:
logger.debug(
f"Failed to send {message} ProcessSignal {signum} : {e}"
)

def stop(self, signum=None, frame=None):
"""
Expand Down Expand Up @@ -1168,7 +1173,9 @@ def restart(self):
process["failed"] -= 1
self.processes.remove(process)
except BackgroundProcess.DoesNotExist:
pass
logger.debug(
f"BackgroundProcess for {self.label} does not exist : failed to restart."
)
except:
logger.debug(
"%s, unhandled exception\n%s" % (self.label, traceback.format_exc())
Expand Down Expand Up @@ -1309,7 +1316,9 @@ def restart(self):
process["failed"] -= 1
self.processes.remove(process)
except BackgroundProcess.DoesNotExist:
pass
logger.debug(
f"BackgroundProcess for {self.label} does not exist : failed to restart."
)
except:
logger.debug(
"%s, unhandled exception\n%s" % (self.label, traceback.format_exc())
Expand Down

0 comments on commit 6efa7e7

Please sign in to comment.