Search code examples
djangopython-3.xpython-huey

Huey; Doesn't run tasks in one Django App


i have an app called "tickets", it's in the settings file, can be imported correctly.

INSTALLED_APPS = [
... 
"huey.contrib.djhuey",
"core",
"telefon",
"termine",
"tickets",
...
]

I am running Huey for background tasks and it does run all tasks in two other apps, just not in the app "tickets". Here is the module "helpers" in the app tickets:

from huey import crontab
from huey.contrib.djhuey import db_periodic_task, periodic_task

@periodic_task(crontab(minute="*/1"))
def checkForRunningHuey():
    logger.debug("Huey did run at {pendulum.now()}")


@db_periodic_task(crontab(minute="*/5"))
def getKissTickets():
    site_settings = Setting.load()
    if not site_settings.last_update_tickets:
        soy, now = getThisYear
        site_settings.last_update_tickets = soy
        site_settings.save()
    site_settings = Setting.load()
    ...

And here is my Huey Configuration:

HUEY = {
    "huey_class": "huey.RedisHuey",  # Huey implementation to use.
    "name": "Huey",  # Use db name for huey.
    "results": False,  # Store return values of tasks.
    "store_none": False,  # If a task returns None, do not save to results.
    "immediate": False,  # If DEBUG=True, run synchronously.
    "utc": True,  # Use UTC for all times internally.
    "blocking": True,  # Perform blocking pop rather than poll Redis.
    "connection": {
        "host": "192.168.x.xxx",
        "port": 6379,
        "db": 0,
        "connection_pool": None,  # Definitely you should use pooling!
        "read_timeout": 1,  # If not polling (blocking pop), use timeout.
        "url": None,  # Allow Redis config via a DSN.
    },
}

And here's the output of manage.py run_huey:

$ python manage.py run_huey --huey-verbose
[2020-03-18 14:17:08,249] INFO:huey.consumer:MainThread:Huey consumer started with 1 thread, PID 3100 at 2020-03-18 13:17:08.249881
[2020-03-18 14:17:08,249] INFO:huey.consumer:MainThread:Scheduler runs every 1 second(s).
[2020-03-18 14:17:08,251] INFO:huey.consumer:MainThread:Periodic tasks are enabled.
[2020-03-18 14:17:08,252] INFO:huey.consumer:MainThread:The following commands are available:
[2020-03-18 14:17:08,368] DEBUG:huey.consumer.Scheduler:Scheduler:Checking periodic tasks
[2020-03-18 14:17:08,368] DEBUG:huey.consumer.Scheduler:Scheduler:Sleeping for 0.8815112113952637
[2020-03-18 14:17:09,288] DEBUG:huey.consumer.Scheduler:Scheduler:Sleeping for 0.9611930847167969
[2020-03-18 14:17:10,316] DEBUG:huey.consumer.Scheduler:Scheduler:Sleeping for 0.9331824779510498
[2020-03-18 14:17:11,296] DEBUG:huey.consumer.Scheduler:Scheduler:Sleeping for 0.9533252716064453
[2020-03-18 14:17:12,330] DEBUG:huey.consumer.Scheduler:Scheduler:Sleeping for 0.9188826084136963
[2020-03-18 14:17:13,311] DEBUG:huey.consumer.Scheduler:Scheduler:Sleeping for 0.9387216567993164

Solution

  • When the consumer is run, it needs to have all the modules where tasks are defined imported or else they are not picked up. The run_huey management command will auto discover any modules names tasks.py using autodiscover_modules("tasks").

    In order for your periodic tasks to be picked up, you either need to define them in tasks.py or let tasks.py import the module where they are defined.