def test_deserialize_run_every_setting(): examples = [ ({ 'crontab': { 'minute': '*/30', 'hour': '0-5' } }, crontab(minute='*/30', hour='0-5')), ({ 'timedelta': { 'minutes': 10 } }, timedelta(minutes=10)), (30, 30), ] for input_value, expected_output_value in examples: assert_equal(deserialize_run_every_setting(input_value), expected_output_value) negative_examples = [ # unacceptable function ({ 'nontab': {'args'} }, ValueError), # multiple functions ({ 'crontab': { 'minute': '*/30', 'hour': '0-5' }, 'timedelta': { 'minutes': 10 } }, ValueError), # no function ({}, ValueError), # string ('30', ValueError), # bad params ({ 'crontab': { 'foo': 'bar' } }, TypeError), ] for input_value, exception_type in negative_examples: with assert_raises(exception_type): deserialize_run_every_setting(input_value)
def test_deserialize_run_every_setting(): examples = [ ({'crontab': {'minute': '*/30', 'hour': '0-5'}}, crontab(minute='*/30', hour='0-5')), ({'timedelta': {'minutes': 10}}, timedelta(minutes=10)), (30, 30), ] for input_value, expected_output_value in examples: assert_equal(deserialize_run_every_setting(input_value), expected_output_value) negative_examples = [ # unacceptable function ({'nontab': {'args'}}, ValueError), # multiple functions ({'crontab': {'minute': '*/30', 'hour': '0-5'}, 'timedelta': {'minutes': 10}}, ValueError), # no function ({}, ValueError), # string ('30', ValueError), # bad params ({'crontab': {'foo': 'bar'}}, TypeError), ] for input_value, exception_type in negative_examples: with assert_raises(exception_type): deserialize_run_every_setting(input_value)
def update_domain_date(user_id, domain): from corehq.apps.users.models import WebUser user = WebUser.get_by_user_id(user_id) domain_membership = user.get_domain_membership(domain, allow_mirroring=False) today = datetime.today().date() if domain_membership and ( not domain_membership.last_accessed or domain_membership.last_accessed < today): domain_membership.last_accessed = today try: user.save() except ResourceConflict: pass process_reporting_metadata_staging_schedule = deserialize_run_every_setting( settings.USER_REPORTING_METADATA_BATCH_SCHEDULE ) @periodic_task( run_every=process_reporting_metadata_staging_schedule, queue='background_queue', ) def process_reporting_metadata_staging(): from corehq.apps.users.models import ( CouchUser, UserReportingMetadataStaging ) start = datetime.utcnow() with transaction.atomic():
def update_domain_date(user_id, domain): from corehq.apps.users.models import WebUser user = WebUser.get_by_user_id(user_id, domain) domain_membership = user.get_domain_membership(domain) today = datetime.today().date() if domain_membership and (not domain_membership.last_accessed or domain_membership.last_accessed < today): domain_membership.last_accessed = today try: user.save() except ResourceConflict: pass @periodic_task( run_every=deserialize_run_every_setting( settings.USER_REPORTING_METADATA_BATCH_SCHEDULE), queue='background_queue', ) def process_reporting_metadata_staging(): from corehq.apps.users.models import (CouchUser, UserReportingMetadataStaging) with transaction.atomic(): records = (UserReportingMetadataStaging.objects.select_for_update( skip_locked=True).order_by('pk'))[:100] for record in records: user = CouchUser.get_by_user_id(record.user_id, record.domain) record.process_record(user) record.delete() if UserReportingMetadataStaging.objects.exists():
from __future__ import absolute_import from __future__ import unicode_literals from celery.task.base import periodic_task from corehq.preindex.accessors import index_design_doc, get_preindex_designs from corehq.util.celery_utils import deserialize_run_every_setting from corehq.util.decorators import serial_task from django.conf import settings couch_reindex_schedule = deserialize_run_every_setting(settings.COUCH_REINDEX_SCHEDULE) @periodic_task(serializer='pickle', run_every=couch_reindex_schedule, queue=settings.CELERY_PERIODIC_QUEUE) def run_continuous_indexing_task(): preindex_couch_views.delay() @serial_task('couch-continuous-indexing', timeout=60 * 60, queue=settings.CELERY_PERIODIC_QUEUE, max_retries=0) def preindex_couch_views(): for design in get_preindex_designs(): index_design_doc(design)
from __future__ import absolute_import from __future__ import unicode_literals from celery.task.base import periodic_task from corehq.preindex.accessors import index_design_doc, get_preindex_designs from corehq.util.celery_utils import deserialize_run_every_setting from corehq.util.decorators import serial_task from django.conf import settings couch_reindex_schedule = deserialize_run_every_setting( settings.COUCH_REINDEX_SCHEDULE) @periodic_task(serializer='pickle', run_every=couch_reindex_schedule, queue=settings.CELERY_PERIODIC_QUEUE) def run_continuous_indexing_task(): preindex_couch_views.delay() @serial_task('couch-continuous-indexing', timeout=60 * 60, queue=settings.CELERY_PERIODIC_QUEUE, max_retries=0) def preindex_couch_views(): for design in get_preindex_designs(): index_design_doc(design)