Merge pull request #133 from xSke/resilience-fixes

Resilience fixes
This commit is contained in:
Sakimori 2021-01-06 19:58:18 -05:00 committed by GitHub
commit e4cf9f584e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 7 deletions

2
.gitignore vendored
View File

@ -347,4 +347,6 @@ ids
# database # database
matteo.db matteo.db
matteo.db-wal
matteo.db-shm
/matteo_env/Lib/site-packages/flask_socketio/__init__.py /matteo_env/Lib/site-packages/flask_socketio/__init__.py

View File

@ -2,12 +2,17 @@
import os, json, datetime, re import os, json, datetime, re
import sqlite3 as sql import sqlite3 as sql
data_dir = "data"
def create_connection(): def create_connection():
#create connection, create db if doesn't exist #create connection, create db if doesn't exist
conn = None conn = None
try: try:
conn = sql.connect("matteo.db") conn = sql.connect(os.path.join(data_dir, "matteo.db"))
# enable write-ahead log for performance and resilience
conn.execute('pragma journal_mode=wal')
return conn return conn
except: except:
print("oops, db connection no work") print("oops, db connection no work")

View File

@ -2,8 +2,13 @@ import json, random, os, math, jsonpickle
from enum import Enum from enum import Enum
import database as db import database as db
data_dir = "data"
games_config_file = os.path.join(data_dir, "games_config.json")
def config(): def config():
if not os.path.exists("games_config.json"): if not os.path.exists(os.path.dirname(games_config_file)):
os.makedirs(os.path.dirname(games_config_file))
if not os.path.exists(games_config_file):
#generate default config #generate default config
config_dic = { config_dic = {
"default_length" : 3, "default_length" : 3,
@ -16,11 +21,11 @@ def config():
"stolen_base_chance_mod" : 1, "stolen_base_chance_mod" : 1,
"stolen_base_success_mod" : 1 "stolen_base_success_mod" : 1
} }
with open("games_config.json", "w") as config_file: with open(games_config_file, "w") as config_file:
json.dump(config_dic, config_file, indent=4) json.dump(config_dic, config_file, indent=4)
return config_dic return config_dic
else: else:
with open("games_config.json") as config_file: with open(games_config_file) as config_file:
return json.load(config_file) return json.load(config_file)
def all_weathers(): def all_weathers():

View File

@ -4,6 +4,8 @@ import onomancer as ono
from flask import Flask from flask import Flask
from uuid import uuid4 from uuid import uuid4
data_dir = "data"
config_filename = os.path.join(data_dir, "config.json")
class Command: class Command:
def isauthorized(self, user): def isauthorized(self, user):
@ -605,7 +607,9 @@ thread1 = threading.Thread(target=main_controller.update_loop)
thread1.start() thread1.start()
def config(): def config():
if not os.path.exists("config.json"): if not os.path.exists(os.path.dirname(config_filename)):
os.makedirs(os.path.dirname(config_filename))
if not os.path.exists(config_filename):
#generate default config #generate default config
config_dic = { config_dic = {
"token" : "", "token" : "",
@ -617,12 +621,12 @@ def config():
"soulscream channel id" : 0, "soulscream channel id" : 0,
"game_freeze" : 0 "game_freeze" : 0
} }
with open("config.json", "w") as config_file: with open(config_filename, "w") as config_file:
json.dump(config_dic, config_file, indent=4) json.dump(config_dic, config_file, indent=4)
print("please fill in bot token and any bot admin discord ids to the new config.json file!") print("please fill in bot token and any bot admin discord ids to the new config.json file!")
quit() quit()
else: else:
with open("config.json") as config_file: with open(config_filename) as config_file:
return json.load(config_file) return json.load(config_file)
@client.event @client.event