· 5 years ago · May 31, 2020, 09:32 AM
1-------celery.py
2
3# from __future__ import absolute_import
4import os
5from celery import Celery
6from celery.schedules import crontab
7from django.conf import settings
8
9os.environ['DJANGO_SETTINGS_MODULE'] = "testbot.settings"
10
11app = Celery('testbot')
12app.conf = settings
13app.autodiscover_tasks()
14
15# celery beat task
16
17app.conf.beat = {
18 'send-daily-crypto': {
19 'task': 'bot.tasks.send_daily_cryptocurrency',
20 'schedule': crontab('*/3')
21 }
22}
23---------settings.py
24
25"""
26Django settings for testbot project.
27
28Generated by 'django-admin startproject' using Django 3.0.3.
29
30For more information on this file, see
31https://docs.djangoproject.com/en/3.0/topics/settings/
32
33For the full list of settings and their values, see
34https://docs.djangoproject.com/en/3.0/ref/settings/
35"""
36
37import os
38import redis
39import urllib.parse as urlparse
40
41# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
42BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
43
44# Quick-start development settings - unsuitable for production
45# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/
46
47# SECURITY WARNING: keep the secret key used in production secret!
48
49SECRET_KEY = 'secret'
50
51# SECURITY WARNING: don't run with debug turned on in production!
52DEBUG = True
53
54ALLOWED_HOSTS = ['*']
55
56# Application definition
57
58INSTALLED_APPS = [
59 'django.contrib.admin',
60 'django.contrib.auth',
61 'django.contrib.contenttypes',
62 'django.contrib.sessions',
63 'django.contrib.messages',
64 'django.contrib.staticfiles',
65 'bot',
66]
67MIDDLEWARE = [
68 'django.middleware.security.SecurityMiddleware',
69 'django.contrib.sessions.middleware.SessionMiddleware',
70 'django.middleware.common.CommonMiddleware',
71 'django.middleware.csrf.CsrfViewMiddleware',
72 'django.contrib.auth.middleware.AuthenticationMiddleware',
73 'django.contrib.messages.middleware.MessageMiddleware',
74 'django.middleware.clickjacking.XFrameOptionsMiddleware',
75]
76
77ROOT_URLCONF = 'testbot.urls'
78
79TEMPLATES = [
80 {
81 'BACKEND': 'django.template.backends.django.DjangoTemplates',
82 'DIRS': [],
83 'APP_DIRS': True,
84 'OPTIONS': {
85 'context_processors': [
86 'django.template.context_processors.debug',
87 'django.template.context_processors.request',
88 'django.contrib.auth.context_processors.auth',
89 'django.contrib.messages.context_processors.messages',
90 ],
91 },
92 },
93]
94
95WSGI_APPLICATION = 'testbot.wsgi.application'
96
97# Database
98# https://docs.djangoproject.com/en/3.0/ref/settings/#databases
99
100DATABASES = {
101 'default': {
102 'ENGINE': 'django.db.backends.postgresql_psycopg2',
103 'NAME': 'd8mcfajta6ksr',
104 'USER': 'bbutkkljbayvxw',
105 'PASSWORD': '78352d9a393074248f043e4538c7867e6a860f3957265f2c1836058fb9ce4022',
106 'HOST': 'ec2-54-81-37-115.compute-1.amazonaws.com',
107 'PORT': '5432'
108 }
109}
110
111# Password validation
112# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators
113
114AUTH_PASSWORD_VALIDATORS = [
115 {
116 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
117 },
118 {
119 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
120 },
121 {
122 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
123 },
124 {
125 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
126 },
127]
128
129# Internationalization
130# https://docs.djangoproject.com/en/3.0/topics/i18n/
131
132LANGUAGE_CODE = 'en-us'
133
134TIME_ZONE = 'UTC'
135
136USE_I18N = True
137
138USE_L10N = True
139
140USE_TZ = True
141
142# AUTH_USER_MODEL = 'core.User'
143# Static files (CSS, JavaScript, Images)
144# https://docs.djangoproject.com/en/3.0/howto/static-files/
145
146STATIC_URL = '/static/'
147
148
149# redis settings for celery
150REDIS_URL = 'redis://h:p5067e3205757872a84ea31d841e6cf3ce88f7fcb568d463ff4dc1708d8f8c792@ec2-3-220-244-30.compute-1.amazonaws.com:14059'
151redis_url = urlparse.urlparse(os.environ.get(REDIS_URL))
152
153CACHES = {
154 "default": {
155 "BACKEND": "redis_cache.RedisCache",
156 "LOCATION": "{0}:{1}".format(redis_url.hostname, redis_url.port),
157 "OPTIONS": {
158 "PASSWORD": redis_url.password,
159 "DB": 0,
160 }
161 }
162
163}
164# BROKER_URL = REDIS_URL
165
166CELERY_BROKER_URL = REDIS_URL
167CELERY_RESULT_BACKEND = REDIS_URL
168
169CELERY_BROKER_TRANSPORT_OPTIONS = {'visibility_timeout': 3600}
170CELERY_ACCEPT_CONTENT = ['application/json']
171CELERY_TASK_SERIALIZER = 'json'
172CELERY_RESULT_SERIALIZER = 'json'