diff --git a/d1/cpanel.py b/d1/cpanel.py new file mode 100644 index 0000000..d285874 --- /dev/null +++ b/d1/cpanel.py @@ -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) diff --git a/d1/f2.py b/d1/f2.py index 0aac2b4..c51a442 100644 --- a/d1/f2.py +++ b/d1/f2.py @@ -20,7 +20,7 @@ sys.path.insert(0, os.path.dirname(__file__)) class Application: - MAX_FILE_SIZE = 2 * 1024 * 1024 * 1024 + MAX_FILE_SIZE = 4 * 1024 * 1024 * 1024 CHUNK_SIZE = 4096 MAX_OUTPUT_SIZE = 4 * 1024 * 1024 LOG_SIZE = 10 * 1024 * 1024 diff --git a/docker-compose.yml b/docker-compose.yml index 29b0c90..3a0e3d2 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -8,6 +8,16 @@ services: - ./d1/nginx_config.py:/app/d1/nginx_config.py:ro - ./tmp/cache/forward.nginx.json:/app/tmp/cache/forward.nginx.json:ro 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: build: context: . diff --git a/docker/cpanel/Dockerfile b/docker/cpanel/Dockerfile new file mode 100644 index 0000000..9d0c81c --- /dev/null +++ b/docker/cpanel/Dockerfile @@ -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"] diff --git a/dotfiles/.local/bin/commands b/dotfiles/.local/bin/commands index 60c87ef..812d251 100755 --- a/dotfiles/.local/bin/commands +++ b/dotfiles/.local/bin/commands @@ -1039,6 +1039,7 @@ def http_server(argv): -u root \ -it --entrypoint=/bin/bash \ -v $PWD:/app:ro \ + --log-driver none \ nginx:latest \ -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', 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) @@ -1864,7 +1872,10 @@ def desktop_services(argv): self.events = [] self.last_skip_loop = None self.data = [] - self.backlight = Backlight() + if options.backlight_service: + self.backlight = Backlight() + else: + self.backlight = None self.bg = None self.bg_terminate = False @@ -2123,7 +2134,8 @@ def desktop_services(argv): pprint.pprint(self.events) del self.events[:] - self.backlight.check() + if not self.backlight is None: + self.backlight.check() self.background_check()