-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
37 lines (32 loc) · 1.1 KB
/
utils.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
from connectors import MongoConnector, ElasticConnector
from pistonapi.steemnoderpc import SteemNodeRPC
from functools import wraps
import celery
import logging
logging.basicConfig(filename='log.log', format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')
RESTART_INTERVAL = 60 * 5
connectors = {
'mongo': MongoConnector,
'elasticsearch': ElasticConnector
}
def get_connectors(database, connector_type='mongo'):
"""
Returns connectors to a golos node and to a specified database
Database type should be selected from a list:
- Mongo
- Elasticsearch
"""
rpc = SteemNodeRPC("ws://localhost:8090", apis=["follow", "database"])
connector = connectors[connector_type](database)
return rpc, connector
class RestartableTask(celery.Task):
"""
Class to make all celery tasks restartable
"""
def on_failure(self, exc, task_id, args, kwargs, einfo):
"""
Puts log entry abount an exception
and restarts task when RESTART_INTERVAL ends
"""
logging.warning("Task {} ({}): {}".format(task_id, args, exc))
self.retry(countdown=RESTART_INTERVAL, exc=exc)