import random from huey.djhuey import task, periodic_task, crontab, db_task @task() def count_beans(number): print('-- counted %s beans --' % number) return 'Counted %s beans' % number @periodic_task(crontab(minute='*/5')) def every_five_mins(): print('Every five minutes this will be printed by the consumer') @task(retries=3, retry_delay=10) def try_thrice(): if random.randint(1, 3) == 1: print('OK') else: print('About to fail, will retry in 10 seconds') raise Exception('Crap something went wrong') @db_task() def foo(number): print('foo(%s)' % number)
from django.utils import timezone from huey.djhuey import db_periodic_task from huey.djhuey import crontab from huey.djhuey import db_task import pytz import requests import json import datetime from beerstatus import models from lxml import html from django.utils.timezone import utc import re LOGGER = logging.getLogger(__name__) @db_periodic_task(crontab(hour='*/6')) def refresh_beer_availability_statuses(): LOGGER.info("starting beer status updates") for beer in models.Beer.objects.all().filter(active=True): for city_id, city in models.SUPPORTED_ALKO_CITIES: refresh_beer_availability(beer.pk, city_id) LOGGER.info("finished scheduling beer status updates") def parse_alko_availability_date(value): #alko servers are always in helsinki local time so references like #yesterday etc. also follow that helsinki = pytz.timezone("Europe/Helsinki") date = datetime.datetime.utcnow().replace(tzinfo=utc).astimezone(helsinki) if value == "today": return date if value == "yesterday":
import time from huey.djhuey import crontab, db_periodic_task, db_task from .models import JobRequest @db_periodic_task(crontab(minute='*/15')) def complete_job_requests(): """This task gets autodiscovered by the huey task queue. Every fifteen minutes, checks any open job requests to see if they need moving over to being complete. """ count = 0 for job_request in JobRequest.objects.need_completing(): job_request.complete() job_request.save() count += 1 print('[%s] Automatically completed %d job requests.' % (time.ctime(), count))
# Tasks ####################################################################### @db_task() def provision_instance(instance_pk): """ Run provisioning on an existing instance """ logger.info('Retreiving instance: pk=%s', instance_pk) instance = OpenEdXInstance.objects.get(pk=instance_pk) logger.info('Running provisioning on %s', instance) instance.provision() @db_periodic_task(crontab(minute='*/1')) def watch_pr(): """ Automatically create/update sandboxes for PRs opened by members of the watched organization on the watched repository """ team_username_list = get_username_list_from_team( settings.WATCH_ORGANIZATION) for username in team_username_list: for pr in get_pr_list_from_username(username, settings.WATCH_FORK): sub_domain = 'pr{number}.sandbox'.format(number=pr.number) instance, created = OpenEdXInstance.objects.update_or_create_from_pr( pr, sub_domain) if created: logger.info('New PR found, creating sandbox: %s', pr)
from huey.djhuey import crontab, periodic_task from models import Task, MailSent from datetime import timedelta from django.utils import timezone from fcs.backend.mailing_helper import MailingHelper from django.conf import settings @periodic_task(crontab(minute='*/15')) def notify_about_crawler_data(): """ Task-function for Huey plugin. Sends notification about waiting data from crawler. Requires running Redis server. """ earlier_23_hours = timezone.now() - timedelta(hours=23) tasks = list(Task.objects.filter(active=True, last_data_download__lte=earlier_23_hours)) tasks = tasks + list(Task.objects.filter(active=True, last_data_download=None, created__lte=earlier_23_hours)) earlier_24_hours = timezone.now() - timedelta(hours=24) tasks_with_users_already_notified = [list(x.tasks.all()) for x in MailSent.objects.filter(date__gte=earlier_24_hours)] tasks_with_users_already_notified = [x for inner_list in tasks_with_users_already_notified for x in inner_list] tasks_with_users_already_notified = set(tasks_with_users_already_notified).intersection(tasks) for task in tasks_with_users_already_notified: tasks.remove(task) if len(tasks) > 0: tasks.sort(key=lambda x: x.user.id) grouped_list = [] usr = None lst = [] for task in tasks: if task.user != usr: usr = task.user grouped_list.append(lst)
from datetime import datetime import pytz import logging from huey.djhuey import crontab, db_periodic_task, db_task from django.conf import settings from django.contrib.auth import get_user_model from main.models import Host from stats.models import StatisticsEntry @db_periodic_task(crontab(hour='0', minute='0')) def save_user_count(): try: last_count = StatisticsEntry.objects.filter( stat_type='user_count').order_by('-created')[0].value except Exception as e: last_count = 0 logging.warn('Dropped exception {0} in task save_user_count'.format(e)) count = get_user_model().objects.all().count() if not count == last_count: StatisticsEntry.objects.create(stat_type='user_count', value=count) @db_periodic_task(crontab(hour='0', minute='0')) def save_host_count(): try: last_count = StatisticsEntry.objects.filter( stat_type='host_count').order_by('-created')[0].value except Exception as e:
import datetime from huey.djhuey import crontab, db_periodic_task, db_task from django.conf import settings from django.contrib.auth import get_user_model from main.models import Host from stats.models import StatisticsEntry @db_periodic_task(crontab(hour='0', minute='0')) def save_user_count(): count = get_user_model().objects.all().count() StatisticsEntry.objects.create(stat_type='user_count', value=count) @db_periodic_task(crontab(hour='0', minute='0')) def save_host_count(): count = Host.objects.all().count() StatisticsEntry.objects.create(stat_type='host_count', value=count) @db_task() def increment_ip_update_count(): today = datetime.date.today() (se, created) = StatisticsEntry.objects.get_or_create( stat_type='ip_update_count', created__year=(today.year), created__month=(today.month), created__day=(today.day), defaults={'value': 0} )
from huey.djhuey import crontab, db_periodic_task from crop.send_mail import send_reminding_mail @db_periodic_task(crontab(day='*/2')) def every_two_days(): # This is a periodic task that executes queries. print 'send another mail!' send_reminding_mail()
from django.utils import timezone from django.conf import settings from huey.djhuey import crontab from huey.djhuey import db_periodic_task from huey.djhuey import task from . import models from . import prelisten from . import creditor logger = logging.getLogger(__name__) @db_periodic_task(crontab(minute="59")) def update_exchange_rates(): """ Fetch new exchange rates from btcaverage. """ from tatianastore.models import get_rate_converter converter = get_rate_converter() converter.update() @task() def generate_prelisten(song_id): """ Generate prelisten clips for the song on background. """ # Stupid workaround to let the tranasaction of web process have time to commit # when adding song from Django admin time.sleep(5)
port = '6030' context = zmq.Context() push_socket = context.socket(zmq.PUSH) push_socket.connect("tcp://*****:*****@periodic_task(crontab(minute='*/10')) def scheduler(): lost_sch = False querysets = schedule() if not querysets: querysets = get_lost_schedule() lost_sch = True for item in querysets: #print datetime.date(item.date), datetime.time(item.time) cdatetime = getdatetime(datetime.date(item.date), datetime.time(item.time)) if any([item.frequency != " ", item.due_dates != '0']): load.schedule(args=(item.user.username, item.amount, item.type, item.phone_no), eta=cdatetime) try: inter, loop = item.frequency.split('-') # eg 2-month except ValueError:
with Timer() as timer: source_image.create_tiles() logger.info( "Took %s to create source image tiles for NormalizedSourceImage: %s", timer.elapsed, source_image_pk, ) # go ahead and trigger tile matching queue_source_image_tiles_for_matching() MATCH_BATCH_SIZE = excavator.env_int('MOSAIC_BATCH_SIZE', default=20) @periodic_task(crontab(minute='*')) def queue_source_image_tiles_for_matching(): if not SourceImageTile.objects.unmatched().exists(): return if SourceImageTile.objects.processing().exists(): return logger.info("Queueing tiles for matching") for _ in range(MATCH_BATCH_SIZE): match_souce_image_tiles() @db_task() def match_souce_image_tiles(tile_pk=None): if tile_pk: tile = SourceImageTile.objects.get(pk=tile_pk)
port = '6030' context = zmq.Context() push_socket = context.socket(zmq.PUSH) push_socket.connect("tcp://*****:*****@periodic_task(crontab(minute='*/10')) def scheduler(): lost_sch = False querysets = schedule() if not querysets: querysets = get_lost_schedule() lost_sch = True for item in querysets: #print datetime.date(item.date), datetime.time(item.time) cdatetime = getdatetime(datetime.date(item.date), datetime.time(item.time)) if any([item.frequency != " ", item.due_dates != '0']): load.schedule(args=(item.user.username, item.amount, item.type, item.phone_no), eta=cdatetime)
from django.utils import timezone from django.conf import settings from huey.djhuey import crontab from huey.djhuey import db_periodic_task from huey.djhuey import task from . import models from . import prelisten from . import creditor logger = logging.getLogger(__name__) @db_periodic_task(crontab(minute='59')) def update_exchange_rates(): """ Fetch new exchange rates from btcaverage. """ from tatianastore.models import get_rate_converter converter = get_rate_converter() converter.update() @task() def generate_prelisten(song_id): """ Generate prelisten clips for the song on background. """ # Stupid workaround to let the tranasaction of web process have time to commit # when adding song from Django admin time.sleep(5)