-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject_persistence.py
More file actions
49 lines (39 loc) · 1.73 KB
/
project_persistence.py
File metadata and controls
49 lines (39 loc) · 1.73 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
from multiprocessing import Pool
from multiprocessing_logging import install_mp_handler
import MySQLdb
import json
from kafka import KafkaConsumer
from persistence import Persistence
import logging
class ProjectPersistence(Persistence):
'''Project data persistence'''
def __init__(self, consumer_id):
'''Initialize through Persistence'''
Persistence.__init__(self, consumer_id, 'project_persistent')
logging.info("Consumer %d - initialized", self._id)
def persist_item(self, item):
'''Save project to database'''
try:
sql = 'DELETE FROM project_test WHERE project_slug = "' + item["slug"] + '"'
self._cursor.execute(sql)
self._db.commit()
sql = 'INSERT INTO project_test (project_slug, project_url, title, winner, tagline, teamsize, technique, hackathon_alias, description) VALUES ("' + item["slug"] + '", "' + item["url"] + '", "' + item["title"] + '", ' + item["winner"] + ', "' + item["tagline"] + '", ' + item["teamsize"] + ', "' + item["technique"] + '", "' + item["hackathon_alias"] + '", "' + item["description"] + '")'
self._cursor.execute(sql)
self._db.commit()
except MySQLdb.Error as e:
try:
logging.error("MySQL Error [%d]: %s; Error SQL: %s", e.args[0], e.args[1], sql)
except IndexError:
logging.error("MySQL Error %s", str(e))
def consumer_pool(consumer_id):
consumer = ProjectPersistence(consumer_id)
consumer.start_consumer()
def main():
logging.basicConfig(filename='project_persistence.log', filemode='w', format='%(asctime)s - %(levelname)s - %(message)s', level=logging.INFO)
install_mp_handler()
# Start multiple processes as Project Persistence
nprocess = 1
pool = Pool(nprocess)
results = pool.map(consumer_pool, range(1, nprocess + 1))
if __name__ == '__main__':
main()