Auto consolidate .pgn.bz2 files into a .tar archive

This commit is contained in:
andrew committed 2023-12-11 12:42:03 -05:00
1 parent a280e63dba
commit 468281d52e
5 files changed
+87 -9

No files matched your search

+4
View File
@@ -14,3 +14,7 @@ Media/*
!Client/*.py
!Client/cutechess-ob
!Client/cutechess-ob.exe
Media/PGNs/*
Media/Networks/*
Media/Events/*
+5 -1
View File
@@ -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()
self.watcher.kill()
self.pgn_watcher.kill()
+1
View File
@@ -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()
+66
View File
@@ -0,0 +1,66 @@
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# #
# OpenBench is a chess engine testing framework authored by Andrew Grant. #
# <https://github.com/AndyGrant/OpenBench> <andrew@grantnet.us> #
# #
# 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 <http://www.gnu.org/licenses/>. #
# #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
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)
+11 -8
View File
@@ -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({})