Rewrite service files for uwsgi & systemctl

This commit is contained in:
斟酌 鵬兄 2019-01-19 17:13:36 +08:00
parent a63e4751df
commit 7419c1c869
10 changed files with 93 additions and 63 deletions

View File

@ -1,57 +0,0 @@
#!env/bin/python
import os, sys
from botanjs.config import Config as config, DEBUG
from subprocess import Popen
from botanjs.service.webapi import WebAPI
SiteRoot = os.path.abspath( "." )
# Setting the SiteRoot for config
config["Paths"]["SiteRoot"] = SiteRoot;
# Create the lock folder for celery
lockDir = os.path.join( SiteRoot, "env", "var", "run", "celery" )
os.makedirs( lockDir, exist_ok=True )
sys.path.append( os.path.abspath( "." ) )
RUNTIME_ENV = os.path.abspath( os.path.join( "env", "bin" ) )
if RUNTIME_ENV not in os.environ[ "PATH" ]:
os.environ[ "PATH" ] = RUNTIME_ENV + os.pathsep + os.environ[ "PATH" ]
if __name__ == "__main__":
jwork = "botanjs.service.jwork"
nodeName = "botanNode1"
celOut = open( os.path.join( config["Paths"]["Log"], jwork + "-err.log" ), "a+" )
cel = Popen(
[
"celery", "multi", "restart", nodeName
, "-A", jwork, "worker"
, "--pidfile=" + os.path.join( lockDir, jwork + ".pid" )
, "--logfile=" + os.path.join( config["Paths"]["Log"], jwork + ".log" )
, "--workdir=" + config["Paths"]["Runtime"]
, "beat", "-l", "info"
]
, stdout = celOut
, stderr = celOut
)
celOut.close()
if not DEBUG and os.fork():
import logging
logging.basicConfig(
filename = os.path.join( config["Paths"]["Log"], "access.log" )
, level = logging.DEBUG
)
sys.exit()
WebAPI(
jsCache = config["Paths"]["Cache"]
, jsRoot = config["BotanJS"]["SrcDir"]
, brokerURL = config["BotanJS"]["CeleryBroker"]
)

0
botanjs/compressor/closure.py Executable file → Normal file
View File

0
botanjs/compressor/yui.py Executable file → Normal file
View File

0
botanjs/config.py Executable file → Normal file
View File

9
botanjs/service/webapi.py Executable file → Normal file
View File

@ -1,11 +1,10 @@
#!/usr/bin/env python3
from flask import Flask
from flask import Response
from flask import render_template
from flask import request
from botanjs.service.jclassresv import BotanClassResolver as JCResv
from botanjs.service.jwork import app as CeleryApp, JWork
from botanjs.config import Config, DEBUG
from botanjs.config import Config
import os
@ -33,10 +32,8 @@ class WebAPI:
self.app.add_url_rule( "/<mode>/" , view_func = lambda mode: self.api_request( mode, "zpayload" ) )
self.app.add_url_rule( "/<mode>/<path:code>" , view_func = self.api_request )
self.app.run(
host = Config[ "Service" ][ "BindAddress" ]
, port = int( Config[ "Service" ][ "Port" ] )
, debug = DEBUG )
def run( self, *args, **kwargs ):
return self.app.run( *args, **kwargs )
def index( self ):
return "Hello, this is the BotanJS Service API.", 200

22
main.py Normal file
View File

@ -0,0 +1,22 @@
#!env/bin/python
import os
from botanjs.config import Config as config, DEBUG
from subprocess import Popen
from botanjs.service.webapi import WebAPI
SiteRoot = os.path.abspath( "." )
# Setting the SiteRoot for config
config["Paths"]["SiteRoot"] = SiteRoot;
service = WebAPI(
jsCache = config["Paths"]["Cache"]
, jsRoot = config["BotanJS"]["SrcDir"]
, brokerURL = config["BotanJS"]["CeleryBroker"]
)
application = service.app
if __name__ == "__main__":
service.run( debug = DEBUG )

15
setup/celery.conf Normal file
View File

@ -0,0 +1,15 @@
# Absolute or relative path to the 'celery' command:
CELERY_BIN="BIN_ROOT/celery"
CELERYD_NODES="w1 w2"
CELERY_APP="botanjs.service.jwork"
# How to call manage.py
CELERYD_MULTI="multi"
# - %n will be replaced with the first part of the nodename.
# # - %I will be replaced with the current child process index
# # and is important when using the prefork pool to avoid race conditions.
CELERYD_PID_FILE="RUN_ROOT/tasks-%n.pid"
CELERYD_LOG_FILE="PROJ_ROOT/logs/tasks-%n.log"
CELERYD_LOG_LEVEL="INFO"

View File

@ -0,0 +1,19 @@
[Unit]
Description=BotanJS Compiler Tasks
After=network.target
[Service]
Type=forking
EnvironmentFile=-ETC_ROOT/celery.conf
WorkingDirectory=PROJ_ROOT
ExecStart=/bin/sh -c '${CELERY_BIN} multi start ${CELERYD_NODES} \
-A ${CELERY_APP} --pidfile=${CELERYD_PID_FILE} \
--logfile=${CELERYD_LOG_FILE} --loglevel=${CELERYD_LOG_LEVEL} ${CELERYD_OPTS}'
ExecStop=/bin/sh -c '${CELERY_BIN} multi stopwait ${CELERYD_NODES} \
--pidfile=${CELERYD_PID_FILE}'
ExecReload=/bin/sh -c '${CELERY_BIN} multi restart ${CELERYD_NODES} \
-A ${CELERY_APP} --pidfile=${CELERYD_PID_FILE} \
--logfile=${CELERYD_LOG_FILE} --loglevel=${CELERYD_LOG_LEVEL} ${CELERYD_OPTS}'
[Install]
WantedBy=multi-user.target

13
setup/config Executable file
View File

@ -0,0 +1,13 @@
#!/bin/bash
[[ $CONFIG ]] && return
CONFIG=1
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
SCRIPT_DIR="$( realpath "$SCRIPT_DIR" )"
PROJ_ROOT="$( cd "$SCRIPT_DIR/../" && pwd )"
ENV_ROOT="$PROJ_ROOT/env"
BIN_ROOT="$ENV_ROOT/bin"
ETC_ROOT="$ENV_ROOT/etc"
RUN_ROOT="$ENV_ROOT/run"
PYTHON="$BIN_ROOT/python3"

21
setup/install Executable file
View File

@ -0,0 +1,21 @@
#!/bin/bash
INST_DIR=$( dirname "${BASH_SOURCE[0]}" )
source "$INST_DIR/config"
mkdir -p "$RUN_ROOT"
mkdir -p "$ETC_ROOT"
sed -e "s|PROJ_ROOT|$PROJ_ROOT|g" \
-e "s|BIN_ROOT|$BIN_ROOT|g" \
-e "s|RUN_ROOT|$RUN_ROOT|g" \
"$INST_DIR/celery.conf" > "$ETC_ROOT/celery.conf"
sed -e "s|PROJ_ROOT|$PROJ_ROOT|g" \
-e "s|ETC_ROOT|$ETC_ROOT|g" \
-e "s|RUN_AS|$RUN_AS|g" \
"$INST_DIR/compiler-tasks.service" > $HOME/.config/systemd/user/botanjs-tasks.service
systemctl --user enable botanjs-tasks.service
systemctl --user daemon-reload
systemctl --user start botanjs-tasks.service