penguin/AstroJS

Javascript framework for my blog

commit 7419c1c86962f23f223653a946a90fad3503a82a

author斟酌 鵬兄 <tgckpg@gmail.com>
date2019-01-19T09:13:36Z
subjectRewrite service files for uwsgi & systemctl
commit 7419c1c86962f23f223653a946a90fad3503a82a
Author: 斟酌 鵬兄 <tgckpg@gmail.com>
Date:   2019-01-19T09:13:36Z

    Rewrite service files for uwsgi & systemctl
---
 botan-start.py                | 57 -------------------------------------------
 botanjs/compressor/closure.py |  0
 botanjs/compressor/yui.py     |  0
 botanjs/config.py             |  0
 botanjs/service/webapi.py     |  9 +++----
 main.py                       | 22 +++++++++++++++++
 setup/celery.conf             | 15 ++++++++++++
 setup/compiler-tasks.service  | 19 +++++++++++++++
 setup/config                  | 13 ++++++++++
 setup/install                 | 21 ++++++++++++++++
 10 files changed, 93 insertions(+), 63 deletions(-)

diff --git a/botan-start.py b/botan-start.py
deleted file mode 100755
index e36bf7b..0000000
--- a/botan-start.py
+++ /dev/null
@@ -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"]
-	)
diff --git a/botanjs/compressor/closure.py b/botanjs/compressor/closure.py
old mode 100755
new mode 100644
diff --git a/botanjs/compressor/yui.py b/botanjs/compressor/yui.py
old mode 100755
new mode 100644
diff --git a/botanjs/config.py b/botanjs/config.py
old mode 100755
new mode 100644
diff --git a/botanjs/service/webapi.py b/botanjs/service/webapi.py
old mode 100755
new mode 100644
index 54a62ea..ab2dd5c
--- a/botanjs/service/webapi.py
+++ b/botanjs/service/webapi.py
@@ -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
diff --git a/main.py b/main.py
new file mode 100644
index 0000000..d3ade1d
--- /dev/null
+++ b/main.py
@@ -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 )
diff --git a/setup/celery.conf b/setup/celery.conf
new file mode 100644
index 0000000..24fe5c8
--- /dev/null
+++ b/setup/celery.conf
@@ -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"
diff --git a/setup/compiler-tasks.service b/setup/compiler-tasks.service
new file mode 100644
index 0000000..28630cb
--- /dev/null
+++ b/setup/compiler-tasks.service
@@ -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
diff --git a/setup/config b/setup/config
new file mode 100755
index 0000000..8cb7eff
--- /dev/null
+++ b/setup/config
@@ -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"
diff --git a/setup/install b/setup/install
new file mode 100755
index 0000000..9166471
--- /dev/null
+++ b/setup/install
@@ -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