Skip to content
This repository has been archived by the owner on Aug 6, 2023. It is now read-only.

Commit

Permalink
Fix file crash errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Ali-Toosi committed Apr 21, 2022
1 parent 0ec50ed commit 30d063d
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 27 deletions.
8 changes: 4 additions & 4 deletions app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@


create_game_groups = not os.path.isfile('../data/groups.json')
number_of_groups = 2 # 8
number_of_groups = 1 # 8

create_game_matches = not os.path.isfile('../data/matches.json')
matching_strategy = 'random'
# matching_strategy = 'full'
random_matching_matches_per_team = 1000 # None
# matching_strategy = 'random'
matching_strategy = 'full'
random_matching_matches_per_team = 1 # None
1 change: 1 addition & 0 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def heartbeat():
normalized_teams = [x.__dict__ for x in teams]
normalized_matches = [x.__dict__ for x in matches]
normalized_groups = groups
save_all_matches(matches)
return json_response({
'teams': normalized_teams,
'groups': normalized_groups,
Expand Down
81 changes: 59 additions & 22 deletions app/match.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
from logs import log
from lock import lock_matches, unlock_matches
from random import random
import shutil
import os


_last_backup = 0
_matches = []


class Match:
Expand Down Expand Up @@ -37,41 +40,69 @@ def unassign(self):
self.start_time = None

def is_expired(self):
return self.start_time is not None and int(time.time()) - self.start_time >= 20 * 60
return self.status == self.IN_PROGRESS and self.start_time is not None and int(time.time()) - self.start_time >= 20 * 60


def save_all_matches(matches, lock=True):
global _last_backup
if lock:
lock_matches()
try:
normalized_matches = [match.__dict__ for match in matches]
if time.time() - _last_backup >= 60:
with open('../data/matches_backup.json', 'w') as f:
f.write(json.dumps(normalized_matches))
_last_backup = time.time()
global _matches
normalized_matches = [match.__dict__ for match in matches]
if time.time() - _last_backup >= 60:
with open('../data/matches.json', 'w') as f:
f.write(json.dumps(normalized_matches))
time.sleep(random()/8)
except Exception:
pass
if lock:
unlock_matches()
_last_backup = time.time()
_matches = matches
# if lock:
# lock_matches()
# try:
# normalized_matches = [match.__dict__ for match in matches]
# if time.time() - _last_backup >= 60:
# with open('../data/matches_backup.json', 'w') as f:
# f.write(json.dumps(normalized_matches))
# _last_backup = time.time()
# with open('../data/matches.json', 'w') as f:
# f.write(json.dumps(normalized_matches))
# time.sleep(random()/8)
# except Exception:
# pass
# if lock:
# unlock_matches()


def load_matches(lock=True):
if lock:
lock_matches()
result = []
global _matches
if len(_matches) > 0:
return _matches
try:
with open('../data/matches.json', 'r') as f:
raw_matches = json.loads(f.read())
result = [Match.from_dict(raw_match) for raw_match in raw_matches]
except Exception:
_matches = [Match.from_dict(raw_match) for raw_match in raw_matches]
except Exception as e:
log('Unable to load the matches from json file')
if lock:
unlock_matches()
return result
log(str(e))
return _matches
# if lock:
# lock_matches()
# result = []
# restored = 0
# while restored < 2:
# try:
# with open('../data/matches.json', 'r') as f:
# raw_matches = json.loads(f.read())
# result = [Match.from_dict(raw_match) for raw_match in raw_matches]
# break
# except Exception as e:
# log('Unable to load the matches from json file')
# log(str(e))
# try:
# restore_matches()
# except Exception as ee:
# log('Failed to restore matches')
# log(str(ee))
# restored += 1
# if lock:
# unlock_matches()
# return result


def unassign_all_expired_matches(matches, lock=True):
Expand All @@ -82,3 +113,9 @@ def unassign_all_expired_matches(matches, lock=True):
match.unassign()
if found_any:
save_all_matches(matches, lock)


def restore_matches():
if os.path.isfile('../data/matches.json'):
os.remove('../data/matches.json')
shutil.copyfile('../data/matches_backup.json', '../data/matches.json')
4 changes: 3 additions & 1 deletion uwsgi.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
module = main
callable = app
master = true
touch-reload = /app/uwsgi.ini
touch-reload = /app/uwsgi.ini
processes = 1
cheaper = 0

0 comments on commit 30d063d

Please sign in to comment.