-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy pathapp.py
More file actions
359 lines (288 loc) · 11.1 KB
/
Copy pathapp.py
File metadata and controls
359 lines (288 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
from flask import Flask, render_template, request, send_file, Response, jsonify
import os
import shutil
import uuid
import queue
import threading
import time
import gc
from downloader import WebsiteDownloader, zip_directory, get_site_name
app = Flask(__name__)
DOWNLOAD_FOLDER = 'downloads'
if not os.path.exists(DOWNLOAD_FOLDER):
os.makedirs(DOWNLOAD_FOLDER)
# Tunable retention windows (seconds)
COMPLETE_TTL = 1800 # complete sessions (zip waiting for download)
ERROR_TTL = 600 # error sessions
PROCESSING_TTL = 1800 # safety net for stuck/zombie sessions
ORPHAN_FILE_TTL = 1800 # files on disk with no matching session
CLEANUP_INTERVAL = 300 # how often the janitor runs
def cleanup_downloads_folder():
"""Remove all files and folders from downloads directory."""
try:
for item in os.listdir(DOWNLOAD_FOLDER):
item_path = os.path.join(DOWNLOAD_FOLDER, item)
if os.path.isfile(item_path):
os.remove(item_path)
elif os.path.isdir(item_path):
shutil.rmtree(item_path)
print("🧹 Pasta downloads limpa com sucesso")
except Exception as e:
print(f"⚠️ Erro ao limpar pasta downloads: {e}")
cleanup_downloads_folder()
# Per-session state. Always touch via session_lock when iterating/mutating.
message_queues = {}
download_results = {}
session_lock = threading.Lock()
def _purge_session(session_id):
"""Remove a single session's in-memory state and any disk artifacts."""
with session_lock:
result = download_results.pop(session_id, None)
message_queues.pop(session_id, None)
if not result:
return
zip_path = result.get('zip_path')
if zip_path and os.path.exists(zip_path):
try:
os.remove(zip_path)
except Exception:
pass
# Some error paths may leave the raw directory behind.
raw_dir = os.path.join(DOWNLOAD_FOLDER, session_id)
if os.path.isdir(raw_dir):
try:
shutil.rmtree(raw_dir)
except Exception:
pass
def _cleanup_orphan_files():
"""
Remove files/dirs in downloads/ that don't belong to any active session.
Catches leftovers from worker crashes or restarts.
"""
try:
with session_lock:
known_ids = set(download_results.keys())
now = time.time()
for entry in os.listdir(DOWNLOAD_FOLDER):
path = os.path.join(DOWNLOAD_FOLDER, entry)
try:
age = now - os.path.getmtime(path)
except OSError:
continue
# Strip trailing .zip to recover the session uuid
base = entry[:-4] if entry.endswith('.zip') else entry
if base in known_ids:
continue
if age < ORPHAN_FILE_TTL:
continue
try:
if os.path.isfile(path):
os.remove(path)
print(f"🗑️ Removido arquivo órfão: {entry}")
elif os.path.isdir(path):
shutil.rmtree(path)
print(f"🗑️ Removido diretório órfão: {entry}")
except Exception:
pass
except Exception as e:
print(f"⚠️ Erro varrendo órfãos: {e}")
def cleanup_abandoned_sessions():
"""
Janitor thread: removes complete/error/zombie sessions and orphan files.
Runs every CLEANUP_INTERVAL seconds.
"""
while True:
time.sleep(CLEANUP_INTERVAL)
try:
now = time.time()
to_remove = []
with session_lock:
snapshot = list(download_results.items())
for session_id, result in snapshot:
status = result.get('status')
created_at = result.get('created_at') or result.get('started_at') or 0
if not created_at:
continue
age = now - created_at
if status == 'complete' and age > COMPLETE_TTL:
to_remove.append((session_id, 'complete'))
elif status == 'error' and age > ERROR_TTL:
to_remove.append((session_id, 'error'))
elif status == 'processing' and age > PROCESSING_TTL:
to_remove.append((session_id, 'zombie'))
for session_id, reason in to_remove:
_purge_session(session_id)
print(f"🧹 Sessão {session_id[:8]} removida ({reason})")
_cleanup_orphan_files()
gc.collect()
except Exception as e:
print(f"⚠️ Erro no janitor: {e}")
threading.Thread(target=cleanup_abandoned_sessions, daemon=True).start()
@app.route('/')
def index():
return render_template('index.html')
@app.route('/health')
def health():
"""Lightweight health endpoint with memory + session counts for monitoring."""
info = {'status': 'ok'}
with session_lock:
info['sessions'] = len(download_results)
info['queues'] = len(message_queues)
try:
import psutil
proc = psutil.Process()
info['rss_mb'] = round(proc.memory_info().rss / (1024 * 1024), 1)
except Exception:
# psutil is optional - fall back to resource module on POSIX
try:
import resource
rss_kb = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
# macOS reports bytes, Linux reports kilobytes
divisor = 1024 * 1024 if os.uname().sysname == 'Darwin' else 1024
info['rss_mb'] = round(rss_kb / divisor, 1)
except Exception:
pass
return jsonify(info)
@app.route('/start-download', methods=['POST'])
def start_download():
"""Start download process and return session ID for SSE."""
data = request.get_json(silent=True) or {}
url = data.get('url')
if not url:
return jsonify({'error': 'URL is required'}), 400
session_id = str(uuid.uuid4())
now = time.time()
with session_lock:
message_queues[session_id] = queue.Queue()
download_results[session_id] = {
'status': 'processing',
'zip_path': None,
'filename': None,
'started_at': now,
}
thread = threading.Thread(target=process_download, args=(session_id, url))
thread.daemon = True
thread.start()
return jsonify({'session_id': session_id})
def process_download(session_id, url):
"""Background download worker."""
with session_lock:
q = message_queues.get(session_id)
if q is None:
return
download_dir = os.path.join(DOWNLOAD_FOLDER, session_id)
zip_path = os.path.join(DOWNLOAD_FOLDER, f"{session_id}.zip")
def log_callback(message):
q.put(message)
downloader = None
try:
downloader = WebsiteDownloader(url, download_dir, log_callback=log_callback)
success = downloader.process()
if not success:
q.put("❌ Falha no download")
with session_lock:
download_results[session_id] = {
'status': 'error',
'error': 'Failed to download site',
'created_at': time.time(),
}
return
site_name = get_site_name(url)
zip_filename = f"{site_name}.zip"
q.put("📦 Criando arquivo ZIP...")
zip_directory(download_dir, zip_path)
# Free raw files immediately
if os.path.isdir(download_dir):
shutil.rmtree(download_dir, ignore_errors=True)
q.put("🎉 Download pronto!")
with session_lock:
download_results[session_id] = {
'status': 'complete',
'zip_path': zip_path,
'filename': zip_filename,
'created_at': time.time(),
}
except Exception as e:
q.put(f"❌ Erro: {str(e)}")
with session_lock:
download_results[session_id] = {
'status': 'error',
'error': str(e),
'created_at': time.time(),
}
# Best-effort cleanup of partial artifacts
if os.path.exists(download_dir):
shutil.rmtree(download_dir, ignore_errors=True)
if os.path.exists(zip_path):
try:
os.remove(zip_path)
except Exception:
pass
finally:
# Drop downloader reference so its in-memory buffers can be GC'd
downloader = None
gc.collect()
@app.route('/stream/<session_id>')
def stream(session_id):
"""SSE endpoint for log streaming."""
def generate():
with session_lock:
q = message_queues.get(session_id)
if q is None:
yield "data: ❌ Sessão não encontrada\n\n"
yield "event: done\ndata: error\n\n"
return
# Hard cap how long a single SSE connection can live to avoid
# accumulating zombie generators.
deadline = time.time() + 30 * 60 # 30 minutes
while True:
if time.time() > deadline:
yield "data: ⏱️ Conexão encerrada por inatividade\n\n"
yield "event: done\ndata: timeout\n\n"
return
try:
message = q.get(timeout=30)
yield f"data: {message}\n\n"
with session_lock:
result = download_results.get(session_id, {})
if result.get('status') in ('complete', 'error'):
yield f"event: done\ndata: {result['status']}\n\n"
return
except queue.Empty:
with session_lock:
result = download_results.get(session_id, {})
# Worker died/finished without final message - don't hang forever
if result.get('status') in ('complete', 'error'):
yield f"event: done\ndata: {result['status']}\n\n"
return
yield ": keepalive\n\n"
return Response(generate(), mimetype='text/event-stream')
@app.route('/download-file/<session_id>')
def download_file(session_id):
"""Download the generated ZIP file and clean up immediately."""
with session_lock:
result = download_results.get(session_id)
if not result or result.get('status') != 'complete':
return "File not ready", 404
zip_path = result['zip_path']
filename = result['filename']
if not zip_path or not os.path.exists(zip_path):
# File was already cleaned up - drop the stale session entry
_purge_session(session_id)
return "File not found", 404
try:
response = send_file(zip_path, as_attachment=True, download_name=filename)
def cleanup():
time.sleep(2)
_purge_session(session_id)
print(f"🗑️ Sessão {session_id[:8]} ({filename}) removida após download")
threading.Thread(target=cleanup, daemon=True).start()
return response
except Exception as e:
print(f"❌ Erro ao enviar arquivo: {e}")
return "Error sending file", 500
if __name__ == '__main__':
app.run(debug=True, port=5001, threaded=True)
else:
# Production: Gunicorn entrypoint
pass