Skip to content

Instantly share code, notes, and snippets.

@felipe3dfx
Created May 22, 2017 14:16
Show Gist options
  • Save felipe3dfx/dbedd934d11de12aabc240eda22b6750 to your computer and use it in GitHub Desktop.
Save felipe3dfx/dbedd934d11de12aabc240eda22b6750 to your computer and use it in GitHub Desktop.
Huey
# settings.py
'huey.contrib.djhuey',
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'level-and-date': {
'format':
'%(levelname)s\t%(asctime)s\t%(funcName)s\t%(message)s'
},
},
'handlers': {
'console': {
'class': 'logging.StreamHandler',
'formatter': 'level-and-date',
},
},
'loggers': {
'huey': {
'handlers': ['console'],
'level': 'INFO',
'propagate': False,
},
},
}
HUEY = {
'connection': {'host': REDIS_HOST, 'port': REDIS_PORT, 'db': 1},
'consumer': {'workers': 2, 'worker_type': 'thread', 'utc': False},
'always_eager': False,
}
import logging
from huey import crontab
from huey.contrib.djhuey import db_task
from huey.contrib.djhuey import db_periodic_task
from huey.contrib.djhuey import task
from user.models import User
LOGGER = logging.getLogger('huey.consumer')
@task(retries=3, retry_delay=60 * 5, retries_as_argument=True)
def retries(retries):
LOGGER.info('Retries counter: %s', retries)
raise Exception
@db_task()
def daily_report():
User.objects.all().update(is_staff=True)
LOGGER.info('Users are updated')
@db_periodic_task(crontab(hour='6', minute='0'))
def scheduled_report():
count = User.objects.all().count()
LOGGER.info('Users are updated: %s', count)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment