Add settings and live sidebar counts
This commit is contained in:
parent
2a99edeec3
commit
a809bde16c
16 changed files with 696 additions and 51 deletions
|
|
@ -5,6 +5,7 @@ import os
|
|||
import signal
|
||||
import subprocess
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
from dataclasses import dataclass
|
||||
from datetime import UTC, datetime, timedelta
|
||||
|
|
@ -15,7 +16,15 @@ from apscheduler.schedulers.background import BackgroundScheduler
|
|||
from apscheduler.triggers.cron import CronTrigger
|
||||
|
||||
from repub.config import feed_output_dir, feed_output_path
|
||||
from repub.model import Job, JobExecution, JobExecutionStatus, Source, database, utc_now
|
||||
from repub.model import (
|
||||
Job,
|
||||
JobExecution,
|
||||
JobExecutionStatus,
|
||||
Source,
|
||||
database,
|
||||
load_max_concurrent_jobs,
|
||||
utc_now,
|
||||
)
|
||||
|
||||
SCHEDULER_JOB_PREFIX = "job-"
|
||||
POLL_JOB_ID = "runtime-poll-workers"
|
||||
|
|
@ -105,6 +114,7 @@ class JobRuntime:
|
|||
self.graceful_stop_seconds = graceful_stop_seconds
|
||||
self.scheduler = BackgroundScheduler(timezone=UTC)
|
||||
self._workers: dict[int, RunningWorker] = {}
|
||||
self._run_lock = threading.Lock()
|
||||
self._started = False
|
||||
|
||||
def start(self) -> None:
|
||||
|
|
@ -178,28 +188,32 @@ class JobRuntime:
|
|||
def run_job_now(self, job_id: int, *, reason: str) -> int | None:
|
||||
del reason
|
||||
self.start()
|
||||
with database.connection_context():
|
||||
job = Job.get_or_none(id=job_id)
|
||||
if job is None:
|
||||
return None
|
||||
with self._run_lock:
|
||||
with database.connection_context():
|
||||
job = Job.get_or_none(id=job_id)
|
||||
if job is None:
|
||||
return None
|
||||
|
||||
already_running = (
|
||||
JobExecution.select()
|
||||
.where(
|
||||
(JobExecution.job == job)
|
||||
& (JobExecution.running_status == JobExecutionStatus.RUNNING)
|
||||
if self._max_concurrent_jobs_reached():
|
||||
return None
|
||||
|
||||
already_running = (
|
||||
JobExecution.select()
|
||||
.where(
|
||||
(JobExecution.job == job)
|
||||
& (JobExecution.running_status == JobExecutionStatus.RUNNING)
|
||||
)
|
||||
.exists()
|
||||
)
|
||||
.exists()
|
||||
)
|
||||
if already_running:
|
||||
return None
|
||||
if already_running:
|
||||
return None
|
||||
|
||||
execution = JobExecution.create(
|
||||
job=job,
|
||||
started_at=utc_now(),
|
||||
running_status=JobExecutionStatus.RUNNING,
|
||||
)
|
||||
execution_id = _execution_id(execution)
|
||||
execution = JobExecution.create(
|
||||
job=job,
|
||||
started_at=utc_now(),
|
||||
running_status=JobExecutionStatus.RUNNING,
|
||||
)
|
||||
execution_id = _execution_id(execution)
|
||||
|
||||
artifacts = JobArtifacts.for_execution(
|
||||
log_dir=self.log_dir, job_id=job_id, execution_id=execution_id
|
||||
|
|
@ -239,6 +253,14 @@ class JobRuntime:
|
|||
self._trigger_refresh()
|
||||
return execution_id
|
||||
|
||||
def _max_concurrent_jobs_reached(self) -> bool:
|
||||
return (
|
||||
JobExecution.select()
|
||||
.where(JobExecution.running_status == JobExecutionStatus.RUNNING)
|
||||
.count()
|
||||
>= load_max_concurrent_jobs()
|
||||
)
|
||||
|
||||
def request_execution_cancel(self, execution_id: int) -> bool:
|
||||
with database.connection_context():
|
||||
execution = JobExecution.get_or_none(id=execution_id)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue