This commit is contained in:
@@ -1,39 +1,65 @@
|
|||||||
import logging
|
import logging
|
||||||
|
|
||||||
import stomp
|
import stomp
|
||||||
import json
|
|
||||||
import time
|
import time
|
||||||
|
import yaml
|
||||||
import msgspec
|
import msgspec
|
||||||
|
from pathlib import Path
|
||||||
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
|
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
|
||||||
|
from platformdirs import PlatformDirs
|
||||||
|
|
||||||
|
|
||||||
parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter)
|
parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter)
|
||||||
|
parser.add_argument("--config", "-c", default="kontor-docker")
|
||||||
parser.add_argument('--verbose', '-v', action='count', default=0)
|
parser.add_argument('--verbose', '-v', action='count', default=0)
|
||||||
parser.add_argument("--server", "-s", default="127.0.0.1")
|
parser.add_argument("--server", "-s", default="127.0.0.1")
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
def get_logger(level: int, config: str):
|
||||||
|
"""
|
||||||
|
create Logger with configuration from config file
|
||||||
|
"""
|
||||||
|
dirs = PlatformDirs(config)
|
||||||
|
logging_config = Path(dirs.user_config_dir, "logging-config.yaml")
|
||||||
|
with open(logging_config, "rt", encoding="UTF-8") as f:
|
||||||
|
config_dict = yaml.safe_load(f.read())
|
||||||
|
logging.config.dictConfig(config_dict)
|
||||||
|
log = logging.getLogger("development")
|
||||||
|
if level is not None:
|
||||||
|
match level:
|
||||||
|
case 0:
|
||||||
|
log.setLevel(logging.INFO)
|
||||||
|
case 1:
|
||||||
|
log.setLevel(logging.DEBUG)
|
||||||
|
case _:
|
||||||
|
log.setLevel(logging.CRITICAL)
|
||||||
|
return log
|
||||||
|
|
||||||
|
|
||||||
class Link(msgspec.Struct):
|
class Link(msgspec.Struct):
|
||||||
url: str
|
url: str
|
||||||
|
|
||||||
class MyListener(stomp.ConnectionListener):
|
class MyListener(stomp.ConnectionListener):
|
||||||
def __init__(self):
|
def __init__(self, log):
|
||||||
|
self.log = log
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def on_error(self, frame):
|
def on_error(self, frame):
|
||||||
logger.info("received an error %s", frame.body)
|
self.log.info("received an error %s", frame.body)
|
||||||
|
|
||||||
def on_message(self, frame):
|
def on_message(self, frame):
|
||||||
logger.info("received a message %s", frame.body)
|
self.log.info("received a message %s", frame.body)
|
||||||
link = msgspec.json.decode(frame.body, type=Link)
|
link = msgspec.json.decode(frame.body, type=Link)
|
||||||
logger.info("found link: %s", link.url)
|
self.log.info("found link: %s", link.url)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
logger.setLevel(logging.INFO)
|
logger = get_logger(args.verbose, args.config)
|
||||||
logger.info("kontor.read_queue started")
|
logger.info("kontor.read_queue started")
|
||||||
host = [(args.server, 61616)]
|
host = [(args.server, 61616)]
|
||||||
conn = stomp.Connection(host_and_ports=host)
|
conn = stomp.Connection(host_and_ports=host)
|
||||||
conn.set_listener('', MyListener())
|
conn.set_listener('', MyListener(logger))
|
||||||
conn.connect(username='artemis', passcode='artemis', wait=True)
|
conn.connect(username='artemis', passcode='artemis', wait=True)
|
||||||
conn.subscribe(destination='add_link', id=1, ack='auto', headers={})
|
conn.subscribe(destination='add_link', id=1, ack='auto', headers={})
|
||||||
time.sleep(5)
|
time.sleep(5)
|
||||||
|
|||||||
Reference in New Issue
Block a user