Skip to content

Commit aae6fce

Browse files
committed
Make sender monitor run optional
1 parent 60d3689 commit aae6fce

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

simvue/sender/actions.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -186,15 +186,16 @@ def _single_item_upload(
186186
id_mapping: dict[str, str],
187187
cache_directory: pathlib.Path,
188188
thread_lock: threading.Lock,
189-
simvue_monitor_run: SimvueRun,
189+
simvue_monitor_run: SimvueRun | None,
190190
*,
191191
throw_exceptions: bool = False,
192192
retry_failed: bool = False,
193193
upload_status: dict[str, str | float] | None,
194194
) -> None:
195195
"""Upload a single item of this object type."""
196196
_label: str = cls.object_type[:-1] if cls.singular_object else cls.object_type
197-
simvue_monitor_run.log_event(f"Uploading {_label} '{identifier}'")
197+
if simvue_monitor_run:
198+
simvue_monitor_run.log_event(f"Uploading {_label} '{identifier}'")
198199
_json_file = cache_directory.joinpath(f"{cls.object_type}/{identifier}.json")
199200

200201
with _json_file.open() as in_f:
@@ -230,10 +231,11 @@ def _single_item_upload(
230231
_exception_msg: str = (
231232
f"Error while committing {_label} '{identifier}': {err}"
232233
)
233-
simvue_monitor_run.log_event(_exception_msg)
234-
simvue_monitor_run.log_alert(
235-
name="sender_object_upload_failure", state="critical"
236-
)
234+
if simvue_monitor_run:
235+
simvue_monitor_run.log_event(_exception_msg)
236+
simvue_monitor_run.log_alert(
237+
name="sender_object_upload_failure", state="critical"
238+
)
237239
cls.logger.error(_exception_msg)
238240
cls._log_upload_failed(cache_directory, identifier, _data)
239241
return
@@ -266,9 +268,10 @@ def _single_item_upload(
266268
with thread_lock:
267269
upload_status.setdefault(cls.object_type, 0)
268270
upload_status[cls.object_type] += 1
269-
simvue_monitor_run.log_metrics(
270-
{f"uploads.{cls.object_type}": upload_status[cls.object_type]}
271-
)
271+
if simvue_monitor_run:
272+
simvue_monitor_run.log_metrics(
273+
{f"uploads.{cls.object_type}": upload_status[cls.object_type]}
274+
)
272275

273276
cls.post_tasks(
274277
offline_id=identifier,
@@ -285,7 +288,7 @@ def upload(
285288
thread_lock: threading.Lock,
286289
threading_threshold: int,
287290
max_thread_workers: int,
288-
simvue_monitor_run: SimvueRun,
291+
simvue_monitor_run: SimvueRun | None,
289292
*,
290293
throw_exceptions: bool = False,
291294
retry_failed: bool = False,
@@ -306,6 +309,8 @@ def upload(
306309
the number of cached files above which threading will be used.
307310
max_thread_workers : int
308311
the maximum number of threads to use.
312+
simvue_monitor_run : SimvueRun | None
313+
run to track uploading
309314
throw_exceptions : bool, optional
310315
whether to throw exceptions and terminate, default False.
311316
retry_failed : bool, optional
@@ -934,7 +939,7 @@ def _single_item_upload(
934939
id_mapping: dict[str, str],
935940
cache_directory: pathlib.Path,
936941
thread_lock: threading.Lock,
937-
simvue_monitor_run: SimvueRun,
942+
simvue_monitor_run: SimvueRun | None,
938943
*,
939944
throw_exceptions: bool = False,
940945
retry_failed: bool = False,
@@ -1032,7 +1037,7 @@ def _single_item_upload(
10321037
id_mapping: dict[str, str],
10331038
cache_directory: pathlib.Path,
10341039
thread_lock: threading.Lock,
1035-
simvue_monitor_run: SimvueRun,
1040+
simvue_monitor_run: SimvueRun | None,
10361041
*,
10371042
throw_exceptions: bool = False,
10381043
retry_failed: bool = False,
@@ -1057,7 +1062,7 @@ def upload(
10571062
throw_exceptions: bool = False,
10581063
retry_failed: bool = False,
10591064
upload_status: dict[str, str | float] | None = None,
1060-
simvue_monitor_run: dict[str, str | float] | None = None,
1065+
simvue_monitor_run: SimvueRun | None = None,
10611066
) -> None:
10621067
"""Upload CO2 intensity data."""
10631068
_ = id_mapping

simvue/sender/base.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ def __init__(
4848
retry_failed_uploads: bool = False,
4949
run_notification: typing.Literal["none", "all", "email"] = "none",
5050
run_retention_period: str | None = None,
51+
monitor_uploads: bool = False,
5152
) -> None:
5253
"""Initialise a local data sender.
5354
@@ -64,6 +65,8 @@ def __init__(
6465
default is False (exceptions will be logged)
6566
retry_failed_uploads : bool, optional
6667
Whether to retry sending objects which previously failed, by default False
68+
monitor_uploads : bool, optional
69+
Whether to track uploads as a Simvue run, by default False
6770
"""
6871
_local_config: SimvueConfiguration = SimvueConfiguration.fetch(mode="online")
6972
self._cache_directory = cache_directory or _local_config.offline.cache
@@ -77,6 +80,7 @@ def __init__(
7780
self._run_notification: typing.Literal["none", "email"] = run_notification
7881
self._run_retention_period: str | None = run_retention_period
7982
self._upload_status: dict[str, str | float] = {}
83+
self._monitor_uploads: bool = monitor_uploads
8084
self._id_mapping = {
8185
file_path.name.split(".")[0]: file_path.read_text()
8286
for file_path in self._cache_directory.glob("server_ids/*.txt")
@@ -149,7 +153,8 @@ def upload(self, objects_to_upload: list[UploadItem] | None = None) -> None:
149153
"""
150154
self._lock()
151155

152-
_monitor_run = self._initialise_monitor_run()
156+
_monitor_run = self._initialise_monitor_run() if self._monitor_uploads else None
157+
153158
self._upload_status = {}
154159

155160
for action in UPLOAD_ACTION_ORDER:
@@ -171,5 +176,6 @@ def upload(self, objects_to_upload: list[UploadItem] | None = None) -> None:
171176
simvue_monitor_run=_monitor_run,
172177
upload_status=self._upload_status,
173178
)
174-
_monitor_run.close()
179+
if _monitor_run:
180+
_monitor_run.close()
175181
self._release()

0 commit comments

Comments
 (0)