source: src/mapservice/server_impl/CeleryWorker.py@ 7be94aa

debian/bullseye
Last change on this file since 7be94aa was 7be94aa, checked in by Joachim Langenbach <joachim.langenbach@…>, 7 years ago
  • Added yapf style config (src/python-style.yapf)
    • Updated all files to the yapf formatted version
  • Property mode set to 100644
File size: 2.7 KB
Line 
1'''
2 Copyright (C) 2020 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16'''
17from celery import Celery
18import flask
19from sqlalchemy import event
20
21from . import config_object, app, application, celery, setup_app
22from .database.Model import db
23from .tasks import tasks
24
25
26# see https://flask.palletsprojects.com/en/1.1.x/patterns/celery/
27def setup_celery(flask_app, celery_app):
28 celery_app.conf.update(flask_app.config)
29 TaskBase = celery_app.Task
30
31 class ContextTask(TaskBase):
32 abstract = True
33
34 def __call__(self, *args, **kwargs):
35 with flask_app.app_context():
36
37 @event.listens_for(db.engine, "connect")
38 def load_spatialite(dbapi_conn, connection_record):
39 if 'sqlite' in flask.current_app.config[
40 'SQLALCHEMY_DATABASE_URI']:
41 # From https://geoalchemy-2.readthedocs.io/en/latest/spatialite_tutorial.html
42 dbapi_conn.enable_load_extension(True)
43 dbapi_conn.load_extension(
44 '/usr/local/lib64/mod_spatialite'
45 )
46
47 return TaskBase.__call__(self, *args, **kwargs)
48
49 celery_app.Task = ContextTask
50
51 with flask_app.app_context():
52 if not 'TILES_LITTLE_ZOOM_SCHEDULE' in flask.current_app.config:
53 flask.current_app.logger.error(
54 'TILES_LITTLE_ZOOM_SCHEDULE not configured'
55 )
56 raise ImportError('TILES_LITTLE_ZOOM_SCHEDULE not configured')
57 tiles_little_zoom_schedule = flask.current_app.config[
58 'TILES_LITTLE_ZOOM_SCHEDULE']
59
60 celery_app.conf.beat_schedule = {
61 'add-log_little_zoom_slice': {
62 'task':
63 'gnuboat_mapservice.server_impl.tasks.tasks.log_little_zoom_slice',
64 'schedule':
65 tiles_little_zoom_schedule
66 },
67 }
68
69 return celery_app
70
71
72setup_app('GNUBoat Celery Worker', config_object)
73setup_celery(application, celery)
Note: See TracBrowser for help on using the repository browser.