Django celery触发器manage.py cmd @periodic_task

时间:2019-01-17 00:39:00

标签: python django triggers celery manage.py

我想每隔X分钟从芹菜运行一次manage.py cmd作为一项定期任务,但是每次我尝试完成该操作时,如下所示,我得到以下错误:

  

[2019-01-17 01:36:00,006:INFO / MainProcess]收到的任务:删除   未使用的媒体文件[3dd2b93b-e32a-4736-8b24-028b9ad8da35]
  [2019-01-17 01:36:00,007:警告/ ForkPoolWorker-3]正在扫描   未使用的媒体文件[2019-01-17 01:36:00,008:警告/ ForkPoolWorker-3]   未知命令:“ cleanup_unused_media --noinput --remove-empty-dirs”   [2019-01-17 01:36:00,008:INFO / ForkPoolWorker-3]任务删除未使用的   媒体文件[3dd2b93b-e32a-4736-8b24-028b9ad8da35]成功   0.0008139749998008483s:无

tasks.py

from celery import Celery
from celery.schedules import crontab
from celery.task import periodic_task
from celery.utils.log import get_task_logger
import requests
from django.core import management


logger = get_task_logger(__name__)
app = Celery('tasks', broker='redis://127.0.0.1')

...
@periodic_task(run_every=(crontab(minute='*/90')), name="Delete unused media file(s)", ignore_result=True)
def delete_unused_media():
    try:
        print("Scanning for unused media files")
        management.call_command('cleanup_unused_media --noinput --remove-empty-dirs')
        return "success, old media files have been deleted"
    except Exception as e:
        print(e)

管理cmd来自以下项目: https://github.com/akolpakov/django-unused-media

我的管理层打来的电话完全是错的还是怎么回事?

预先感谢

更新(Celery Config):

settings.py

INSTALLED_APPS = [
...
'celery',
'django_unused_media',
...
# Celery Settings:
BROKER_URL = 'redis://127.0.0.1:6379'
CELERY_RESULT_BACKEND = 'redis://127.0.0.1:6379'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = 'UTC'
...
CACHES = {
    "default": {
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": [
            "redis://127.0.0.1:6379/0",
            #"redis://127.0.0.1:6379/1",
        ],
        "OPTIONS": {
            "CLIENT_CLASS": "django_redis.client.DefaultClient",
            "SOCKET_CONNECT_TIMEOUT": 15,  # in seconds
            "SOCKET_TIMEOUT": 15,  # in seconds
            "COMPRESSOR": "django_redis.compressors.zlib.ZlibCompressor",
            "CONNECTION_POOL_KWARGS": {"max_connections": 1000, "retry_on_timeout": True}
        }
    }
}

celery.py

from __future__ import absolute_import, unicode_literals
from celery import Celery
import os

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'MyProject.settings')

app = Celery('MyProject')

# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
#   should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings', namespace='CELERY')

# Load task modules from all registered Django app configs.
app.autodiscover_tasks()



@app.task(bind=True)
def debug_task(self):
    print('Request: {0!r}'.format(self.request))

_init __。py

from __future__ import absolute_import, unicode_literals

# This will make sure the app is always imported when
# Django starts so that shared_task will use this app.
from .celery import app as celery_app

__all__ = ('celery_app')

例如这项工作正常:

@periodic_task(run_every=(crontab(minute='*/1')), name="Get exchange rate(s)", ignore_result=True)
def get_exchange_rate():
    api_url = "https://api.coinmarketcap.com/v1/ticker/?limit=1"
    try:
        exchange_rate = requests.get(api_url).json()
        logger.info("BTC Exchange rate updated successfully.")
    except Exception as e:
        print(e)
        exchange_rate = dict()
    return exchange_rate

2 个答案:

答案 0 :(得分:0)

问题在于您致电call_command的方式。 call_command将命令名称作为第一个参数,然后是在位置传递的参数。您将全部传递为单个字符串。尝试将其更改为:

management.call_command('cleanup_unused_media', '--noinput', '--remove-empty-dirs')

答案 1 :(得分:0)

对于使用Django 1.7+的任何人,似乎仅仅导入设置模块是不够的。

我让它像这样

from celery import Celery
from celery.schedules import crontab
from celery.task import periodic_task
from celery.utils.log import get_task_logger
import requests, os, django
from django.core import management


logger = get_task_logger(__name__)
app = Celery('tasks', broker='redis://127.0.0.1')

@periodic_task(run_every=(crontab(minute='*/1')), name="Delete unused media file(s)", ignore_result=True)
def delete_unused_media():
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "MyProject.settings")
    django.setup()
    try:
        print("Scanning for unused media files")
        management.call_command('cleanup_unused_media', '--noinput', '--remove-empty-dirs')
        return "success, old media files have been deleted"
    except Exception as e:
        print(e)