Guest User

Untitled

a guest
Mar 4th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.86 KB | None | 0 0
  1. Привет, закинул все в контейнеры.
  2. Celery, Redis, Django, Pstgr
  3. Пытаюсь юзать celery-beat.
  4. Таска задекларирована в settings.py и сервис celery в контейнере ее видит, но через определенное время она не вызывается.
  5.  
  6.  
  7. docker-compose.yml
  8.  
  9. version: '3'
  10. services:
  11.   db:
  12.     image: postgres
  13.     volumes:
  14.       - postgres_data:/var/lib/postgresql/data/
  15.   redis:
  16.     image: "redis:alpine"
  17.   web:
  18.     build: .
  19.     volumes:
  20.       - .:/code
  21.     ports:
  22.       - "8000:8000"
  23.     depends_on:
  24.       - db
  25.       - redis
  26.   celery:
  27.     build: .
  28.     command: celery -A test_dir worker -l info
  29.     volumes:
  30.       - .:/code
  31.     depends_on:
  32.       - db
  33.       - redis
  34.   celery-beat:
  35.     build: .
  36.     command: celery -A test_dir beat -l info
  37.     volumes:
  38.       - .:/code
  39.     depends_on:
  40.       - db
  41.       - redis
  42. volumes:
  43.   postgres_data:
  44.  
  45.  
  46.  
  47.  
  48. settings.py
  49. CELERY_BROKER_URL = 'redis://redis:6379'
  50. CELERY_RESULT_BACKEND = 'redis://redis:6379'
  51. CELERY_ACCEPT_CONTENT = ['application/json']
  52. CELERY_TASK_SERIALIZER = 'json'
  53. CELERY_RESULT_SERIALIZER = 'json'
  54.  
  55. CELERY_BEAT_SCHEDULE = {
  56.     'hello': {
  57.         'task': 'API.tasks.hello',
  58.         'schedule': crontab()
  59.     }
  60. }
  61.  
  62.  
  63. __init__.py
  64. from .celery import app as celery_app
  65.  
  66. __all__ = ['celery_app']
  67.  
  68. celery.py рядом c settings.py
  69.  
  70. import os
  71. from celery import Celery
  72.  
  73.  
  74. os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'test_dir.settings')
  75.  
  76. app = Celery('test_dir')
  77. app.config_from_object('django.conf:settings', namespace='CELERY')
  78. app.autodiscover_tasks()
  79.  
  80. @app.task(bind=True)
  81. def debug_task(self):
  82.     print('Request: {0!r}'.format(self.request))
  83.  
  84.  
  85. tasks.py в приложении API
  86. from celery import shared_task
  87.  
  88.  
  89. @shared_task
  90. def hello():
  91.     print('Hello there!')
Advertisement
Add Comment
Please, Sign In to add comment