Compare commits

..

2 Commits

Author SHA1 Message Date
d61c1d0f1b add files for docker 2023-02-12 02:14:53 +09:00
29cba6e079 improve event loop to end gracefully 2023-02-12 02:14:45 +09:00
3 changed files with 29 additions and 2 deletions

7
Dockerfile Normal file
View File

@ -0,0 +1,7 @@
FROM python:3-alpine
WORKDIR /repo
ADD requirements.txt /repo
RUN pip3 install -r requirements.txt
CMD ["python3", "main.py"]

8
docker-compose.yml Normal file
View File

@ -0,0 +1,8 @@
version: '3'
services:
maidbot:
build:
context: .
volumes:
- ./:/repo

16
main.py
View File

@ -1,6 +1,7 @@
import logging import logging
import json import json
import os import os
import signal
import time import time
from datetime import datetime, timezone from datetime import datetime, timezone
@ -54,6 +55,10 @@ class App:
self.mods = {} self.mods = {}
self.alive = True
signal.signal(signal.SIGTERM, lambda a,b: self.exit())
signal.signal(signal.SIGINT, lambda a,b: self.exit())
def authenticate(self): def authenticate(self):
if os.path.exists("auth.json"): if os.path.exists("auth.json"):
with open("auth.json", "r") as f: with open("auth.json", "r") as f:
@ -92,7 +97,7 @@ class App:
def run(self): def run(self):
self.install("elicense", mods.elicense) self.install("elicense", mods.elicense)
while True: while self.alive:
try: try:
self.cycle() self.cycle()
self.logging.debug("cycle done") self.logging.debug("cycle done")
@ -101,7 +106,10 @@ class App:
for mod in self.mods: for mod in self.mods:
self.mods[mod].cycle() self.mods[mod].cycle()
time.sleep(self.CONFIG["interval"])
for i in range(self.CONFIG["interval"]):
if not self.alive: break
time.sleep(1)
def cycle(self): def cycle(self):
items = self.M.notifications( items = self.M.notifications(
@ -117,6 +125,10 @@ class App:
# TODO: call mod cycles # TODO: call mod cycles
def exit(self):
self.alive = False
self.logging.info("exit requested")
def reply(self, st, msg): def reply(self, st, msg):
try: try:
self.logging.debug("reply:", msg) self.logging.debug("reply:", msg)