|
1 | | -#!/usr/bin/env python3 |
| 1 | +#!/nix/store/qfcbvbm2wzpgh2m1j5b8yd61y8y8x5nw-isd-tui-env/bin/python |
2 | 2 | import http.server |
3 | | -import socketserver |
4 | 3 | import time |
5 | | -import threading |
6 | 4 | import sys |
7 | 5 | import random |
8 | | -from pathlib import Path |
9 | 6 |
|
10 | 7 | PORT = 8181 |
11 | | -HELLO_FILE = Path("/opt/hello/hello.txt") |
12 | 8 |
|
13 | 9 |
|
14 | 10 | class HelloHandler(http.server.SimpleHTTPRequestHandler): |
| 11 | + def __init__(self, *args, **kwargs): |
| 12 | + super().__init__(*args, **kwargs) |
| 13 | + |
15 | 14 | def do_GET(self): |
16 | 15 | self.send_response(200) |
17 | 16 | self.send_header("Content-type", "text/plain; charset=utf-8") |
18 | 17 | self.end_headers() |
19 | | - time.sleep(5) |
20 | | - try: |
21 | | - text = HELLO_FILE.read_text() |
22 | | - except Exception as e: |
23 | | - text = f"ERROR: cannot read {HELLO_FILE}: {e}" |
24 | | - self.wfile.write(text.encode("utf-8")) |
25 | | - |
26 | | - |
27 | | -def noisy_logger(): |
28 | | - """Generate noisy logs forever""" |
29 | | - while True: |
30 | | - r = random.randint(0, 2) |
31 | | - if r == 0: |
32 | | - print( |
33 | | - f"{time.ctime()} DEBUG: heartbeat from awesome-hello-world", |
34 | | - file=sys.stderr, |
35 | | - flush=True, |
36 | | - ) |
37 | | - elif r == 1: |
38 | | - print( |
39 | | - f"{time.ctime()} WARN: pretending to have triggered an warning in awesome-hello-world", |
40 | | - file=sys.stderr, |
41 | | - flush=True, |
42 | | - ) |
43 | | - else: |
44 | | - print( |
45 | | - f"{time.ctime()} LOG: pretending to log in awesome-hello-world", |
46 | | - file=sys.stderr, |
47 | | - flush=True, |
48 | | - ) |
49 | | - |
50 | | - time.sleep(0.5) |
| 18 | + self.wfile.write("Hello World!".encode("utf-8")) |
| 19 | + |
| 20 | + |
| 21 | +def log_debug(): |
| 22 | + print( |
| 23 | + f"{time.ctime()} DEBUG: heartbeat", |
| 24 | + file=sys.stderr, |
| 25 | + flush=True, |
| 26 | + ) |
| 27 | + |
| 28 | + |
| 29 | +def log_log(): |
| 30 | + print( |
| 31 | + f"{time.ctime()} LOG: logging something interesting...", |
| 32 | + file=sys.stderr, |
| 33 | + flush=True, |
| 34 | + ) |
51 | 35 |
|
52 | 36 |
|
53 | 37 | if __name__ == "__main__": |
54 | 38 | # Start background noisy logger |
55 | | - threading.Thread(target=noisy_logger, daemon=True).start() |
| 39 | + # add some noisy output |
| 40 | + for _ in range(30): |
| 41 | + r = random.randint(0, 1) |
| 42 | + if r == 0: |
| 43 | + log_debug() |
| 44 | + if r == 2: |
| 45 | + log_log() |
| 46 | + |
| 47 | + try: |
| 48 | + import socketserver |
| 49 | + |
| 50 | + with socketserver.TCPServer(("", PORT), HelloHandler) as httpd: |
| 51 | + # print(f"Serving on port {PORT}, reading from {HELLO_FILE}", flush=True) |
| 52 | + httpd.serve_forever() |
| 53 | + except Exception as e: |
| 54 | + # simulate shutting down |
| 55 | + print( |
| 56 | + f"{time.ctime()} ERROR: {e}", |
| 57 | + file=sys.stderr, |
| 58 | + flush=True, |
| 59 | + ) |
| 60 | + print( |
| 61 | + f"{time.ctime()} STOPPING: shutting down with delay", |
| 62 | + file=sys.stderr, |
| 63 | + flush=True, |
| 64 | + ) |
| 65 | + for _ in range(20): |
| 66 | + log_debug() |
56 | 67 |
|
57 | | - with socketserver.TCPServer(("", PORT), HelloHandler) as httpd: |
58 | | - # print(f"Serving on port {PORT}, reading from {HELLO_FILE}", flush=True) |
59 | | - httpd.serve_forever() |
| 68 | + exit(1) |
0 commit comments