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,21 +5,24 @@ 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):
|
||||||
update_random_sort()
|
if limit_rate('item.tasks.cronjob', 8 * 60 * 60):
|
||||||
update_random_clip_sort()
|
update_random_sort()
|
||||||
|
update_random_clip_sort()
|
||||||
|
|
||||||
def update_random_sort():
|
def update_random_sort():
|
||||||
if filter(lambda f: f['id'] == 'random', settings.CONFIG['itemKeys']):
|
if filter(lambda f: f['id'] == 'random', settings.CONFIG['itemKeys']):
|
||||||
|
|
|
@ -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):
|
||||||
for c in models.Channel.objects.all():
|
if limit_rate('tv.tasks.update_program', 8 * 60 * 60):
|
||||||
c.update_program()
|
for c in models.Channel.objects.all():
|
||||||
|
c.update_program()
|
||||||
|
|
Loading…
Reference in a new issue