Removed DeVries. Added process monitoring to collection process so it will restart if it dies. Seems to be a 2h limit on API, so reporting has been failing at the end of long races.

This commit is contained in:
tamservo 2023-07-24 16:36:36 -04:00
parent fb7387fc47
commit 090230f11a
3 changed files with 124 additions and 73 deletions

23
create_queue_db.py Normal file → Executable file
View File

@ -1,11 +1,12 @@
import sqlite3 #!/usr/bin/python3
import sqlite3
if __name__ == '__main__':
con = sqlite3.connect('messages.db') if __name__ == '__main__':
cur = con.cursor() con = sqlite3.connect('messages.db')
#cur.execute("""drop table messages""") cur = con.cursor()
cur.execute("""create table messages( id integer primary key, message );""") #cur.execute("""drop table messages""")
con.commit() cur.execute("""create table messages( id integer primary key, message );""")
cur.close() con.commit()
con.close() cur.close()
con.close()

View File

@ -1,9 +1,12 @@
#!/usr/bin/python3 #!/usr/bin/python3
import asyncio import asyncio
from asyncio.subprocess import PIPE, STDOUT
import collections.abc import collections.abc
import json import json
import os
import sqlite3 import sqlite3
from subprocess import Popen
import time import time
import discord import discord
@ -497,6 +500,7 @@ class Robottas(commands.Bot):
async def _report(self, ctx): async def _report(self, ctx):
self.is_reporting = True self.is_reporting = True
self.channel = ctx.channel self.channel = ctx.channel
await self.start_collect()
while self.is_reporting: while self.is_reporting:
# Do processing # Do processing
@ -515,17 +519,46 @@ class Robottas(commands.Bot):
# process any messages in the delay queue # process any messages in the delay queue
await self.process_delay_messages() await self.process_delay_messages()
#If collecting, make sure the collection process is running
if self.is_collecting:
if self.collector_proc == None or \
self.collector_proc.poll() != None:
await self.start_collect()
def get_token(self, token_file): def get_token(self, token_file):
with open(token_file) as tok: with open(token_file) as tok:
return tok.readline().strip() return tok.readline().strip()
async def start_collect(self):
self.is_collecting = True
dir_path = os.path.dirname(os.path.realpath(__file__))
command_txt = os.path.join(dir_path, self.collector_command)
command_txt += self.collector_params
print(f"command_txt: {command_txt}")
self.collector_proc = Popen(command_txt.split())
async def stop_collect(self):
self.is_collecting = False
try:
if self.collector_proc != None:
self.collector_proc.kill()
except:
print("Tried to kill collection process")
def __init__(self): def __init__(self):
# Set debug or not # Set debug or not
self.debug = True self.debug = True
# Discord authentication token # Discord authentication token
self.token = self.get_token("token.txt") self.token = self.get_token("token.txt")
self.collector_command = "robottas_collector.py"
self.collector_params = " save dummy.txt"
self.collector_proc = None
self.is_collecting = False
# Preface messages with the following # Preface messages with the following
self.report_preamble = ':robot::peach: Alert!' self.report_preamble = ':robot::peach: Alert!'
@ -580,7 +613,6 @@ class Robottas(commands.Bot):
'20': '<:MAG:1067883814992486510>', '20': '<:MAG:1067883814992486510>',
'23': '<:ALB:1067874026871074887>', '23': '<:ALB:1067874026871074887>',
'2': '<:SAR:1067890949197414410>', '2': '<:SAR:1067890949197414410>',
'21': '<:DEV:1067891622727131248>'
} }
# Holds dictionary for driver 3 letter code to icon # Holds dictionary for driver 3 letter code to icon
@ -606,7 +638,6 @@ class Robottas(commands.Bot):
'MAG': '<:MAG:1067883814992486510>', 'MAG': '<:MAG:1067883814992486510>',
'ALB': '<:ALB:1067874026871074887>', 'ALB': '<:ALB:1067874026871074887>',
'SAR': '<:SAR:1067890949197414410>', 'SAR': '<:SAR:1067890949197414410>',
'DEV': '<:DEV:1067891622727131248>'
} }
# Holds dictionary for race states to icons # Holds dictionary for race states to icons
@ -710,6 +741,7 @@ class Robottas(commands.Bot):
async def rbstop(ctx): async def rbstop(ctx):
self.is_reporting = False self.is_reporting = False
self.report_id = None self.report_id = None
await self.stop_collect()
await ctx.send(":robot::peach: powering down") await ctx.send(":robot::peach: powering down")
@ -778,6 +810,23 @@ class Robottas(commands.Bot):
await self._test_file(ctx) await self._test_file(ctx)
@self.command()
async def start_collect(ctx):
if str(ctx.author) == "tamservo#0" or ctx.author.guild_permissions.administrator:
# if an authorized user, start the collection script that
# puts records into the database
await self.start_collect()
@self.command()
async def calm(ctx):
file_name = os.path.dirname(os.path.realpath(__file__))
file_name = os.path.join(file_name, "images/calm.png")
with open(file_name, "rb") as handle:
df = discord.File(handle, filename=file_name)
await ctx.send(file = df)
if __name__ == '__main__': if __name__ == '__main__':
rb = Robottas() rb = Robottas()
rb.run_robottas() rb.run_robottas()

