mirror of
https://github.com/Rushilwiz/spaceout.git
synced 2025-04-08 14:00:16 -04:00
32 lines
1.0 KiB
Python
32 lines
1.0 KiB
Python
from django.http import HttpResponse
|
|
from django.views.generic import View
|
|
from django.db import Error, OperationalError
|
|
from psycopg2 import errorcodes
|
|
from functools import wraps
|
|
import time
|
|
|
|
|
|
# Create your views here.
|
|
def retry_on_exception(view, num_retries=3, on_failure=HttpResponse(status=500), delay_=0.5, backoff_=1.5):
|
|
@wraps(view)
|
|
def retry(*args, **kwargs):
|
|
delay = delay_
|
|
for i in range(num_retries):
|
|
try:
|
|
return view(*args, **kwargs)
|
|
except OperationalError as ex:
|
|
if i == num_retries - 1:
|
|
return on_failure
|
|
elif getattr(ex.__cause__, 'pgcode', '') == errorcodes.SERIALIZATION_FAILURE:
|
|
time.sleep(delay)
|
|
delay *= backoff_
|
|
else:
|
|
return on_failure
|
|
except Error as ex:
|
|
return on_failure
|
|
return retry
|
|
|
|
|
|
class PingView(View):
|
|
def get(self, request, *args, **kwargs):
|
|
return HttpResponse("python/django", status=200) |