Skip to content

Commit

Permalink
improve deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
mozart committed Jan 5, 2020
1 parent b47176f commit 7fb92a1
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 6 deletions.
17 changes: 17 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
.github/

.vscode/

assets/

README.md

requirements.in

Procfile

**/*/_pycache__/

*.pyc

.env
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ __pycache__

venv

.vscode
.vscode

.idea/
21 changes: 21 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
FROM python:3.7

RUN apt update && apt install --yes ffmpeg

ADD ./requirements.txt /app/requirements.txt

RUN pip install -r /app/requirements.txt

ADD ./xhaka /app/xhaka
ADD ./uwsgi.ini /app

RUN useradd -m xhaka
RUN chown xhaka:xhaka /app
WORKDIR /app

USER xhaka

ENV PORT=5000
CMD ["uwsgi", "uwsgi.ini" ]

EXPOSE 5000
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,4 @@ youtube-dl==2019.11.28 \

# WARNING: The following packages were not pinned, but pip requires them to be
# pinned when the requirements file includes hashes. Consider using the --allow-unsafe flag.
# setuptools==42.0.2 # via apscheduler, gunicorn
# setuptools
8 changes: 5 additions & 3 deletions uwsgi.ini
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
[uwsgi]
http-socket = :$(PORT)
master = false
master = true
processes = 2
enable-threads = true
threads = 16
threads = 4
die-on-term = true
wsgi-file = xhaka/app.py
callable = app
memory-report = true
vacuum = true
need-app = true
lazy = true
stats = :9191
32 changes: 31 additions & 1 deletion xhaka/app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from functools import wraps
from datetime import datetime, timezone, timedelta
from logging.config import dictConfig
from flask import Flask, current_app, render_template, request, redirect, \
url_for, session, json
from werkzeug.exceptions import Unauthorized
Expand All @@ -14,6 +15,22 @@
from xhaka.ggdrive_folders import folder_list_filted, get_folder_hierarchy
# from .scheduler_setup import is_predefined_crontask_lck

import atexit

dictConfig({
'version': 1,
'formatters': {'default': {
'format': '[PID %(process)d - %(asctime)s] %(levelname)s in %(module)s: %(message)s',
}},
'handlers': {'wsgi': {
'class': 'logging.StreamHandler',
'formatter': 'default'
}},
'root': {
'level': 'DEBUG',
'handlers': ['wsgi']
}
})

jobinfo_keys = set([
'started_at',
Expand Down Expand Up @@ -136,6 +153,13 @@ def timestamp_to_datetime(s):
scheduler.start()


def shutdown_scheduler_on_exit():
scheduler.shutdown()


atexit.register(shutdown_scheduler_on_exit)


def clean_up_job_info():
from json import JSONDecodeError
print('cleaning up task running')
Expand Down Expand Up @@ -272,9 +296,13 @@ def upload():
if request.method == "POST":
folder_id = request.form.get("folder_id")
yt_url = request.form.get("url")
app.logger.info("adding job")
job = scheduler.add_job(
schedule_job,
args=(yt_url, folder_id, oauth.google.token, session['user_id']))
app.logger.info("add job done")

app.logger.info("save initial job info to redis")
with redis_jobstore.redis.pipeline() as pipe:
redis_jobstore.redis.set(
"apscheduler.jobinfo:%s:%s" % (session['user_id'], job.id),
Expand All @@ -289,6 +317,8 @@ def upload():
ex=3600 # expires in 1 hour
)
pipe.execute()
app.logger.info("save to redis done")

session['msg'] = {'type': 'success', 'msg': 'Task created'}
return redirect(url_for("upload"))

Expand Down Expand Up @@ -340,6 +370,6 @@ def tasks():
kv[0].decode('ascii'): json.loads(kv[1])
# value of jobinfo still in bytes but we can order base on it because
# started_at is first item
for kv in sorted(jobdata.items(), key=lambda x: x[1], reverse=True)
for kv in sorted(jobdata.items(), key=lambda x: x[1])
}
return render_template('tasks.html', jobs=jobs)

0 comments on commit 7fb92a1

Please sign in to comment.