1
0
mirror of https://github.com/craigerl/aprsd.git synced 2024-09-28 16:16:51 -04:00

Added support to save/load MsgTrack on exit/start

This patch added saving of the MsgTrack list of messages at aprsd exit.
The will be loaded at startup unless you pass in the --flush option.
This commit is contained in:
Hemna 2020-12-30 07:32:20 -05:00
parent 2e90c0bdbb
commit 2659a0b3b9
3 changed files with 63 additions and 10 deletions

View File

@ -150,7 +150,6 @@ def signal_handler(signal, frame):
) )
threads.APRSDThreadList().stop_all() threads.APRSDThreadList().stop_all()
server_event.set() server_event.set()
sys.exit(0) # thread ignores this
# end signal_handler # end signal_handler
@ -333,7 +332,16 @@ def send_message(
default=utils.DEFAULT_CONFIG_FILE, default=utils.DEFAULT_CONFIG_FILE,
help="The aprsd config file to use for options.", help="The aprsd config file to use for options.",
) )
def server(loglevel, quiet, disable_validation, config_file): @click.option(
"-f",
"--flush",
"flush",
is_flag=True,
show_default=True,
default=False,
help="Flush out all old aged messages on disk.",
)
def server(loglevel, quiet, disable_validation, config_file, flush):
"""Start the aprsd server process.""" """Start the aprsd server process."""
global event global event
@ -364,6 +372,15 @@ def server(loglevel, quiet, disable_validation, config_file):
plugin_manager.setup_plugins() plugin_manager.setup_plugins()
client.Client(config) client.Client(config)
# Now load the msgTrack from disk if any
if flush:
LOG.debug("Deleting saved MsgTrack.")
messaging.MsgTrack().flush()
else:
# Try and load saved MsgTrack list
LOG.debug("Loading saved MsgTrack object.")
messaging.MsgTrack().load()
rx_msg_queue = queue.Queue(maxsize=20) rx_msg_queue = queue.Queue(maxsize=20)
tx_msg_queue = queue.Queue(maxsize=20) tx_msg_queue = queue.Queue(maxsize=20)
msg_queues = { msg_queues = {
@ -378,6 +395,8 @@ def server(loglevel, quiet, disable_validation, config_file):
rx_thread.start() rx_thread.start()
tx_thread.start() tx_thread.start()
messaging.MsgTrack().restart()
cntr = 0 cntr = 0
while not server_event.is_set(): while not server_event.is_set():
# to keep the log noise down # to keep the log noise down
@ -387,9 +406,10 @@ def server(loglevel, quiet, disable_validation, config_file):
cntr += 1 cntr += 1
time.sleep(10) time.sleep(10)
# If there are items in the msgTracker, then save them
tracker = messaging.MsgTrack()
tracker.save()
LOG.info("APRSD Exiting.") LOG.info("APRSD Exiting.")
sys.exit(0)
# setup and run the main blocking loop
if __name__ == "__main__": if __name__ == "__main__":

View File

@ -1,12 +1,15 @@
import abc import abc
import datetime import datetime
import logging import logging
import os
import pathlib
import pickle
import re import re
import threading import threading
import time import time
from multiprocessing import RawValue from multiprocessing import RawValue
from aprsd import client, threads from aprsd import client, threads, utils
LOG = logging.getLogger("APRSD") LOG = logging.getLogger("APRSD")
@ -44,6 +47,7 @@ class MsgTrack(object):
lock = None lock = None
track = {} track = {}
total_messages_tracked = 0
def __new__(cls, *args, **kwargs): def __new__(cls, *args, **kwargs):
if cls._instance is None: if cls._instance is None:
@ -56,6 +60,7 @@ class MsgTrack(object):
with self.lock: with self.lock:
key = int(msg.id) key = int(msg.id)
self.track[key] = msg self.track[key] = msg
self.total_messages_tracked += 1
def get(self, id): def get(self, id):
with self.lock: with self.lock:
@ -82,11 +87,35 @@ class MsgTrack(object):
def save(self): def save(self):
"""Save this shit to disk?""" """Save this shit to disk?"""
pass if len(self) > 0:
LOG.info("Need to save tracking to disk")
pickle.dump(self.dump(), open(utils.DEFAULT_SAVE_FILE, "wb+"))
def restore(self): def dump(self):
"""Restore this shit?""" dump = {}
pass with self.lock:
for key in self.track.keys():
dump[key] = self.track[key]
return dump
def load(self):
if os.path.exists(utils.DEFAULT_SAVE_FILE):
raw = pickle.load(open(utils.DEFAULT_SAVE_FILE, "rb"))
if raw:
self.track = raw
LOG.debug("Loaded MsgTrack dict from disk.")
LOG.debug(self)
def restart(self):
"""Walk the list of messages and restart them if any."""
for key in self.track.keys():
msg = self.track[key]
msg.send()
def flush(self):
"""Nuke the old pickle file that stored the old results from last aprsd run."""
pathlib.Path(utils.DEFAULT_SAVE_FILE).unlink()
class MessageCounter(object): class MessageCounter(object):

View File

@ -5,6 +5,7 @@ import functools
import os import os
import sys import sys
import threading import threading
from pathlib import Path
import click import click
import yaml import yaml
@ -46,7 +47,10 @@ DEFAULT_CONFIG_DICT = {
}, },
} }
DEFAULT_CONFIG_FILE = "~/.config/aprsd/aprsd.yml" home = str(Path.home())
DEFAULT_CONFIG_DIR = "{}/.config/aprsd/".format(home)
DEFAULT_SAVE_FILE = "{}/.config/aprsd/aprsd.p".format(home)
DEFAULT_CONFIG_FILE = "{}/.config/aprsd/aprsd.yml".format(home)
def synchronized(wrapped): def synchronized(wrapped):