github-actions[bot] commited on
Commit
e19359c
·
1 Parent(s): b0d8269

Auto-deploy from GitHub: aa24273d4c7a2734c1840f1bfd81f851eda4c7b4

Browse files
Files changed (1) hide show
  1. app.py +45 -0
app.py CHANGED
@@ -49,6 +49,49 @@ def start_worker():
49
  worker_thread.start()
50
  print("✅ Worker thread started")
51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  def worker_loop():
53
  """Main worker loop that processes audio files"""
54
  print("🤖 STT Worker started. Monitoring for new audio files...")
@@ -62,6 +105,8 @@ def worker_loop():
62
  import json
63
 
64
  while worker_running:
 
 
65
  try:
66
  # Get next unprocessed file
67
  conn = sqlite3.connect('audio_captions.db')
 
49
  worker_thread.start()
50
  print("✅ Worker thread started")
51
 
52
+ def cleanup_old_entries():
53
+ """Delete database entries and audio files older than 10 days"""
54
+ from datetime import timedelta
55
+
56
+ try:
57
+ conn = sqlite3.connect('audio_captions.db')
58
+ conn.row_factory = sqlite3.Row
59
+ c = conn.cursor()
60
+
61
+ # Calculate cutoff date (10 days ago)
62
+ cutoff_date = (datetime.now() - timedelta(days=10)).isoformat()
63
+
64
+ # First, get all old entries to delete their audio files
65
+ c.execute('''SELECT id, filepath FROM audio_files
66
+ WHERE created_at < ?''', (cutoff_date,))
67
+ old_entries = c.fetchall()
68
+
69
+ if old_entries:
70
+ deleted_files = 0
71
+ deleted_rows = 0
72
+
73
+ for entry in old_entries:
74
+ # Delete the audio file if it exists
75
+ filepath = entry['filepath']
76
+ if filepath and os.path.exists(filepath):
77
+ try:
78
+ os.remove(filepath)
79
+ deleted_files += 1
80
+ except Exception as e:
81
+ print(f"⚠️ Failed to delete old audio file {filepath}: {e}")
82
+
83
+ # Delete old database entries
84
+ c.execute('''DELETE FROM audio_files WHERE created_at < ?''', (cutoff_date,))
85
+ deleted_rows = c.rowcount
86
+ conn.commit()
87
+
88
+ if deleted_rows > 0 or deleted_files > 0:
89
+ print(f"🧹 Cleanup: Deleted {deleted_rows} old entries and {deleted_files} audio files (older than 10 days)")
90
+
91
+ conn.close()
92
+ except Exception as e:
93
+ print(f"⚠️ Cleanup error: {e}")
94
+
95
  def worker_loop():
96
  """Main worker loop that processes audio files"""
97
  print("🤖 STT Worker started. Monitoring for new audio files...")
 
105
  import json
106
 
107
  while worker_running:
108
+ # Run cleanup before processing each task
109
+ cleanup_old_entries()
110
  try:
111
  # Get next unprocessed file
112
  conn = sqlite3.connect('audio_captions.db')