def synchronize_all_feeds(): """Synchronize feeds every 30 minutes. To avoid a spike of load, the synchronization is spread over the whole period. Feeds that have their sync explicitly disabled or that have no active subscribers are not synchronized. """ current_date = now() inactive_user_threshold = current_date - ( timedelta(seconds=settings.SESSION_COOKIE_AGE) * 2) feeds_to_sync = models.Feed.objects.filter( is_sync_enabled=True, subscribers__user__is_active=True, subscribers__user__last_login__gte=inactive_user_threshold) ats = list() for i in range(0, 29): ats.append(current_date + timedelta(minutes=i)) batch = Batch() for feed_id in feeds_to_sync.values_list('id', flat=True): batch.schedule_at('synchronize_feed', random.choice(ats), feed_id) tasks.schedule_batch(batch)
def synchronize_all_feeds(): """Synchronize all feeds every 30 minutes. To avoid a spike of load, the synchronization is spread over the whole period. """ current_date = now() ats = list() for i in range(0, 29): ats.append(current_date + timedelta(minutes=i)) batch = Batch() for feed_id in models.Feed.objects.values_list('id', flat=True): batch.schedule_at('synchronize_feed', random.choice(ats), feed_id) tasks.schedule_batch(batch)
def test_schedule_batch(patch_now): now = get_now() tasks = Tasks() tasks.add(print, 'foo_task') tasks.add(print, 'bar_task') broker = Mock() s = Engine(broker, namespace='tests') s.attach_tasks(tasks) batch = Batch() batch.schedule('foo_task', 1, 2) batch.schedule_at('bar_task', now, three=True) s.schedule_batch(batch) broker.enqueue_jobs.assert_called_once_with([ANY, ANY]) foo_job = broker.enqueue_jobs.call_args[0][0][0] assert foo_job.task_name == 'foo_task' assert foo_job.at == now assert foo_job.task_args == (1, 2) assert foo_job.task_kwargs == {} bar_job = broker.enqueue_jobs.call_args[0][0][1] assert bar_job.task_name == 'bar_task' assert bar_job.at == now assert bar_job.task_args == () assert bar_job.task_kwargs == {'three': True}
def sync(self, request, queryset): batch = Batch() for feed in queryset: batch.schedule('synchronize_feed', feed.id, force=True) tasks.schedule_batch(batch)
def form_valid(self, form): batch = Batch() for uri in form.cleaned_data['opml_uris']: batch.schedule('create_feed', self.request.user.id, uri) tasks.tasks.schedule_batch(batch) return super().form_valid(form)