[~] Refactor
This commit is contained in:
parent
5a7b0b5e21
commit
fdd19fbed2
141
d1/cpanel.py
Normal file
141
d1/cpanel.py
Normal file
@ -0,0 +1,141 @@
|
|||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
import io
|
||||||
|
import copy
|
||||||
|
import traceback
|
||||||
|
import datetime
|
||||||
|
import pprint
|
||||||
|
import logging
|
||||||
|
import json
|
||||||
|
import time
|
||||||
|
|
||||||
|
with io.open(
|
||||||
|
'tmp/d1/cpanel.json', 'r'
|
||||||
|
) as f:
|
||||||
|
t3 = json.load(f)
|
||||||
|
|
||||||
|
t2 = copy.deepcopy(t3)
|
||||||
|
for k in t2:
|
||||||
|
v = t2[k]
|
||||||
|
v['task'] = lambda : subprocess.Popen(
|
||||||
|
v['task_cmd'],
|
||||||
|
stdin=subprocess.DEVNULL,
|
||||||
|
)
|
||||||
|
|
||||||
|
def stop_task(task):
|
||||||
|
task.terminate()
|
||||||
|
try:
|
||||||
|
task.wait(1)
|
||||||
|
except:
|
||||||
|
task.kill()
|
||||||
|
|
||||||
|
t1 = dict()
|
||||||
|
|
||||||
|
shutdown = False
|
||||||
|
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
for k, v in t2.items():
|
||||||
|
if not k in t1:
|
||||||
|
logging.info(json.dumps(dict(
|
||||||
|
task=k,
|
||||||
|
status='starting',
|
||||||
|
)))
|
||||||
|
t1[k] = v['task']()
|
||||||
|
logging.info(json.dumps(dict(
|
||||||
|
task=k,
|
||||||
|
status='started',
|
||||||
|
)))
|
||||||
|
continue
|
||||||
|
|
||||||
|
o = t1[k]
|
||||||
|
|
||||||
|
not_alive = None
|
||||||
|
|
||||||
|
try:
|
||||||
|
url_content = []
|
||||||
|
with subprocess.Popen(
|
||||||
|
[
|
||||||
|
'curl',
|
||||||
|
'-q', '--silent',
|
||||||
|
'-v',
|
||||||
|
'--max-time', '4',
|
||||||
|
'--max-filesize', '%d' % (4 * 1024 * 1024),
|
||||||
|
v['url'],
|
||||||
|
],
|
||||||
|
stdout=subprocess.DEVNULL,
|
||||||
|
stderr=subprocess.PIPE,
|
||||||
|
) as curl:
|
||||||
|
def read_chunk():
|
||||||
|
chunk = curl.stderr.read().decode('utf-8')
|
||||||
|
if isinstance(chunk, str) and len(chunk) > 0:
|
||||||
|
url_content.append(chunk)
|
||||||
|
|
||||||
|
if isinstance(chunk, str) and 'status: ' in chunk:
|
||||||
|
stop_task(curl)
|
||||||
|
|
||||||
|
return chunk
|
||||||
|
|
||||||
|
while curl.poll() is None:
|
||||||
|
read_chunk()
|
||||||
|
|
||||||
|
while True:
|
||||||
|
chunk = read_chunk()
|
||||||
|
|
||||||
|
if chunk is None or chunk == '':
|
||||||
|
break
|
||||||
|
|
||||||
|
url_content2 = ''.join(url_content)
|
||||||
|
|
||||||
|
if not 'status: 502' in url_content2 and (
|
||||||
|
'status: 200' in url_content2 or
|
||||||
|
'status: 302' in url_content2
|
||||||
|
):
|
||||||
|
not_alive = False
|
||||||
|
else:
|
||||||
|
not_alive = True
|
||||||
|
except:
|
||||||
|
logging.error(json.dumps(dict(
|
||||||
|
error=traceback.format_exc(),
|
||||||
|
)))
|
||||||
|
not_alive = True
|
||||||
|
|
||||||
|
if not_alive:
|
||||||
|
logging.error(json.dumps(
|
||||||
|
dict(
|
||||||
|
o=pprint.pformat(o.__dict__),
|
||||||
|
status='not_alive',
|
||||||
|
)
|
||||||
|
))
|
||||||
|
|
||||||
|
stop_task(o)
|
||||||
|
del t1[k]
|
||||||
|
continue
|
||||||
|
|
||||||
|
if not o.poll() is None:
|
||||||
|
logging.error(json.dumps(
|
||||||
|
dict(
|
||||||
|
o=pprint.pformat(o.__dict__),
|
||||||
|
return_code=o.poll(),
|
||||||
|
status='crashed',
|
||||||
|
)
|
||||||
|
))
|
||||||
|
del t1[k]
|
||||||
|
continue
|
||||||
|
|
||||||
|
if shutdown:
|
||||||
|
break
|
||||||
|
|
||||||
|
print('\r%s tasks %d' % (
|
||||||
|
datetime.datetime.now().isoformat(),
|
||||||
|
len(t1),
|
||||||
|
), end='')
|
||||||
|
sys.stdout.flush()
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print('\nshutting down')
|
||||||
|
break
|
||||||
|
finally:
|
||||||
|
time.sleep(10)
|
||||||
|
|
||||||
|
for o in t1:
|
||||||
|
stop_task(o)
|
2
d1/f2.py
2
d1/f2.py
@ -20,7 +20,7 @@ sys.path.insert(0, os.path.dirname(__file__))
|
|||||||
|
|
||||||
|
|
||||||
class Application:
|
class Application:
|
||||||
MAX_FILE_SIZE = 2 * 1024 * 1024 * 1024
|
MAX_FILE_SIZE = 4 * 1024 * 1024 * 1024
|
||||||
CHUNK_SIZE = 4096
|
CHUNK_SIZE = 4096
|
||||||
MAX_OUTPUT_SIZE = 4 * 1024 * 1024
|
MAX_OUTPUT_SIZE = 4 * 1024 * 1024
|
||||||
LOG_SIZE = 10 * 1024 * 1024
|
LOG_SIZE = 10 * 1024 * 1024
|
||||||
|
@ -8,6 +8,16 @@ services:
|
|||||||
- ./d1/nginx_config.py:/app/d1/nginx_config.py:ro
|
- ./d1/nginx_config.py:/app/d1/nginx_config.py:ro
|
||||||
- ./tmp/cache/forward.nginx.json:/app/tmp/cache/forward.nginx.json:ro
|
- ./tmp/cache/forward.nginx.json:/app/tmp/cache/forward.nginx.json:ro
|
||||||
restart: always
|
restart: always
|
||||||
|
cpanel:
|
||||||
|
build:
|
||||||
|
context: .
|
||||||
|
dockerfile: ./docker/cpanel/Dockerfile
|
||||||
|
links:
|
||||||
|
- app
|
||||||
|
volumes:
|
||||||
|
- ./d1/:/app/d1:ro
|
||||||
|
- ./tmp/d1/:/app/tmp/d1/:ro
|
||||||
|
restart: always
|
||||||
dynu:
|
dynu:
|
||||||
build:
|
build:
|
||||||
context: .
|
context: .
|
||||||
|
13
docker/cpanel/Dockerfile
Normal file
13
docker/cpanel/Dockerfile
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
FROM alpine:latest
|
||||||
|
RUN apk add openssh
|
||||||
|
RUN apk add python3
|
||||||
|
RUN apk add tini
|
||||||
|
RUN apk add bash curl
|
||||||
|
RUN apk add py3-pip
|
||||||
|
RUN pip3 install requests
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
ENTRYPOINT ["tini", "--"]
|
||||||
|
|
||||||
|
CMD ["python3", "d1/cpanel.py"]
|
@ -1039,6 +1039,7 @@ def http_server(argv):
|
|||||||
-u root \
|
-u root \
|
||||||
-it --entrypoint=/bin/bash \
|
-it --entrypoint=/bin/bash \
|
||||||
-v $PWD:/app:ro \
|
-v $PWD:/app:ro \
|
||||||
|
--log-driver none \
|
||||||
nginx:latest \
|
nginx:latest \
|
||||||
-c 'echo "server{listen 80; charset UTF-8; root /app; %s}" > /etc/nginx/conf.d/default.conf; nginx -g "daemon off;"'
|
-c 'echo "server{listen 80; charset UTF-8; root /app; %s}" > /etc/nginx/conf.d/default.conf; nginx -g "daemon off;"'
|
||||||
''' % (
|
''' % (
|
||||||
@ -1351,6 +1352,13 @@ def desktop_services(argv):
|
|||||||
action='store_true',
|
action='store_true',
|
||||||
help='decrease keyboard backlight',
|
help='decrease keyboard backlight',
|
||||||
)
|
)
|
||||||
|
parser.add_option(
|
||||||
|
'--backlight_service',
|
||||||
|
dest='backlight_service',
|
||||||
|
action='store_true',
|
||||||
|
default=False,
|
||||||
|
help='enable backlight_service',
|
||||||
|
)
|
||||||
|
|
||||||
options, args = parser.parse_args(argv)
|
options, args = parser.parse_args(argv)
|
||||||
|
|
||||||
@ -1864,7 +1872,10 @@ def desktop_services(argv):
|
|||||||
self.events = []
|
self.events = []
|
||||||
self.last_skip_loop = None
|
self.last_skip_loop = None
|
||||||
self.data = []
|
self.data = []
|
||||||
self.backlight = Backlight()
|
if options.backlight_service:
|
||||||
|
self.backlight = Backlight()
|
||||||
|
else:
|
||||||
|
self.backlight = None
|
||||||
self.bg = None
|
self.bg = None
|
||||||
self.bg_terminate = False
|
self.bg_terminate = False
|
||||||
|
|
||||||
@ -2123,7 +2134,8 @@ def desktop_services(argv):
|
|||||||
pprint.pprint(self.events)
|
pprint.pprint(self.events)
|
||||||
del self.events[:]
|
del self.events[:]
|
||||||
|
|
||||||
self.backlight.check()
|
if not self.backlight is None:
|
||||||
|
self.backlight.check()
|
||||||
|
|
||||||
self.background_check()
|
self.background_check()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user