121
robottas_collector.py Normal file → Executable file
View File

@ -1,60 +1,61 @@
import argparse #!/usr/bin/python3
import sys import argparse
from RobottasSignalr import SignalRClient, messages_from_raw import sys
from RobottasSignalr import SignalRClient, messages_from_raw
def save(args):
mode = 'a' if args.append else 'w' def save(args):
client = SignalRClient(args.file, filemode=mode, debug=args.debug, mode = 'a' if args.append else 'w'
timeout=args.timeout) client = SignalRClient(args.file, filemode=mode, debug=args.debug,
client.start() timeout=args.timeout)
client.start()
def convert(args):
with open(args.input, 'r') as infile: def convert(args):
messages = infile.readlines() with open(args.input, 'r') as infile:
data, ec = messages_from_raw(messages) messages = infile.readlines()
with open(args.output, 'w') as outfile: data, ec = messages_from_raw(messages)
for elem in data: with open(args.output, 'w') as outfile:
outfile.write(str(elem)+'\n') for elem in data:
print(f"Completed with {ec} error(s)") outfile.write(str(elem)+'\n')
print(f"Completed with {ec} error(s)")
parser = argparse.ArgumentParser(
prog="python -m fastf1.livetiming", parser = argparse.ArgumentParser(
description="Save live timing data during a session", prog="python -m fastf1.livetiming",
formatter_class=argparse.ArgumentDefaultsHelpFormatter description="Save live timing data during a session",
) formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
subparsers = parser.add_subparsers()
subparsers = parser.add_subparsers()
rec_parser = subparsers.add_parser(
'save', help='Save live timing data' rec_parser = subparsers.add_parser(
) 'save', help='Save live timing data'
conv_parser = subparsers.add_parser( )
'extract', help='Extract messages from saved debug-mode data' conv_parser = subparsers.add_parser(
) 'extract', help='Extract messages from saved debug-mode data'
)
rec_parser.add_argument('file', type=str, help='Output file name')
rec_parser.add_argument('--append', action='store_true', default=False, rec_parser.add_argument('file', type=str, help='Output file name')
help="Append to output file. By default the file is " rec_parser.add_argument('--append', action='store_true', default=False,
"overwritten if it exists already.") help="Append to output file. By default the file is "
rec_parser.add_argument('--debug', action='store_true', default=False, "overwritten if it exists already.")
help='Enable debug mode: save full SignalR message, ' rec_parser.add_argument('--debug', action='store_true', default=False,
'not just the data.') help='Enable debug mode: save full SignalR message, '
rec_parser.add_argument('--timeout', type=int, default=60, 'not just the data.')
help='Timeout in seconds after which the client will ' rec_parser.add_argument('--timeout', type=int, default=60,
'automatically exit if no data is received.') help='Timeout in seconds after which the client will '
rec_parser.set_defaults(func=save) 'automatically exit if no data is received.')
rec_parser.set_defaults(func=save)
conv_parser.add_argument("input", type=str, help='Input file name')
conv_parser.add_argument("output", type=str, help='Output file name') conv_parser.add_argument("input", type=str, help='Input file name')
conv_parser.set_defaults(func=convert) conv_parser.add_argument("output", type=str, help='Output file name')
conv_parser.set_defaults(func=convert)
if not len(sys.argv) > 1:
# user did not provide any arguments if not len(sys.argv) > 1:
parser.print_help() # user did not provide any arguments
parser.exit(1) parser.print_help()
parser.exit(1)
args = parser.parse_args()
args.func(args) # call function associated with subparser args = parser.parse_args()
args.func(args) # call function associated with subparser