Interim headless docker commit \o/
This commit is contained in:
parent
a97e72abd7
commit
c1f1742948
7 changed files with 179 additions and 137 deletions
9
api/.dockerignore
Normal file
9
api/.dockerignore
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
/out/
|
||||||
|
/lib/
|
||||||
|
/lib64
|
||||||
|
/bin/
|
||||||
|
|
||||||
|
state.dat
|
||||||
|
filekey.key
|
||||||
|
__pycache__
|
||||||
|
.envrc
|
||||||
33
api/Dockerfile
Normal file
33
api/Dockerfile
Normal file
|
|
@ -0,0 +1,33 @@
|
||||||
|
FROM python:3.10-buster
|
||||||
|
|
||||||
|
RUN apt-get update && apt-get upgrade -y
|
||||||
|
RUN apt-get install -y libasound2 \
|
||||||
|
libnspr4 \
|
||||||
|
libnss3 \
|
||||||
|
libxss1 \
|
||||||
|
xdg-utils \
|
||||||
|
unzip \
|
||||||
|
libappindicator1 \
|
||||||
|
fonts-liberation \
|
||||||
|
xvfb \
|
||||||
|
libatk-bridge2.0-0 \
|
||||||
|
libatspi2.0-0 \
|
||||||
|
libgbm1 \
|
||||||
|
libgtk-3-0 \
|
||||||
|
libu2f-udev \
|
||||||
|
libvulkan1 \
|
||||||
|
libxkbcommon0
|
||||||
|
RUN apt-get -f install
|
||||||
|
RUN wget http://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
|
||||||
|
RUN dpkg -i google-chrome*.deb && rm google-chrome*.deb
|
||||||
|
|
||||||
|
# Patch to make XVFB actually work
|
||||||
|
RUN mkdir /tmp/.X11-unix
|
||||||
|
RUN chmod 1777 /tmp/.X11-unix
|
||||||
|
RUN chown root /tmp/.X11-unix/
|
||||||
|
|
||||||
|
COPY . .
|
||||||
|
|
||||||
|
RUN pip install -r requirements.txt
|
||||||
|
|
||||||
|
CMD ["python", "src/__main__.py"]
|
||||||
|
|
@ -38,6 +38,7 @@ pymongo==4.3.3
|
||||||
PySocks==1.7.1
|
PySocks==1.7.1
|
||||||
python-dotenv==1.0.0
|
python-dotenv==1.0.0
|
||||||
pytoolconfig==1.2.5
|
pytoolconfig==1.2.5
|
||||||
|
PyVirtualDisplay==3.0
|
||||||
requests==2.28.2
|
requests==2.28.2
|
||||||
rope==1.7.0
|
rope==1.7.0
|
||||||
selenium==4.8.3
|
selenium==4.8.3
|
||||||
|
|
|
||||||
66
api/src/__api__.py
Normal file
66
api/src/__api__.py
Normal file
|
|
@ -0,0 +1,66 @@
|
||||||
|
"""
|
||||||
|
The API endpoints for the proessor
|
||||||
|
Author: oitsjustjose @ modrinth/curseforge/twitter
|
||||||
|
"""
|
||||||
|
|
||||||
|
import json
|
||||||
|
from dataclasses import asdict
|
||||||
|
from multiprocessing import Process
|
||||||
|
from os import environ as env
|
||||||
|
|
||||||
|
from flask import Flask, request
|
||||||
|
|
||||||
|
from data import Status
|
||||||
|
from job_database import Job, JobDb, Status
|
||||||
|
|
||||||
|
app = Flask(__name__)
|
||||||
|
|
||||||
|
db = JobDb(env["MONGO_URI"], int(env["MONGO_PORT"]))
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/jobs/status/<job_id>", methods=["GET", "POST"])
|
||||||
|
def get_job_status(job_id: str) -> str:
|
||||||
|
"Gets the queue status for a given job"
|
||||||
|
|
||||||
|
status, logs, queue = db.get_job_status(job_id)
|
||||||
|
if status == Status.ENQUEUED:
|
||||||
|
return f"Enqueued: {queue} Job(s) Ahead In Queue", 200
|
||||||
|
if status == Status.PROCESSING:
|
||||||
|
return f"Currently Processing\n{logs}", 200
|
||||||
|
if status == Status.COMPLETE:
|
||||||
|
return logs, 200
|
||||||
|
return "Failed to find job", 404
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/jobs/info/<job_id>", methods=["GET", "POST"])
|
||||||
|
def get_job_info(job_id: str):
|
||||||
|
"""Gets the job info for a given uuid"""
|
||||||
|
job = db.get_job(job_id)
|
||||||
|
if not job:
|
||||||
|
return "Failed to find job", 404
|
||||||
|
job.github_pat = ""
|
||||||
|
return json.dumps(asdict(job), indent=2), 200
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/jobs/new", methods=["POST"])
|
||||||
|
def new_job():
|
||||||
|
"""Creates a new job"""
|
||||||
|
try:
|
||||||
|
data = json.loads(request.data)
|
||||||
|
jobe = Job(
|
||||||
|
github_pat=data["githubPat"],
|
||||||
|
curseforge_slug=data["slug"],
|
||||||
|
modrinth_id=data["projId"],
|
||||||
|
delimiter=data["delimiter"] if "delimiter" in data else "-",
|
||||||
|
)
|
||||||
|
|
||||||
|
# Job already exists in queue, give it back
|
||||||
|
existing = db.get_existing_queued_job(jobe.curseforge_slug, jobe.modrinth_id)
|
||||||
|
if existing:
|
||||||
|
return existing, 200
|
||||||
|
return db.enqueue_job(jobe), 200
|
||||||
|
except:
|
||||||
|
return "Internal Server Error", 500
|
||||||
|
|
||||||
|
|
||||||
|
app.run("0.0.0.0", env["PORT"] if "PORT" in env else 3000)
|
||||||
|
|
@ -1,72 +1,79 @@
|
||||||
"""
|
"""
|
||||||
The API endpoints for the proessor
|
Does the actual work behind the scenes
|
||||||
Author: oitsjustjose @ modrinth/curseforge/twitter
|
Author: oitsjustjose @ modrinth/curseforge/twitter
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import json
|
|
||||||
from dataclasses import asdict
|
|
||||||
from multiprocessing import Process
|
|
||||||
from os import environ as env
|
from os import environ as env
|
||||||
|
from time import sleep
|
||||||
|
|
||||||
from flask import Flask, request
|
from data import Job, Status
|
||||||
|
from job_database import JobDb
|
||||||
from data import Status
|
from netio_processor import download, upload
|
||||||
from job_database import Job, JobDb, Status
|
|
||||||
from job_processor import worker
|
|
||||||
|
|
||||||
app = Flask(__name__)
|
|
||||||
|
|
||||||
db = JobDb(env["MONGO_URI"], int(env["MONGO_PORT"]))
|
|
||||||
|
|
||||||
|
|
||||||
@app.route("/jobs/status/<job_id>", methods=["GET", "POST"])
|
def _resume(job_db: JobDb):
|
||||||
def get_job_status(job_id: str) -> str:
|
"""Resumes processing jobs on server restart"""
|
||||||
"Gets the queue status for a given job"
|
print("Resuming jobs from last run")
|
||||||
|
|
||||||
status, logs, queue = db.get_job_status(job_id)
|
for job in job_db.get_active_jobs():
|
||||||
if status == Status.ENQUEUED:
|
print(f"Resuming job {job.job_id}")
|
||||||
return f"Enqueued: {queue} Job(s) Ahead In Queue", 200
|
job_db.update_job_status(job.job_id, Status.PROCESSING)
|
||||||
if status == Status.PROCESSING:
|
dl_status, dl_logs = download(job.curseforge_slug)
|
||||||
return f"Currently Processing\n{logs}", 200
|
if dl_status == Status.FAIL: # Failed - early exit, update DB for user
|
||||||
if status == Status.COMPLETE:
|
job_db.update_job_status(
|
||||||
return logs, 200
|
job.job_id, Status.COMPLETE, "\n".join(["**FAILED**"] + dl_logs)
|
||||||
return "Failed to find job", 404
|
)
|
||||||
|
continue
|
||||||
|
# Didn't fail, update DB with latest logs and same status
|
||||||
@app.route("/jobs/info/<job_id>", methods=["GET", "POST"])
|
job_db.update_job_status(job.job_id, Status.PROCESSING, "\n".join(dl_logs))
|
||||||
def get_job_info(job_id: str):
|
ul_status, ul_logs = upload(
|
||||||
"""Gets the job info for a given uuid"""
|
job.github_pat, job.curseforge_slug, job.modrinth_id, job.delimiter
|
||||||
job = db.get_job(job_id)
|
)
|
||||||
if not job:
|
# Take the higher severity - it would only be
|
||||||
return "Failed to find job", 404
|
text_status = Status(max(dl_status.value, ul_status.value)).name
|
||||||
job.github_pat = ""
|
job_db.update_job_status(
|
||||||
return json.dumps(asdict(job), indent=2), 200
|
job.job_id,
|
||||||
|
Status.COMPLETE,
|
||||||
|
"\n".join([f"**{text_status}**"] + ul_logs + dl_logs),
|
||||||
@app.route("/jobs/new", methods=["POST"])
|
|
||||||
def new_job():
|
|
||||||
"""Creates a new job"""
|
|
||||||
try:
|
|
||||||
data = json.loads(request.data)
|
|
||||||
jobe = Job(
|
|
||||||
github_pat=data["githubPat"],
|
|
||||||
curseforge_slug=data["slug"],
|
|
||||||
modrinth_id=data["projId"],
|
|
||||||
delimiter=data["delimiter"] if "delimiter" in data else "-",
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# Job already exists in queue, give it back
|
|
||||||
existing = db.get_existing_queued_job(jobe.curseforge_slug, jobe.modrinth_id)
|
def _work(job_db: JobDb) -> bool:
|
||||||
if existing:
|
"""Does the actual lifting"""
|
||||||
return existing, 200
|
job: Job = job_db.get_next_job()
|
||||||
return db.enqueue_job(jobe), 200
|
if not job:
|
||||||
except:
|
return False
|
||||||
return "Internal Server Error", 500
|
|
||||||
|
job_db.update_job_status(job.job_id, Status.PROCESSING)
|
||||||
|
dl_status, dl_logs = download(job.curseforge_slug)
|
||||||
|
if dl_status == Status.FAIL: # Failed - early exit, update DB for user
|
||||||
|
job_db.update_job_status(
|
||||||
|
job.job_id, Status.COMPLETE, "\n".join(["**FAILED**"] + dl_logs)
|
||||||
|
)
|
||||||
|
return True
|
||||||
|
# Didn't fail, update DB with latest logs and same status
|
||||||
|
job_db.update_job_status(job.job_id, Status.PROCESSING, "\n".join(dl_logs))
|
||||||
|
ul_status, ul_logs = upload(
|
||||||
|
job.github_pat, job.curseforge_slug, job.modrinth_id, job.delimiter
|
||||||
|
)
|
||||||
|
# Take the higher severity - it would only be
|
||||||
|
text_status = Status(max(dl_status.value, ul_status.value)).name
|
||||||
|
job_db.update_job_status(
|
||||||
|
job.job_id,
|
||||||
|
Status.COMPLETE,
|
||||||
|
"\n".join([f"**{text_status}**"] + ul_logs + dl_logs),
|
||||||
|
)
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
proc_cnt: int = int(env["PROCS"]) if "PROCS" in env else 1
|
print("Starting the job processor")
|
||||||
processes = [Process(target=worker) for _ in range(proc_cnt)]
|
job_db = JobDb(env["MONGO_URI"], int(env["MONGO_PORT"]))
|
||||||
_ = [p.start() for p in processes]
|
_resume(job_db)
|
||||||
app.run("0.0.0.0", env["PORT"] if "PORT" in env else 3000)
|
while True:
|
||||||
_ = [p.join() for p in processes]
|
try:
|
||||||
|
if _work(job_db):
|
||||||
|
sleep(1)
|
||||||
|
else:
|
||||||
|
sleep(5)
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
break
|
||||||
|
|
|
||||||
|
|
@ -1,80 +0,0 @@
|
||||||
"""
|
|
||||||
Does the actual work behind the scenes
|
|
||||||
Author: oitsjustjose @ modrinth/curseforge/twitter
|
|
||||||
"""
|
|
||||||
from os import environ as env
|
|
||||||
from time import sleep
|
|
||||||
|
|
||||||
from data import Job, Status
|
|
||||||
from job_database import JobDb
|
|
||||||
from netio_processor import download, upload
|
|
||||||
|
|
||||||
|
|
||||||
def _resume(job_db: JobDb):
|
|
||||||
"""Resumes processing jobs on server restart"""
|
|
||||||
print("Resuming jobs from last run")
|
|
||||||
|
|
||||||
for job in job_db.get_active_jobs():
|
|
||||||
print(f"Resuming job {job.job_id}")
|
|
||||||
job_db.update_job_status(job.job_id, Status.PROCESSING)
|
|
||||||
dl_status, dl_logs = download(job.curseforge_slug)
|
|
||||||
if dl_status == Status.FAIL: # Failed - early exit, update DB for user
|
|
||||||
job_db.update_job_status(
|
|
||||||
job.job_id, Status.COMPLETE, "\n".join(["**FAILED**"] + dl_logs)
|
|
||||||
)
|
|
||||||
continue
|
|
||||||
# Didn't fail, update DB with latest logs and same status
|
|
||||||
job_db.update_job_status(job.job_id, Status.PROCESSING, "\n".join(dl_logs))
|
|
||||||
ul_status, ul_logs = upload(
|
|
||||||
job.github_pat, job.curseforge_slug, job.modrinth_id, job.delimiter
|
|
||||||
)
|
|
||||||
# Take the higher severity - it would only be
|
|
||||||
text_status = Status(max(dl_status.value, ul_status.value)).name
|
|
||||||
job_db.update_job_status(
|
|
||||||
job.job_id,
|
|
||||||
Status.COMPLETE,
|
|
||||||
"\n".join([f"**{text_status}**"] + ul_logs + dl_logs),
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def _work(job_db: JobDb) -> bool:
|
|
||||||
"""Does the actual lifting"""
|
|
||||||
job: Job = job_db.get_next_job()
|
|
||||||
if not job:
|
|
||||||
return False
|
|
||||||
|
|
||||||
job_db.update_job_status(job.job_id, Status.PROCESSING)
|
|
||||||
dl_status, dl_logs = download(job.curseforge_slug)
|
|
||||||
if dl_status == Status.FAIL: # Failed - early exit, update DB for user
|
|
||||||
job_db.update_job_status(
|
|
||||||
job.job_id, Status.COMPLETE, "\n".join(["**FAILED**"] + dl_logs)
|
|
||||||
)
|
|
||||||
return True
|
|
||||||
# Didn't fail, update DB with latest logs and same status
|
|
||||||
job_db.update_job_status(job.job_id, Status.PROCESSING, "\n".join(dl_logs))
|
|
||||||
ul_status, ul_logs = upload(
|
|
||||||
job.github_pat, job.curseforge_slug, job.modrinth_id, job.delimiter
|
|
||||||
)
|
|
||||||
# Take the higher severity - it would only be
|
|
||||||
text_status = Status(max(dl_status.value, ul_status.value)).name
|
|
||||||
job_db.update_job_status(
|
|
||||||
job.job_id,
|
|
||||||
Status.COMPLETE,
|
|
||||||
"\n".join([f"**{text_status}**"] + ul_logs + dl_logs),
|
|
||||||
)
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
def worker():
|
|
||||||
"""A worker thread function for processing jobs in the job db"""
|
|
||||||
print("Starting the job processor")
|
|
||||||
job_db = JobDb(env["MONGO_URI"], int(env["MONGO_PORT"]))
|
|
||||||
_resume(job_db)
|
|
||||||
while True:
|
|
||||||
try:
|
|
||||||
if _work(job_db):
|
|
||||||
sleep(1)
|
|
||||||
else:
|
|
||||||
sleep(5)
|
|
||||||
except KeyboardInterrupt:
|
|
||||||
break
|
|
||||||
|
|
@ -15,7 +15,7 @@ from selenium import webdriver
|
||||||
from selenium.webdriver.chrome.options import Options
|
from selenium.webdriver.chrome.options import Options
|
||||||
from selenium.webdriver.chrome.service import Service
|
from selenium.webdriver.chrome.service import Service
|
||||||
from webdriver_manager.chrome import ChromeDriverManager
|
from webdriver_manager.chrome import ChromeDriverManager
|
||||||
|
from pyvirtualdisplay import Display
|
||||||
from data import ModInfo, Status
|
from data import ModInfo, Status
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -239,7 +239,11 @@ def download(slug: str) -> Tuple[Status, List[str]]:
|
||||||
os.makedirs(f"./out/{slug}", exist_ok=True)
|
os.makedirs(f"./out/{slug}", exist_ok=True)
|
||||||
|
|
||||||
# REGION CHROME DRIVER AND OPTIONS
|
# REGION CHROME DRIVER AND OPTIONS
|
||||||
|
display = Display(visible=0, size=(1366, 768))
|
||||||
|
display.start()
|
||||||
|
|
||||||
options = Options()
|
options = Options()
|
||||||
|
options.add_argument("--no-sandbox")
|
||||||
options.add_argument("--ignore-ssl-errors=yes")
|
options.add_argument("--ignore-ssl-errors=yes")
|
||||||
options.add_argument("--ignore-certificate-errors")
|
options.add_argument("--ignore-certificate-errors")
|
||||||
options.add_experimental_option("excludeSwitches", ["enable-logging"])
|
options.add_experimental_option("excludeSwitches", ["enable-logging"])
|
||||||
|
|
@ -276,4 +280,6 @@ def download(slug: str) -> Tuple[Status, List[str]]:
|
||||||
else Status.FAIL
|
else Status.FAIL
|
||||||
)
|
)
|
||||||
|
|
||||||
|
display.stop()
|
||||||
|
driver.close()
|
||||||
return status, logs
|
return status, logs
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue