limit cronjob rate if multiple cronjobs end up in queue

This commit is contained in:
j 2016-08-18 13:41:24 +02:00
parent e1cacdb67a
commit d7b53aa322
4 changed files with 29 additions and 9 deletions

View file

@ -42,6 +42,3 @@ class Settings(models.Model):
else: else:
value = default value = default
return value return value

17
pandora/app/utils.py Normal file
View file

@ -0,0 +1,17 @@
# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
from __future__ import division, with_statement
import time
from .models import Settings
def limit_rate(key, timeout):
key = 'limit.%s.last' % key
last_cronjob = Settings.get(key, 0)
current_time = time.time()
if (current_time - last_cronjob) > timeout:
Settings.set(key, current_time)
return True
return False

View file

@ -5,19 +5,22 @@ from datetime import timedelta, datetime
import gzip import gzip
import random import random
from celery.task import task, periodic_task
from django.conf import settings from django.conf import settings
from django.db import connection, transaction from django.db import connection, transaction
from django.db.models import Q from django.db.models import Q
from ox.utils import ET from ox.utils import ET
from celery.task import task, periodic_task
import models from app.utils import limit_rate
from text.models import Text from text.models import Text
from taskqueue.models import Task from taskqueue.models import Task
import models
@periodic_task(run_every=timedelta(days=1), queue='encoding') @periodic_task(run_every=timedelta(days=1), queue='encoding')
def cronjob(**kwargs): def cronjob(**kwargs):
if limit_rate('item.tasks.cronjob', 8 * 60 * 60):
update_random_sort() update_random_sort()
update_random_clip_sort() update_random_clip_sort()

View file

@ -4,10 +4,13 @@ from datetime import timedelta
from celery.task import periodic_task from celery.task import periodic_task
from app.utils import limit_rate
import models import models
@periodic_task(run_every=timedelta(days=1), queue='encoding') @periodic_task(run_every=timedelta(days=1), queue='encoding')
def update_program(**kwargs): def update_program(**kwargs):
if limit_rate('tv.tasks.update_program', 8 * 60 * 60):
for c in models.Channel.objects.all(): for c in models.Channel.objects.all():
c.update_program() c.update_program()