-
Notifications
You must be signed in to change notification settings - Fork 81
/
PostmonTaskScheduler.py
69 lines (57 loc) · 1.89 KB
/
PostmonTaskScheduler.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from datetime import timedelta
from celery import Celery
from celery.utils.log import get_task_logger
from IbgeTracker import IbgeTracker
import PackTracker
from database import MongoDb as Database
import os
USERNAME = os.environ.get('POSTMON_DB_USER')
PASSWORD = os.environ.get('POSTMON_DB_PASSWORD')
HOST = os.environ.get('POSTMON_DB_HOST', 'localhost')
PORT = os.environ.get('POSTMON_DB_PORT', '27017')
if all((USERNAME, PASSWORD)):
broker_conn_string = 'mongodb://%s:%s@%s:%s' \
% (USERNAME, PASSWORD, HOST, PORT)
else:
broker_conn_string = 'mongodb://%s:%s' % (HOST, PORT)
print(broker_conn_string)
app = Celery('postmon', broker=broker_conn_string)
app.conf.update(
CELERY_TASK_SERIALIZER='json',
CELERY_ACCEPT_CONTENT=['json'], # Ignore other content
CELERY_RESULT_SERIALIZER='json',
CELERY_TIMEZONE='America/Sao_Paulo',
CELERY_ENABLE_UTC=True,
CELERYBEAT_SCHEDULE={
'track_ibge_daily': {
'task': 'PostmonTaskScheduler.track_ibge',
'schedule': timedelta(days=1) # útil para
# testes: timedelta(minutes=1)
},
'track_packs': {
'task': 'PostmonTaskScheduler.track_packs',
'schedule': timedelta(hours=1),
}
}
)
logger = get_task_logger(__name__)
@app.task
def track_ibge():
logger.info('Iniciando tracking do IBGE...')
db = Database()
ibge = IbgeTracker()
ibge.track(db)
logger.info('Finalizou o tracking do IBGE')
@app.task
def track_packs():
logger.info('Iniciando tracking de pacotes...')
db = Database()
for obj in db.packtrack.get_all():
provider = obj['servico']
track = obj['codigo']
changed = PackTracker.run(provider, track)
if changed:
PackTracker.report(provider, track)
logger.info('Finalizou o tracking de pacotes')