2

我有一个名为“tickets”的应用程序,它在设置文件中,可以正确导入。

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

我正在为后台任务运行 Huey,它确实在其他两个应用程序中运行所有任务,而不是在应用程序“票证”中。这是应用票证中的模块“助手”:

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()
    ...

这是我的 Huey 配置:

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.
    },
}

这是 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
4

1 回答 1

3

当消费者运行时,它需要导入定义任务的所有模块,否则它们不会被拾取。管理命令将使用.run_huey自动发现任何模块名称。tasks.pyautodiscover_modules("tasks")

为了让您的定期任务能够被执行,您要么需要在其中定义它们,要么在定义它们tasks.pytasks.py地方导入模块。

于 2020-04-02T23:25:06.060 回答