use cache locks

This commit is contained in:
bakatrouble 2019-11-24 17:07:19 +03:00
parent 7257ea3327
commit 71d7ea7147
4 changed files with 32 additions and 9 deletions

View File

@ -7,7 +7,7 @@ from feeds.modules import EchoFeedModuleConfig, DankMemesFeedModuleConfig, Shitt
@admin.register(Feed)
class FeedAdmin(admin.ModelAdmin):
list_display = '__str__', 'lock',
list_display = '__str__', 'locked',
admin.site.register(EchoFeedModuleConfig)

View File

@ -0,0 +1,17 @@
# Generated by Django 2.1.5 on 2019-11-24 14:07
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('feeds', '0006_tapasfeedmoduleconfig'),
]
operations = [
migrations.RemoveField(
model_name='feed',
name='lock',
),
]

View File

@ -1,6 +1,7 @@
from django.conf import settings
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
from django.contrib.contenttypes.models import ContentType
from django.core.cache import cache
from django.db import models
from django.utils import timezone
from telebot import TeleBot
@ -16,20 +17,26 @@ class Feed(models.Model):
check_interval = models.DurationField(help_text='in seconds')
last_check = models.DateTimeField(null=True, blank=True)
last_id = YAMLField(null=True, blank=True)
lock = models.BooleanField(default=False)
config_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
config_id = models.PositiveIntegerField()
config = GenericForeignKey('config_type', 'config_id')
@property
def lock_key(self):
return 'feed-{}-lock'.format(self.pk)
@property
def locked(self):
return bool(cache.get(self.lock_key))
def run_check(self):
if self.lock:
if self.locked:
return False
if self.last_check and timezone.now() < self.last_check + self.check_interval:
return False
self.lock = True
self.save()
cache.set(self.lock_key, 1, timeout=60)
execute_feed.apply_async(args=(self.pk,), shadow=str(self))
return True

View File

@ -1,9 +1,9 @@
import traceback
import sentry_sdk
from celery.exceptions import Reject
from celery_once import QueueOnce
from django.utils import timezone
from django.core.cache import cache
from telebot import TeleBot
from djconfig import config
@ -14,7 +14,7 @@ from config.celery import app
def check_feeds():
from feeds.models import Feed
feeds = Feed.objects.filter(lock=False)
feeds = Feed.objects.all()
enqueued = []
for feed in feeds:
if feed.run_check():
@ -47,5 +47,4 @@ def execute_feed(feed_pk):
traceback.print_exc()
finally:
if feed:
feed.lock = False
feed.save()
cache.delete(feed.lock_key)