Fengx1n commited on
Commit
cfb310a
·
1 Parent(s): ec1b244

fix: log remove

Browse files
Files changed (1) hide show
  1. app.py +12 -4
app.py CHANGED
@@ -42,7 +42,7 @@ all_actions = [
42
 
43
  game_pids = {}
44
 
45
- alive_game_ids = set()
46
 
47
 
48
  def remove_old_game_dirs():
@@ -52,10 +52,17 @@ def remove_old_game_dirs():
52
  run_lock = FileLock(os.path.join(".", "runs", "run.lock"))
53
  with run_lock:
54
  os.system(f"rm -rf {os.path.join('.', 'runs', game_id)}")
 
 
 
 
 
 
 
55
 
56
  for game_id in list(alive_game_ids):
57
  if not os.path.exists(os.path.join(".", "runs", game_id)):
58
- alive_game_ids.discard(game_id)
59
 
60
  print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), " - Cleaned up old game directories.")
61
  print("Current alive game IDs:", alive_game_ids)
@@ -77,7 +84,7 @@ def start_game(game, level, state, req: gr.Request):
77
 
78
  # game_id = time.strftime('%Y-%m-%d-%H:%M:%S',time.localtime(time.time()))
79
  game_id = req.session_hash
80
- alive_game_ids.add(game_id)
81
  # print("start game: game_id: ", game_id)
82
 
83
  if game_pids.get(game_id):
@@ -126,7 +133,8 @@ def write_action(action, state):
126
 
127
  def cleanup(req: gr.Request):
128
  game_id = req.session_hash
129
- alive_game_ids.discard(game_id)
 
130
  pid = game_pids.get(game_id)
131
  if pid:
132
  try:
 
42
 
43
  game_pids = {}
44
 
45
+ alive_game_ids = {}
46
 
47
 
48
  def remove_old_game_dirs():
 
52
  run_lock = FileLock(os.path.join(".", "runs", "run.lock"))
53
  with run_lock:
54
  os.system(f"rm -rf {os.path.join('.', 'runs', game_id)}")
55
+ elif (datetime.now() - datetime.strptime(alive_game_ids[game_id], '%Y-%m-%d-%H:%M:%S')).days > 1:
56
+ # If the game directory is older than 1 day, remove it
57
+ run_lock = FileLock(os.path.join(".", "runs", "run.lock"))
58
+ with run_lock:
59
+ os.system(f"rm -rf {os.path.join('.', 'runs', game_id)}")
60
+ alive_game_ids.pop(game_id)
61
+
62
 
63
  for game_id in list(alive_game_ids):
64
  if not os.path.exists(os.path.join(".", "runs", game_id)):
65
+ alive_game_ids.pop(game_id)
66
 
67
  print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), " - Cleaned up old game directories.")
68
  print("Current alive game IDs:", alive_game_ids)
 
84
 
85
  # game_id = time.strftime('%Y-%m-%d-%H:%M:%S',time.localtime(time.time()))
86
  game_id = req.session_hash
87
+ alive_game_ids[game_id] = datetime.now().strftime('%Y-%m-%d-%H:%M:%S')
88
  # print("start game: game_id: ", game_id)
89
 
90
  if game_pids.get(game_id):
 
133
 
134
  def cleanup(req: gr.Request):
135
  game_id = req.session_hash
136
+ if game_id in alive_game_ids:
137
+ alive_game_ids.pop(game_id)
138
  pid = game_pids.get(game_id)
139
  if pid:
140
  try: