Handle empty ttl in backend
This commit is contained in:
parent
a6faf8720d
commit
4e72bcfe07
|
@ -4,83 +4,85 @@ import uuid
|
||||||
from django.core.cache import cache
|
from django.core.cache import cache
|
||||||
from rest_framework.decorators import api_view
|
from rest_framework.decorators import api_view
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
|
from django.conf import settings
|
||||||
|
|
||||||
|
|
||||||
@api_view(['POST'])
|
@api_view(['GET', 'POST'])
|
||||||
def manage_items(request, *args, **kwargs):
|
def manage_items(request, *args, **kwargs):
|
||||||
# Define a timeout value (in seconds)
|
if request.method == 'POST':
|
||||||
timeout = 5
|
# Define a timeout value (in seconds)
|
||||||
|
timeout = 5
|
||||||
|
|
||||||
# Get the list of files and the TTL value from the request data
|
# Get the list of files and the TTL value from the request data
|
||||||
files = request.FILES.getlist('file')
|
files = request.FILES.getlist('file')
|
||||||
ttl = request.data.get('ttl')
|
ttl = request.data.get('ttl')
|
||||||
if not ttl:
|
if not ttl:
|
||||||
# Set ttl to 3 days
|
# Set ttl to 3 days
|
||||||
ttl = 259200 # 3 * 24 * 60 * 60
|
ttl = 259200 # 3 * 24 * 60 * 60
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Convert the TTL to an integer
|
# Convert the TTL to an integer
|
||||||
ttl = int(ttl)
|
ttl = int(ttl)
|
||||||
|
|
||||||
if ttl <= 0:
|
if ttl <= 0:
|
||||||
return Response({'msg': 'TTL must be a positive integer'}, status=400)
|
return Response({'msg': 'TTL must be a positive integer'}, status=400)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
return Response({'msg': 'Invalid TTL format'}, status=400)
|
return Response({'msg': 'Invalid TTL format'}, status=400)
|
||||||
|
|
||||||
# Define a function to save a single file in a thread
|
# Define a function to save a single file in a thread
|
||||||
def save_file_to_redis(file):
|
def save_file_to_redis(file):
|
||||||
key = uuid.uuid4().hex
|
key = uuid.uuid4().hex
|
||||||
|
|
||||||
# Get the filename
|
# Get the filename
|
||||||
filename = file.name
|
filename = file.name
|
||||||
|
|
||||||
# Convert file to bytes
|
# Convert file to bytes
|
||||||
file_content = file.read()
|
file_content = file.read()
|
||||||
|
|
||||||
# Set with the provided TTL
|
# Set with the provided TTL
|
||||||
cache.set(key, file_content, timeout=ttl)
|
cache.set(key, file_content, timeout=ttl)
|
||||||
|
|
||||||
response = {
|
response = {
|
||||||
'key': key,
|
'key': key,
|
||||||
'msg': f"{key} successfully set to {filename} with TTL {ttl} seconds"
|
'msg': f"{key} successfully set to {filename} with TTL {ttl} seconds"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Append the response to the shared responses list
|
# Append the response to the shared responses list
|
||||||
responses.append(response)
|
responses.append(response)
|
||||||
|
|
||||||
# Create a list to store the responses for each file
|
# Create a list to store the responses for each file
|
||||||
responses = []
|
responses = []
|
||||||
|
|
||||||
# Create a thread for each file
|
# Create a thread for each file
|
||||||
file_threads = []
|
file_threads = []
|
||||||
for file in files:
|
for file in files:
|
||||||
file_thread = threading.Thread(target=save_file_to_redis, args=(file,))
|
file_thread = threading.Thread(target=save_file_to_redis, args=(file,))
|
||||||
file_threads.append(file_thread)
|
file_threads.append(file_thread)
|
||||||
|
|
||||||
# Start all file-saving threads
|
# Start all file-saving threads
|
||||||
for file_thread in file_threads:
|
|
||||||
file_thread.start()
|
|
||||||
|
|
||||||
# Use a Timer to add a timeout
|
|
||||||
timeout_event = threading.Event()
|
|
||||||
timeout_timer = threading.Timer(timeout, lambda: timeout_event.set())
|
|
||||||
|
|
||||||
try:
|
|
||||||
# Start the timer
|
|
||||||
timeout_timer.start()
|
|
||||||
|
|
||||||
# Wait for all file-saving threads to complete
|
|
||||||
for file_thread in file_threads:
|
for file_thread in file_threads:
|
||||||
file_thread.join()
|
file_thread.start()
|
||||||
|
|
||||||
# Check if the threads completed without a timeout
|
# Use a Timer to add a timeout
|
||||||
if not timeout_event.is_set():
|
timeout_event = threading.Event()
|
||||||
return Response(responses, status=201)
|
timeout_timer = threading.Timer(timeout, lambda: timeout_event.set())
|
||||||
else:
|
|
||||||
return Response({'msg': 'File saving timed out'}, status=500)
|
try:
|
||||||
finally:
|
# Start the timer
|
||||||
# Always cancel the timer to prevent it from firing after the threads complete
|
timeout_timer.start()
|
||||||
timeout_timer.cancel()
|
|
||||||
|
# Wait for all file-saving threads to complete
|
||||||
|
for file_thread in file_threads:
|
||||||
|
file_thread.join()
|
||||||
|
|
||||||
|
# Check if the threads completed without a timeout
|
||||||
|
if not timeout_event.is_set():
|
||||||
|
return Response(responses, status=201)
|
||||||
|
else:
|
||||||
|
return Response({'msg': 'File saving timed out'}, status=500)
|
||||||
|
finally:
|
||||||
|
# Always cancel the timer to prevent it from firing after the threads complete
|
||||||
|
timeout_timer.cancel()
|
||||||
|
|
||||||
|
|
||||||
@api_view(['GET', 'PUT', 'DELETE'])
|
@api_view(['GET', 'PUT', 'DELETE'])
|
||||||
|
|
Loading…
Reference in New Issue