Python 某些芹菜任务启动但挂起且从不执行

Python 某些芹菜任务启动但挂起且从不执行,python,django,asynchronous,celery,Python,Django,Asynchronous,Celery,我对Django和芹菜有一个问题,其中一些注册的任务从未执行过 我的tasks.py文件中有三个任务,其中两个计划通知()和计划存档()无问题工作。它们在预定义的ETA下无问题执行 使用schedule\u monitoring()函数,我可以看到作业是在芹菜花中启动的,但它从未实际执行过。它就在那里 我已经确认我可以从工作人员本地运行该命令,因此我不确定问题可能在哪里 tasks.py(功能失败) 芹菜配置 @task def schedule_monitoring(job_id: str,

我对Django和芹菜有一个问题,其中一些注册的任务从未执行过

我的tasks.py文件中有三个任务,其中两个<代码>计划通知()和
计划存档()
无问题工作。它们在预定义的ETA下无问题执行

使用
schedule\u monitoring()
函数,我可以看到作业是在芹菜花中启动的,但它从未实际执行过。它就在那里

我已经确认我可以从工作人员本地运行该命令,因此我不确定问题可能在哪里

tasks.py(功能失败)

芹菜配置

@task
def schedule_monitoring(job_id: str, action: str) -> str:
    salt = OSApi() # This is a wrapper around a REST API.
    job = Job.objects.get(pk=job_id)
    target = ('compound', f"G@hostname:{ job.network.gateway.host_name } and G@serial:{ job.network.gateway.serial_number }")

    policies = [
        'foo',
        'bar',
        'foobar',
        'barfoo'
    ]

    if action == 'start':
        salt.run(target, 'spectrum.add_to_collection', fun_args=['foo'])  
        for policy in policies:
            salt.run(target, 'spectrum.refresh_policy', fun_args=[policy])

        create_activity("Informational", "MONITORING", "Started proactive monitoring for job.", job)
    elif action == 'stop':
        salt.run(target, 'spectrum.remove_from_collection', fun_args=['bar'])
        for policy in policies:
            salt.run(target, 'spectrum.refresh_policy', fun_args=[policy])

        create_activity("Informational", "MONITORING", "Stopped proactive monitoring for job.", job)
    else:
        raise NotImplementedError

    return f"Applying monitoring action: {action.upper()} to Job: {job.job_code}"
# Async
CELERY_BROKER_URL = os.environ.get('BROKER_URL', 'redis://localhost:6379')
CELERY_RESULT_BACKEND = os.environ.get('RESULT_BACKEND', 'redis://localhost:6379')
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = 'UTC'
CELERY_ENABLE_UTC = True
以下是在本应执行该命令的工作进程上成功执行该命令的情况:

>>> schedule_monitoring(job.pk, 'start')
'Applying monitoring action: START to Job: Test 1'
>>> schedule_monitoring(job.pk, 'stop')
'Applying monitoring action: STOP to Job: Test 1'
>>> exit()
Waiting up to 5 seconds.
Sent all pending logs.
root@9d045ff7dfc1:/app#
从调试工人;当工作开始时,我所看到的只是以下内容,但没有什么有趣的东西

[2021-01-06 17:08:00,001: DEBUG/MainProcess] TaskPool: Apply <function _trace_task_ret at 0x7f6adbc29680> (args:('Operations.tasks.schedule_monitoring', '407e8a87-b3bf-4e8f-8a17-776a33ae5fea', {'lang': 'py', 'task': 'Operations.tasks.schedule_monitoring', 'id': '407e8a87-b3bf-4e8f-8a17-776a33ae5fea', 'shadow': None, 'eta': '2021-01-06T17:08:00+00:00', 'expires': None, 'group': None, 'group_index': None, 'retries': 0, 'timelimit': [None, None], 'root_id': '407e8a87-b3bf-4e8f-8a17-776a33ae5fea', 'parent_id': None, 'argsrepr': "(UUID('11118a85-20f2-488d-9a12-b8d200ea7a74'), 'start')", 'kwargsrepr': '{}', 'origin': 'gen442@31a9de56d061', 'reply_to': '24a8dc4c-2e5c-32ce-aa3d-84392d7cbf41', 'correlation_id': '407e8a87-b3bf-4e8f-8a17-776a33ae5fea', 'hostname': 'celery@bc4bb7af894f', 'delivery_info': {'exchange': '', 'routing_key': 'celery', 'priority': 0, 'redelivered': None}, 'args': ['11118a85-20f2-488d-9a12-b8d200ea7a74', 'start'], 'kwargs': {}}, b'[["11118a85-20f2-488d-9a12-b8d200ea7a74", "start"], {}, {"callbacks": null, "errbacks": null, "chain": null, "chord": null}]', 'application/json', 'utf-8') kwargs:{})
[2021-01-06 17:08:00,303: DEBUG/MainProcess] basic.qos: prefetch_count->32
[2021-01-06 17:08:00,305: DEBUG/MainProcess] Task accepted: Operations.tasks.schedule_monitoring[407e8a87-b3bf-4e8f-8a17-776a33ae5fea] pid:44
[2021-01-06 17:08:00,311: DEBUG/ForkPoolWorker-3] Resetting dropped connection: storage.googleapis.com
[2021-01-06 17:08:00,383: DEBUG/ForkPoolWorker-3] https://storage.googleapis.com:443 "GET /download/storage/v1/b/foo/o/bar?alt=media HTTP/1.1" 200 96
[2021-01-06 17:08:01,228: DEBUG/MainProcess] pidbox received method enable_events() [reply_to:None ticket:None]
[2021-01-06 17:08:06,228: DEBUG/MainProcess] pidbox received method enable_events() [reply_to:None ticket:None]
[2021-01-06 17:08:11,227: DEBUG/MainProcess] pidbox received method enable_events() [reply_to:None ticket:None]
[2021-01-06 17:08:16,228: DEBUG/MainProcess] pidbox received method enable_events() [reply_to:None ticket:None]
[2021-01-06 17:08:21,227: DEBUG/MainProcess] pidbox received method enable_events() [reply_to:None ticket:None]
[2021-01-06 17:08:26,229: DEBUG/MainProcess] pidbox received method enable_events() [reply_to:None ticket:None]
[2021-01-06 17:08:31,231: DEBUG/MainProcess] pidbox received method enable_events() [reply_to:None ticket:None]
[2021-01-06 17:08:00001:DEBUG/MainProcess]任务池:应用(参数:('Operations.tasks.schedule_monitoring','407e8a87-b3bf-4e8f-8a17-776a33ae5fea',{'lang':'py','task':'Operations.tasks.schedule_monitoring','id':'407e8a87-b3bf-4e8f-8a17-776a33ae5fea','shadow':None','eta':'2021-01-06T17:08:00+00:00','expires':None','group':None','group_index':None','retries':0','timelimit':'None','None','root_id':'407e8a87-b3bf-4e8f-8F-8a17-776a33ae5fea','('11118a85-20f2-488d-9a12-b8d200ea7a74'),'start'),'kwargsrepr':'{}','origin':'gen442@31a9de56d061“,”回复“:”24a8dc4c-2e5c-32ce-aa3d-84392d7cbf41“,”相关性id“:”407e8a87-b3bf-4e8f-8a17-776a33ae5fea“,”主机名“:”celery@bc4bb7af894f“,'delivery_info':{'exchange':'','routing_key':'芹菜','priority':0,'redelivered':无}”,'args':['11118a85-20f2-488d-9a12-b8d200ea7a74','start'],'kwargs':{},b'['11118a85-20f2-488d-9a12-b8d200ea7a74',{},{},{“回调”:null,“errbacks”:null,“chain”:null,“chord”:null}],'application/json”,“utf-8')kwargs:{})
[2021-01-06 17:08:00303:DEBUG/MainProcess]basic.qos:prefetch\u count->32
[2021-01-06 17:08:00305:调试/主流程]已接受任务:操作。任务。计划监视[407e8a87-b3bf-4e8f-8a17-776a33ae5fea]pid:44
[2021-01-06 17:08:00311:DEBUG/ForkPoolWorker-3]重置断开的连接:storage.googleapis.com
[2021-01-06 17:08:00383:DEBUG/ForkPoolWorker-3]https://storage.googleapis.com:443 “GET/download/storage/v1/b/foo/o/bar?alt=media HTTP/1.1”200 96
[2021-01-06 17:08:01228:DEBUG/MainProcess]pidbox接收到方法enable_events()[回复:无票证:无]
[2021-01-06 17:08:06228:DEBUG/MainProcess]pidbox接收到方法enable_events()[回复:无票证:无]
[2021-01-06 17:08:11227:DEBUG/MainProcess]pidbox接收到方法enable_events()[回复:无票证:无]
[2021-01-06 17:08:16228:DEBUG/MainProcess]pidbox接收到方法enable_events()[回复:无票证:无]
[2021-01-06 17:08:21227:DEBUG/MainProcess]pidbox接收到方法enable_events()[回复:无票证:无]
[2021-01-06 17:08:26229:DEBUG/MainProcess]pidbox接收到方法enable_events()[reply_to:None票证:None]
[2021-01-06 17:08:31231:DEBUG/MainProcess]pidbox接收到方法enable_events()[回复:无票证:无]

我找到的解决方案是在芹菜中创建两个队列,一个队列通过芹菜节拍管理预定任务,另一个队列的优先级更高

在我创建了单独的队列之后,任务开始流动并正确完成;我猜是一辆拥挤的公共汽车或工人

要创建其他队列,请在设置.py中执行以下操作:

from kombu import Queue, Exchange

CELERYD_MAX_TASKS_PER_CHILD = 4

CELERY_DEFAULT_QUEUE = 'scheduled'
CELERY_QUEUES = (
    Queue('scheduled', Exchange('scheduled'), routing_key='sched'),
    Queue('proactive_monitoring', Exchange('proactive_monitoring'), routing_key='prmon'),
)
然后,在注册任务函数时,传递要将其分配到的队列:

任务。py:

@task(queue='proactive_monitoring')
def schedule_monitoring(job_id: str, action: str) -> str:
最后,确保在每个队列中至少启动一个工作进程。启动工作进程时,通过传递队列来完成此操作:

芹菜-项目工人-l信息-Q主动监测

如果要在localhost上启动多个Worker,则应通过指定name属性至少区分每个队列中的前两个Worker:


芹菜-项目工作人员-l信息-Q主动式监控-n prmon\u first\u worker

我发现解决方案是在芹菜中创建两个队列,一个队列通过芹菜节拍管理计划任务,另一个队列对其余任务具有更高的优先级

在我创建了单独的队列之后,任务开始流动并正确完成;我猜是一辆拥挤的公共汽车或工人

要创建其他队列,请在设置.py中执行以下操作:

from kombu import Queue, Exchange

CELERYD_MAX_TASKS_PER_CHILD = 4

CELERY_DEFAULT_QUEUE = 'scheduled'
CELERY_QUEUES = (
    Queue('scheduled', Exchange('scheduled'), routing_key='sched'),
    Queue('proactive_monitoring', Exchange('proactive_monitoring'), routing_key='prmon'),
)
然后,在注册任务函数时,传递要将其分配到的队列:

任务。py:

@task(queue='proactive_monitoring')
def schedule_monitoring(job_id: str, action: str) -> str:
最后,确保在每个队列中至少启动一个工作进程。启动工作进程时,通过传递队列来完成此操作:

芹菜-项目工人-l信息-Q主动监测

如果要在localhost上启动多个Worker,则应通过指定name属性至少区分每个队列中的前两个Worker:

芹菜-项目工作人员-l信息-Q主动监测-n项目第一工作人员