diff --git a/.gitignore b/.gitignore index 84daff1..d79145a 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,7 @@ Media/* !Client/*.py !Client/cutechess-ob !Client/cutechess-ob.exe + +Media/PGNs/* +Media/Networks/* +Media/Events/* diff --git a/OpenBench/management/commands/runserver.py b/OpenBench/management/commands/runserver.py index b8d1daa..2f0aad6 100644 --- a/OpenBench/management/commands/runserver.py +++ b/OpenBench/management/commands/runserver.py @@ -19,6 +19,8 @@ # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # from OpenBench.watcher import ArtifactWatcher +from OpenBench.pgn_watcher import PGNWatcher + from django.core.management.commands.runserver import Command as BaseRunserverCommand class Command(BaseRunserverCommand): @@ -30,6 +32,8 @@ class Command(BaseRunserverCommand): def pre_start(self): self.watcher = ArtifactWatcher().start() + self.pgn_watcher = PGNWatcher().start() def pre_quit(self): - self.watcher.kill() \ No newline at end of file + self.watcher.kill() + self.pgn_watcher.kill() \ No newline at end of file diff --git a/OpenBench/models.py b/OpenBench/models.py index 4407e29..4bda7ad 100644 --- a/OpenBench/models.py +++ b/OpenBench/models.py @@ -201,6 +201,7 @@ class PGN(Model): test_id = IntegerField(default=0) result_id = IntegerField(default=0) book_index = IntegerField(default=0) + processed = BooleanField(default=False) def __str__(self): return self.filename() diff --git a/OpenBench/pgn_watcher.py b/OpenBench/pgn_watcher.py new file mode 100644 index 0000000..7ec5392 --- /dev/null +++ b/OpenBench/pgn_watcher.py @@ -0,0 +1,66 @@ +# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # +# # +# OpenBench is a chess engine testing framework authored by Andrew Grant. # +# # +# # +# OpenBench is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# OpenBench is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # + +import os +import sys +import tarfile +import threading +import time +import traceback + +from OpenBench.models import PGN + +from django.db import transaction +from django.core.files.base import ContentFile +from django.core.files.storage import FileSystemStorage + +class PGNWatcher(threading.Thread): + + def process_pgn(self, pgn): + + tar_path = FileSystemStorage('Media/PGNs').path('%d.pgn.tar' % (pgn.test_id)) + pgn_path = FileSystemStorage().path(pgn.filename()) + + with transaction.atomic(): + + # Ensure Media/PGNs exists + dir_name = os.path.dirname(tar_path) + if not os.path.exists(dir_name): + os.makedirs(dir_name) + + # First PGN will create the initial .tar file + mode = 'a' if os.path.exists(tar_path) else 'w' + with tarfile.open(tar_path, mode) as tar: + tar.add(pgn_path, arcname=pgn.filename()) + + # Delete the raw .pgn.bz2 file, and don't process it again + FileSystemStorage().delete(pgn.filename()) + pgn.processed = True + pgn.save() + + def run(self): + while True: + for pgn in PGN.objects.filter(processed=False): + try: + self.process_pgn(pgn) + except: + traceback.print_exc() + sys.stdout.flush() + time.sleep(15) diff --git a/OpenBench/views.py b/OpenBench/views.py index 1d13f2c..d2fe4f0 100644 --- a/OpenBench/views.py +++ b/OpenBench/views.py @@ -39,6 +39,7 @@ from OpenBench.models import * from django.contrib.auth.models import User from OpenSite.settings import MEDIA_ROOT +from django.db import transaction from django.db.models import F, Q from django.http import HttpResponse, JsonResponse from django.views.decorators.csrf import csrf_exempt @@ -762,15 +763,17 @@ def client_heartbeat(request, machine): @verify_worker def client_submit_pgn(request, machine): - # Format: test.result.book-index.pgn.bz2 - pgn = PGN() - pgn.test_id = int(request.POST['test_id'] ) - pgn.result_id = int(request.POST['result_id'] ) - pgn.book_index = int(request.POST['book_index']) - pgn.save() + with transaction.atomic(): - # Save the .pgn.bz2 to /Media/ - FileSystemStorage().save(pgn.filename(), ContentFile(request.FILES['file'].read())) + # Format: test.result.book-index.pgn.bz2 + pgn = PGN() + pgn.test_id = int(request.POST['test_id'] ) + pgn.result_id = int(request.POST['result_id'] ) + pgn.book_index = int(request.POST['book_index']) + pgn.save() + + # Save the .pgn.bz2 to /Media/ + FileSystemStorage().save(pgn.filename(), ContentFile(request.FILES['file'].read())) return JsonResponse({})