· 8 years ago · Jan 03, 2018, 03:28 PM
1heroku ps:scale worker=1
2heroku run python
3>>>from Buylist.tasks import *
4>>>add(2,3)
5>>>5
6>>>add.delay(2,3)
7#-- Freezes until I press Control+C
8
9from __future__ import absolute_import, unicode_literals
10import os
11from celery import Celery
12
13
14# set the default Django settings module for the 'celery' program.
15os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings')
16
17app = Celery('project')
18
19# Using a string here means the worker doesn't have to serialize
20# the configuration object to child processes.
21# - namespace='CELERY' means all celery-related configuration keys
22# should have a `CELERY_` prefix.
23app.config_from_object('django.conf:settings', namespace='CELERY')
24app.conf.update(BROKER_URL=os.environ['REDIS_URL'],
25 CELERY_RESULT_BACKEND=os.environ['REDIS_URL'])
26
27# Load task modules from all registered Django app configs.
28app.autodiscover_tasks()
29
30
31@app.task(bind=True)
32def debug_task(self):
33 print('Request: {0!r}'.format(self.request))
34
35# Create your tasks here
36from __future__ import absolute_import, unicode_literals
37from celery import shared_task
38from celery import Celery
39from celery.schedules import crontab
40
41
42@shared_task
43def test(arg):
44 print(arg)
45
46@shared_task
47
48def add(x, y):
49
50 return x + y
51
52
53@shared_task
54
55def mul(x, y):
56
57 return x * y
58
59
60@shared_task
61
62def xsum(numbers):
63
64 return sum(numbers)
65
66from __future__ import absolute_import, unicode_literals
67import dj_database_url
68
69
70"""
71Django settings for project project.
72
73Generated by 'django-admin startproject' using Django 2.0.
74
75For more information on this file, see
76https://docs.djangoproject.com/en/2.0/topics/settings/
77
78For the full list of settings and their values, see
79https://docs.djangoproject.com/en/2.0/ref/settings/
80"""
81
82import os
83import django_heroku
84# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
85BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
86
87
88
89# Quick-start development settings - unsuitable for production
90# See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/
91
92# SECURITY WARNING: keep the secret key used in production secret!
93#Read secret key from a file
94
95SECRET_KEY = 'KEY'
96
97
98
99# SECURITY WARNING: don't run with debug turned on in production!
100#DEBUG = True
101DEBUG = bool( os.environ.get('DJANGO_DEBUG', True) )
102
103ALLOWED_HOSTS = [
104 'shrouded-ocean-19461.herokuapp.com', 'localhost', '127.0.0.1',
105
106]
107
108# Application definition
109
110INSTALLED_APPS = [
111 'django.contrib.admin',
112 'django.contrib.auth',
113 'django.contrib.contenttypes',
114 'django.contrib.sessions',
115 'django.contrib.messages',
116 'django.contrib.staticfiles',
117 'django_celery_results',
118 'Buylist',
119 #'django_q',
120
121]
122
123MIDDLEWARE = [
124 'django.middleware.security.SecurityMiddleware',
125 'whitenoise.middleware.WhiteNoiseMiddleware',
126 'django.contrib.sessions.middleware.SessionMiddleware',
127 'django.middleware.common.CommonMiddleware',
128 'django.middleware.csrf.CsrfViewMiddleware',
129 'django.contrib.auth.middleware.AuthenticationMiddleware',
130 'django.contrib.messages.middleware.MessageMiddleware',
131 'django.middleware.clickjacking.XFrameOptionsMiddleware',
132]
133
134ROOT_URLCONF = 'project.urls'
135
136PROJECT_DIR = os.path.dirname(__file__)
137TEMPLATES = [
138 {
139 'BACKEND': 'django.template.backends.django.DjangoTemplates',
140 'DIRS': [
141 os.path.join(PROJECT_DIR, "templates"),
142 ],
143 'APP_DIRS': True,
144 'OPTIONS': {
145 'context_processors': [
146 'django.template.context_processors.debug',
147 'django.template.context_processors.request',
148 'django.contrib.auth.context_processors.auth',
149 'django.contrib.messages.context_processors.messages',
150 ],
151 },
152 },
153]
154
155WSGI_APPLICATION = 'project.wsgi.application'
156
157
158# Database
159# https://docs.djangoproject.com/en/2.0/ref/settings/#databases
160
161DATABASES = {
162 'default': {
163 'ENGINE': 'django.db.backends.sqlite3',
164 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
165 }
166}
167
168
169# Password validation
170# https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators
171
172AUTH_PASSWORD_VALIDATORS = [
173 {
174 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
175 },
176 {
177 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
178 },
179 {
180 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
181 },
182 {
183 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
184 },
185]
186
187
188# Internationalization
189# https://docs.djangoproject.com/en/2.0/topics/i18n/
190
191LANGUAGE_CODE = 'en-us'
192
193TIME_ZONE = 'UTC'
194
195USE_I18N = True
196
197USE_L10N = True
198
199USE_TZ = True
200
201
202# Static files (CSS, JavaScript, Images)
203# https://docs.djangoproject.com/en/2.0/howto/static-files/
204
205# Heroku: Update database configuration from $DATABASE_URL.
206
207db_from_env = dj_database_url.config(conn_max_age=500)
208DATABASES['default'].update(db_from_env)
209
210# Static files (CSS, JavaScript, Images)
211# https://docs.djangoproject.com/en/1.10/howto/static-files/
212
213# The absolute path to the directory where collectstatic will collect static files for deployment.
214STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
215
216# The URL tos use when referring to static files (where they will be served from)
217STATIC_URL = '/static/'
218
219# Simplified static file serving.
220# https://warehouse.python.org/project/whitenoise/
221STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
222
223#CELERY_RESULT_BACKEND = 'django-db'
224
225CELERY_BROKER_URL = 'amqp://localhost//'
226
227from __future__ import absolute_import, unicode_literals
228
229# This will make sure the app is always imported when
230# Django starts so that shared_task will use this app.
231from .celery import app as celery_app
232
233__all__ = ['celery_app']
234
235web: gunicorn project.wsgi --log-file -
236worker: celery worker --app=tasks.app
237
238REDIS_URL=redis://h:"KEY" # KEY=long string of char