Skip to content

Commit

Permalink
Invalidate cache, warn of quota excess
Browse files Browse the repository at this point in the history
  • Loading branch information
pierotofy committed Aug 26, 2023
1 parent a0dbd68 commit 872d5ab
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 6 deletions.
12 changes: 11 additions & 1 deletion app/models/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,18 @@ def used_quota_cached(self):
return cached

v = self.used_quota()
cache.set(k, v, 300) # 2 minutes
cache.set(k, v, 1800) # 30 minutes
return v

def has_exceeded_quota_cached(self):
if not self.has_quota():
return False

q = self.used_quota_cached()
return q > self.quota

def clear_used_quota_cache(self):
cache.delete(f'used_quota_{self.user.id}')

@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
Expand Down
5 changes: 5 additions & 0 deletions app/models/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,7 @@ def duplicate(self, set_new_name=True):
else:
logger.warning("Task {} doesn't have folder, will skip copying".format(self))

self.project.owner.profile.clear_used_quota_cache()
return task
except Exception as e:
logger.warning("Cannot duplicate task: {}".format(str(e)))
Expand Down Expand Up @@ -1037,6 +1038,8 @@ def delete(self, using=None, keep_parents=False):
except FileNotFoundError as e:
logger.warning(e)

self.project.owner.profile.clear_used_quota_cache()

plugin_signals.task_removed.send_robust(sender=self.__class__, task_id=task_id)

def set_failure(self, error_message):
Expand Down Expand Up @@ -1175,5 +1178,7 @@ def update_size(self, commit=False):
total_bytes += os.path.getsize(fp)
self.size = (total_bytes / 1024 / 1024)
if commit: self.save()

self.project.owner.profile.clear_used_quota_cache()
except Exception as e:
logger.warn("Cannot update size for task {}: {}".format(self, str(e)))
10 changes: 10 additions & 0 deletions app/templates/app/dashboard.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{% extends "app/logged_in_base.html" %}
{% load i18n %}
{% load settings %}

{% block content %}
{% load render_bundle from webpack_loader %}
Expand Down Expand Up @@ -39,6 +40,15 @@ <h3>{% trans 'Welcome!' %} ☺</h3>
</p>
{% endif %}

{% if user.profile.has_exceeded_quota_cached %}
{% with total=user.profile.quota|storage_size used=user.profile.used_quota_cached|storage_size %}
{% quota_exceeded_grace_period as hours %}
<div class="alert alert-warning alert-dismissible">
<i class="fas fa-exclamation-triangle"></i> {% blocktrans %}The current storage quota is being exceeded ({{ used }} of {{ total }} used). The most recent tasks will be automatically deleted within {{ hours }} hours, until usage falls below {{ total }}.{% endblocktrans %}
</div>
{% endwith %}
{% endif %}

<div id="dashboard-app" data-dashboard></div>

{% endif %}
Expand Down
5 changes: 2 additions & 3 deletions app/templates/app/logged_in_base.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
{% if user.profile.has_quota %}
<li class="divider"></li>

{% with tot_quota=user.profile.quota %}
{% with used_quota=user.profile.used_quota_cached %}
{% with tot_quota=user.profile.quota used_quota=user.profile.used_quota_cached %}
{% percentage used_quota tot_quota as perc_quota %}
{% percentage used_quota tot_quota 100 as bar_width %}

Expand All @@ -38,7 +37,7 @@
</div>
</li>

{% endwith %}{% endwith %}
{% endwith %}
{% endif %}
<li class="divider"></li>
<li><a href="/logout/"><i class="fa fa-sign-out-alt fa-fw"></i> {% trans 'Logout' %}</a>
Expand Down
4 changes: 4 additions & 0 deletions app/templatetags/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ def percentage(num, den, maximum=None):
perc = min(perc, maximum)
return perc

@register.simple_tag
def quota_exceeded_grace_period():
return settings.QUOTA_EXCEEDED_GRACE_PERIOD

@register.simple_tag
def is_single_user_mode():
return settings.SINGLE_USER_MODE
Expand Down
5 changes: 3 additions & 2 deletions app/views/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ def dashboard(request):
return redirect(settings.PROCESSING_NODES_ONBOARDING)

no_tasks = Task.objects.filter(project__owner=request.user).count() == 0

no_projects = Project.objects.filter(owner=request.user).count() == 0

# Create first project automatically
if Project.objects.count() == 0:
if no_projects and request.user.has_perm('app.add_project'):
Project.objects.create(owner=request.user, name=_("First Project"))

return render(request, 'app/dashboard.html', {'title': _('Dashboard'),
Expand Down
5 changes: 5 additions & 0 deletions webodm/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,11 @@ def scalebyiv(color, n):
EXTERNAL_AUTH_ENDPOINT = "http://192.168.2.253:5000/r/auth/login"
# TODO: make these env vars?

# Number of hours before tasks are automatically deleted
# from an account that is exceeding a disk quota
QUOTA_EXCEEDED_GRACE_PERIOD = 8


if TESTING or FLUSHING:
CELERY_TASK_ALWAYS_EAGER = True

Expand Down

0 comments on commit 872d5ab

Please sign in to comment.