This repository was archived by the owner on Nov 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy path__init__.py
More file actions
162 lines (138 loc) · 5.68 KB
/
Copy path__init__.py
File metadata and controls
162 lines (138 loc) · 5.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
"""Background tasks for running Assembl.
Tasks are kept running by Supervisord_.
Short-lived tasks are written as Celery_ tasks; long-running tasks are
mostly ad hoc at this point: the :py:mod:`source_reader`
and :py:mod:`changes_router`.
.. _Supervisord: http://supervisord.org/
.. _Celery: http://www.celeryproject.org/
"""
from __future__ import absolute_import, print_function
from os import getcwd
from os.path import join, dirname, realpath, exists
import ConfigParser
from pyramid.paster import get_appsettings
from pyramid.path import DottedNameResolver
from datetime import timedelta
from celery import Celery
from pyramid_mailer import mailer_factory_from_settings
from ..lib.sqla import configure_engine
from ..lib.zmqlib import configure_zmq
from ..lib.config import get, set_config
from zope.component import getGlobalSiteManager
from ..lib.model_watcher import configure_model_watcher
from assembl.indexing.changes import configure_indexing
from ..lib.logging import getLogger
_settings = None
resolver = DottedNameResolver(__package__)
def configure(registry, task_name):
global _settings
from .threaded_model_watcher import configure_threaded_watcher
settings = registry.settings
if _settings is None:
_settings = settings
# temporary solution
configure_threaded_watcher(settings)
configure_model_watcher(registry, task_name)
region = get('aws_region', 'eu-west-1')
broker = settings.get('celery_tasks.broker', '')
config = {
"CELERY_TASK_SERIALIZER": 'json',
"CELERY_ACKS_LATE": True,
"CELERY_CACHE_BACKEND": broker,
"CELERY_STORE_ERRORS_EVEN_IF_IGNORED": True,
"BROKER_TRANSPORT_OPTIONS": {'region': region},
}
if not broker.startswith('sqs'):
config["CELERY_RESULT_BACKEND"] = broker
config['BROKER_URL'] = settings.get(
'%s.broker' % (celery.main,), None
) or settings.get('celery_tasks.broker')
celery.config_from_object(config, force=True)
CELERYBEAT_SCHEDULE = {
'resend-every-10-minutes': {
'task': 'assembl.processes.notify.process_pending_notifications',
'schedule': timedelta(seconds=600),
'options': {
'routing_key': 'notify',
'exchange': 'notify'
}
},
}
# Minimum delay between emails sent to a domain.
# For this to work, you need to have a SINGLE celery process for notification.
SMTP_DOMAIN_DELAYS = {
'': timedelta(0)
}
# INI file values with this prefix will be used to populate SMTP_DOMAIN_DELAYS.
# Anything after the last dot is a domain name (including empty).
# Use seconds (float) as values.
SETTINGS_SMTP_DELAY = "celery_tasks.notify.smtp_delay."
class CeleryWithConfig(Celery):
"A Celery task that can receive settings"
_preconf = {
"CELERYBEAT_SCHEDULE": CELERYBEAT_SCHEDULE
}
def on_configure(self):
global _settings
if _settings is None:
# i.e. includeme not called, i.e. not from pyramid
self.init_from_celery()
def init_from_celery(self):
# A task is called through celery, so it may not have basic
# configuration setup. Go through that setup the first time.
global _settings, SMTP_DOMAIN_DELAYS
rootdir = getcwd()
settings_file = join(rootdir, 'local.ini')
if not exists(settings_file):
settings_file = join(rootdir, 'production.ini')
if not exists(settings_file):
rootdir = dirname(dirname(dirname(realpath(__file__))))
settings_file = join(rootdir, 'local.ini')
if not exists(settings_file):
settings_file = join(rootdir, 'production.ini')
if not exists(settings_file):
raise RuntimeError("Missing settings file")
_settings = settings = get_appsettings(settings_file, 'assembl')
configure_zmq(settings['changes_socket'], False)
config = ConfigParser.SafeConfigParser()
config.read(settings_file)
registry = getGlobalSiteManager()
registry.settings = settings
set_config(settings)
configure_engine(settings, True)
configure_indexing()
if settings.get('%s_debug_signal' % (self.main,), False):
from assembl.lib import signals
signals.listen()
configure(registry, self.main)
from .threaded_model_watcher import ThreadDispatcher
threaded_watcher_class_name = settings.get(
'%s.threadedmodelwatcher' % (self.main,),
"assembl.lib.model_watcher.BaseModelEventWatcher")
ThreadDispatcher.mw_class = resolver.resolve(
threaded_watcher_class_name)
self.mailer = mailer_factory_from_settings(settings)
# setup SETTINGS_SMTP_DELAY
for name, val in settings.iteritems():
if name.startswith(SETTINGS_SMTP_DELAY):
try:
val = timedelta(seconds=float(val))
except ValueError:
print("Not a valid value for %s: %s" % (name, val))
continue
SMTP_DOMAIN_DELAYS[name[len(SETTINGS_SMTP_DELAY):]] = val
getLogger().info("SMTP_DOMAIN_DELAYS", delays=SMTP_DOMAIN_DELAYS)
import assembl.processes.imap
import assembl.processes.notify
import assembl.processes.notification_dispatch
import assembl.processes.translate
import assembl.processes.watson
assembl.processes.notification_dispatch.create_dispatcher()
celery = CeleryWithConfig('celery_tasks')
def includeme(config):
global _settings
_settings = config.registry.settings
config.include('.threaded_model_watcher')
configure(config.registry, 'assembl')
config.include('.source_reader')
config.include('.notification_dispatch')