简体   繁体   中英

Django celery trigger manage.py cmd @periodic_task

i want to run a manage.py cmd from celery as a periodic task every x Minutes but every time i try to accomplish that as show below i get the following error:

[2019-01-17 01:36:00,006: INFO/MainProcess] Received task: Delete unused media file(s)[3dd2b93b-e32a-4736-8b24-028b9ad8da35]
[2019-01-17 01:36:00,007: WARNING/ForkPoolWorker-3] Scanning for unused media files [2019-01-17 01:36:00,008: WARNING/ForkPoolWorker-3] Unknown command: 'cleanup_unused_media --noinput --remove-empty-dirs' [2019-01-17 01:36:00,008: INFO/ForkPoolWorker-3] Task Delete unused media file(s)[3dd2b93b-e32a-4736-8b24-028b9ad8da35] succeeded in 0.0008139749998008483s: None

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)

that management cmd comes from the following project: https://github.com/akolpakov/django-unused-media

is my management call simply wrong or whats the deal?

thanks in advance

UPDATE (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')

eg This task is working fine:

@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

The issue is the way you're calling call_command . call_command takes the name of the command as its first argument, followed by the arguments passed positionally. You're passing the whole lot as a single string. Try changing it to:

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

For anyone using Django 1.7+, it seems that simply import the settings module is not enough.

i got it working like this

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM