Codestyle enhancements

This commit is contained in:
Patrick Jentsch 2024-11-07 08:57:32 +01:00
parent fcb49025e9
commit c223f07289
3 changed files with 27 additions and 32 deletions

View File

@ -1,2 +1,2 @@
from .handle_corpora import job as handle_corpora from .handle_corpora import handle_corpora
from .handle_jobs import job as handle_jobs from .handle_jobs import handle_jobs

View File

@ -1,12 +1,12 @@
from app import db, docker_client, scheduler
from app.models import Corpus, CorpusStatus
from flask import current_app from flask import current_app
import docker import docker
import os import os
import shutil import shutil
from app import db, docker_client, scheduler
from app.models import Corpus, CorpusStatus
def job(): def handle_corpora():
with scheduler.app.app_context(): with scheduler.app.app_context():
_handle_corpora() _handle_corpora()
@ -21,14 +21,14 @@ def _handle_corpora():
for corpus in [x for x in corpora if x.status == CorpusStatus.RUNNING_ANALYSIS_SESSION and x.num_analysis_sessions == 0]: for corpus in [x for x in corpora if x.status == CorpusStatus.RUNNING_ANALYSIS_SESSION and x.num_analysis_sessions == 0]:
corpus.status = CorpusStatus.CANCELING_ANALYSIS_SESSION corpus.status = CorpusStatus.CANCELING_ANALYSIS_SESSION
for corpus in [x for x in corpora if x.status == CorpusStatus.RUNNING_ANALYSIS_SESSION]: for corpus in [x for x in corpora if x.status == CorpusStatus.RUNNING_ANALYSIS_SESSION]:
_checkout_analysing_corpus_container(corpus) _checkout_cqpserver_container(corpus)
for corpus in [x for x in corpora if x.status == CorpusStatus.STARTING_ANALYSIS_SESSION]: for corpus in [x for x in corpora if x.status == CorpusStatus.STARTING_ANALYSIS_SESSION]:
_create_cqpserver_container(corpus) _create_cqpserver_container(corpus)
for corpus in [x for x in corpora if x.status == CorpusStatus.CANCELING_ANALYSIS_SESSION]: for corpus in [x for x in corpora if x.status == CorpusStatus.CANCELING_ANALYSIS_SESSION]:
_remove_cqpserver_container(corpus) _remove_cqpserver_container(corpus)
db.session.commit() db.session.commit()
def _create_build_corpus_service(corpus): def _create_build_corpus_service(corpus: Corpus):
''' # Docker service settings # ''' ''' # Docker service settings # '''
''' ## Command ## ''' ''' ## Command ## '''
command = ['bash', '-c'] command = ['bash', '-c']
@ -53,9 +53,7 @@ def _create_build_corpus_service(corpus):
image = f'{current_app.config["NOPAQUE_DOCKER_IMAGE_PREFIX"]}cwb:r1879' image = f'{current_app.config["NOPAQUE_DOCKER_IMAGE_PREFIX"]}cwb:r1879'
''' ## Labels ## ''' ''' ## Labels ## '''
labels = { labels = {
'origin': current_app.config['SERVER_NAME'], 'nopaque.server_name': current_app.config['SERVER_NAME']
'type': 'corpus.build',
'corpus_id': str(corpus.id)
} }
''' ## Mounts ## ''' ''' ## Mounts ## '''
mounts = [] mounts = []
@ -100,7 +98,7 @@ def _create_build_corpus_service(corpus):
return return
corpus.status = CorpusStatus.QUEUED corpus.status = CorpusStatus.QUEUED
def _checkout_build_corpus_service(corpus): def _checkout_build_corpus_service(corpus: Corpus):
service_name = f'build-corpus_{corpus.id}' service_name = f'build-corpus_{corpus.id}'
try: try:
service = docker_client.services.get(service_name) service = docker_client.services.get(service_name)
@ -128,8 +126,7 @@ def _checkout_build_corpus_service(corpus):
except docker.errors.DockerException as e: except docker.errors.DockerException as e:
current_app.logger.error(f'Remove service "{service_name}" failed: {e}') current_app.logger.error(f'Remove service "{service_name}" failed: {e}')
def _create_cqpserver_container(corpus): def _create_cqpserver_container(corpus: Corpus):
''' # Docker container settings # '''
''' ## Command ## ''' ''' ## Command ## '''
command = [] command = []
command.append( command.append(
@ -146,7 +143,7 @@ def _create_cqpserver_container(corpus):
''' ## Image ## ''' ''' ## Image ## '''
image = f'{current_app.config["NOPAQUE_DOCKER_IMAGE_PREFIX"]}cwb:r1879' image = f'{current_app.config["NOPAQUE_DOCKER_IMAGE_PREFIX"]}cwb:r1879'
''' ## Name ## ''' ''' ## Name ## '''
name = f'cqpserver_{corpus.id}' name = f'nopaque-cqpserver-{corpus.id}'
''' ## Network ## ''' ''' ## Network ## '''
network = f'{current_app.config["NOPAQUE_DOCKER_NETWORK_NAME"]}' network = f'{current_app.config["NOPAQUE_DOCKER_NETWORK_NAME"]}'
''' ## Volumes ## ''' ''' ## Volumes ## '''
@ -203,8 +200,8 @@ def _create_cqpserver_container(corpus):
return return
corpus.status = CorpusStatus.RUNNING_ANALYSIS_SESSION corpus.status = CorpusStatus.RUNNING_ANALYSIS_SESSION
def _checkout_analysing_corpus_container(corpus): def _checkout_cqpserver_container(corpus: Corpus):
container_name = f'cqpserver_{corpus.id}' container_name = f'nopaque-cqpserver-{corpus.id}'
try: try:
docker_client.containers.get(container_name) docker_client.containers.get(container_name)
except docker.errors.NotFound as e: except docker.errors.NotFound as e:
@ -214,8 +211,8 @@ def _checkout_analysing_corpus_container(corpus):
except docker.errors.DockerException as e: except docker.errors.DockerException as e:
current_app.logger.error(f'Get container "{container_name}" failed: {e}') current_app.logger.error(f'Get container "{container_name}" failed: {e}')
def _remove_cqpserver_container(corpus): def _remove_cqpserver_container(corpus: Corpus):
container_name = f'cqpserver_{corpus.id}' container_name = f'nopaque-cqpserver-{corpus.id}'
try: try:
container = docker_client.containers.get(container_name) container = docker_client.containers.get(container_name)
except docker.errors.NotFound: except docker.errors.NotFound:

View File

@ -1,3 +1,10 @@
from datetime import datetime
from flask import current_app
from werkzeug.utils import secure_filename
import docker
import json
import os
import shutil
from app import db, docker_client, hashids, scheduler from app import db, docker_client, hashids, scheduler
from app.models import ( from app.models import (
Job, Job,
@ -6,16 +13,9 @@ from app.models import (
TesseractOCRPipelineModel, TesseractOCRPipelineModel,
SpaCyNLPPipelineModel SpaCyNLPPipelineModel
) )
from datetime import datetime
from flask import current_app
from werkzeug.utils import secure_filename
import docker
import json
import os
import shutil
def job(): def handle_jobs():
with scheduler.app.app_context(): with scheduler.app.app_context():
_handle_jobs() _handle_jobs()
@ -29,7 +29,7 @@ def _handle_jobs():
_remove_job_service(job) _remove_job_service(job)
db.session.commit() db.session.commit()
def _create_job_service(job): def _create_job_service(job: Job):
''' # Docker service settings # ''' ''' # Docker service settings # '''
''' ## Service specific settings ## ''' ''' ## Service specific settings ## '''
if job.service == 'file-setup-pipeline': if job.service == 'file-setup-pipeline':
@ -86,9 +86,7 @@ def _create_job_service(job):
constraints = ['node.role==worker'] constraints = ['node.role==worker']
''' ## Labels ## ''' ''' ## Labels ## '''
labels = { labels = {
'origin': current_app.config['SERVER_NAME'], 'origin': current_app.config['SERVER_NAME']
'type': 'job',
'job_id': str(job.id)
} }
''' ## Mounts ## ''' ''' ## Mounts ## '''
mounts = [] mounts = []
@ -169,7 +167,7 @@ def _create_job_service(job):
return return
job.status = JobStatus.QUEUED job.status = JobStatus.QUEUED
def _checkout_job_service(job): def _checkout_job_service(job: Job):
service_name = f'job_{job.id}' service_name = f'job_{job.id}'
try: try:
service = docker_client.services.get(service_name) service = docker_client.services.get(service_name)
@ -218,7 +216,7 @@ def _checkout_job_service(job):
except docker.errors.DockerException as e: except docker.errors.DockerException as e:
current_app.logger.error(f'Remove service "{service_name}" failed: {e}') current_app.logger.error(f'Remove service "{service_name}" failed: {e}')
def _remove_job_service(job): def _remove_job_service(job: Job):
service_name = f'job_{job.id}' service_name = f'job_{job.id}'
try: try:
service = docker_client.services.get(service_name) service = docker_client.services.get(service_name)