forked from 0x2620/pandora
limit cronjob rate if multiple cronjobs end up in queue
This commit is contained in:
parent
e1cacdb67a
commit
d7b53aa322
4 changed files with 29 additions and 9 deletions
|
@ -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
17
pandora/app/utils.py
Normal 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
|
|
@ -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()
|
||||||
|
|
||||||
|
|
|
@ -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()
|
||||||
|
|
Loading…
Reference in a new issue