From b3f8cc67af09697b5484e082c1e397306761ff58 Mon Sep 17 00:00:00 2001 From: andrew Date: Wed, 8 Nov 2023 14:56:10 -0500 Subject: [PATCH] Add a heartbeat to the Client Hits /clientHeartbeat/ when no data exists to report May return a stop header to abort the workload --- Client/Client.py | 34 ++++++++++++++++++++++---------- OpenBench/urls.py | 1 + OpenBench/views.py | 14 +++++++++++++ Templates/OpenBench/machine.html | 1 + 4 files changed, 40 insertions(+), 10 deletions(-) diff --git a/Client/Client.py b/Client/Client.py index 275c23d..3e3b258 100644 --- a/Client/Client.py +++ b/Client/Client.py @@ -385,6 +385,15 @@ class ServerReporter: return ServerReporter.report(config, 'clientSubmitResults', payload) + @staticmethod + def report_heartbeat(config): + + payload = { + 'test_id' : config.workload['test']['id'] + } + + return ServerReporter.report(config, 'clientHeartbeat', payload) + class Cutechess: ## Handles building the very long string of arguments that need to be passed @@ -629,8 +638,8 @@ class ResultsReporter(object): if result: self.pending.append(result) - # Send results every 30 seconds, until all Tasks are done - if not self.bulk and self.send_results(report_interval=REPORT_INTERVAL): + # Send results, or a heartbeat, every REPORT_INTERVAL seconds until done + if self.send_results(report_interval=REPORT_INTERVAL): return # Kill everything if openbench.exit is created @@ -646,21 +655,26 @@ class ResultsReporter(object): break # Send any remaining results immediately - self.send_results(report_interval=0) + self.send_results(report_interval=0, final_report=True) - def send_results(self, report_interval): + def send_results(self, report_interval, final_report=False): - # Nothing to send, or we are sending too frequently - if not self.pending or self.last_report + report_interval > time.time(): + # Do not send more often than report_interval dictates + if self.last_report + report_interval > time.time(): return False - # Most recent time we sent a report is now + # Most recent time we attempted to sent a report is now self.last_report = time.time() try: - # Send all of the queued Results at once - response = ServerReporter.report_results(self.config, self.pending).json() - self.pending = [] + + # Heartbeat when no results, or still awaiting bulk results + if not self.pending or (self.bulk and not final_report): + response = ServerReporter.report_heartbeat(self.config).json() + + else: # Send all of the queued Results at once + response = ServerReporter.report_results(self.config, self.pending).json() + self.pending = [] # If the test ended, kill all tasks if 'stop' in response: diff --git a/OpenBench/urls.py b/OpenBench/urls.py index 4a6e31c..3a7ccbf 100644 --- a/OpenBench/urls.py +++ b/OpenBench/urls.py @@ -78,6 +78,7 @@ urlpatterns = [ django.urls.path(r'clientSubmitNPS/', OpenBench.views.client_submit_nps), django.urls.path(r'clientSubmitError/', OpenBench.views.client_submit_error), django.urls.path(r'clientSubmitResults/', OpenBench.views.client_submit_results), + django.urls.path(r'clientHeartbeat/', OpenBench.views.client_heartbeat), # Nice endpoints, which can be hit from the website or with credentials cleanly django.urls.path(r'api/config/', OpenBench.views.api_configs), diff --git a/OpenBench/views.py b/OpenBench/views.py index b3e7694..e146773 100644 --- a/OpenBench/views.py +++ b/OpenBench/views.py @@ -43,6 +43,7 @@ from django.http import HttpResponse, JsonResponse from django.views.decorators.csrf import csrf_exempt from django.core.files.storage import FileSystemStorage from django.core.files.base import ContentFile +from django.utils import timezone # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # GENERAL UTILITIES # @@ -745,6 +746,19 @@ def client_submit_results(request): # Returns {}, or { 'stop' : True } return JsonResponse(OpenBench.utils.update_test(request, machine)) +@csrf_exempt +def client_heartbeat(request): + + # Pass along any error messages if they appear + machine, response = client_verify_worker(request) + if response != None: return response + + # Force a refresh of the updated timestamp + machine.save() + + # Include a 'stop' header iff the test was finished + test = Test.objects.get(id=int(request.POST['test_id'])) + return JsonResponse([{}, { 'stop' : True }][test.finished]) # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # diff --git a/Templates/OpenBench/machine.html b/Templates/OpenBench/machine.html index 9fafe34..0661481 100644 --- a/Templates/OpenBench/machine.html +++ b/Templates/OpenBench/machine.html @@ -17,6 +17,7 @@ Client Version{{machine.info.client_ver}} Python Version{{machine.info.python_ver}} System Version{{machine.info.os_name}} {{machine.info.os_ver}} + Last Seen{{machine.updated|date:'U'}}