-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtasks.py
More file actions
34 lines (27 loc) · 1.07 KB
/
Copy pathtasks.py
File metadata and controls
34 lines (27 loc) · 1.07 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
from celery import Celery
from db_utils import get_db_connection
from fetch_nimble_contacts import fetch_nimble_contacts
app = Celery('tasks', broker='redis://localhost:6379/0')
@app.task
def update_contacts_from_nimble():
""" Updates contacts in the database from the Nimble service """
try:
contacts_data = fetch_nimble_contacts()
conn = get_db_connection()
cursor = conn.cursor()
for contact in contacts_data:
first_name = contact['first_name']
last_name = contact['last_name']
email = contact['emails'][0]['email'] if contact['emails'] else None
cursor.execute(
"INSERT INTO contacts (first_name, last_name, email) "
"VALUES (%s, %s, %s) "
"ON CONFLICT (email) DO UPDATE SET first_name = %s, last_name = %s",
(first_name, last_name, email, first_name, last_name)
)
conn.commit()
except Exception as e:
print(f"Error updating contacts: {e}")
finally:
cursor.close()
conn.close()