-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
89 lines (71 loc) · 2.73 KB
/
app.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# source:
# https://kb.objectrocket.com/postgresql/python-and-postgresql-docker-container-part-2-1063
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import datetime
import psycopg2
import os
import time
hostname = os.environ['HOSTNAME']
hostname_replaced = hostname.replace('-', '_')
table_name = hostname_replaced
try:
conn = psycopg2.connect(
dbname = "test_db",
user = "jonas",
host = "minor-version-upgrade-test-aurora-cluster-primary.cluster-c4lg5bob8cbp.us-west-2.rds.amazonaws.com",
password = "password123456789"
)
except psycopg2.OperationalError as e:
# format time as 'HH:MM:SS CT'
current_datetime = datetime.datetime.now()
current_time = current_datetime.strftime('%T CT')
print(f"[ERROR]: database down at {current_time}")
else:
# declare a cursor object from the connection
cursor = conn.cursor()
# format time as 'HH:MM:SS CT'
current_datetime = datetime.datetime.now()
current_time = current_datetime.strftime('%T CT')
# execute an SQL statement using the psycopg2 cursor object
cursor.execute(f"create table {table_name}(id serial primary key, time char(11) not null)")
# not the same time, but oh well
print(f"[INFO]: table {table_name} created at {current_time}")
# actually have to commit changes, doh!
conn.commit()
# close the cursor object to avoid memory leaks
cursor.close()
# close the connection as well
conn.close()
# connection and write loop
while True:
try:
conn = psycopg2.connect(
dbname = "test_db",
user = "jonas",
host = "minor-version-upgrade-test-aurora-cluster-primary.cluster-c4lg5bob8cbp.us-west-2.rds.amazonaws.com",
password = "password123456789"
)
except psycopg2.OperationalError as e:
# format time as 'HH:MM:SS CT'
current_datetime = datetime.datetime.now()
current_time = current_datetime.strftime('%T CT')
print(f"[ERROR]: database down at {current_time}")
time.sleep(1)
else:
# declare a cursor object from the connection
cursor = conn.cursor()
# format time as 'HH:MM:SS CT'
current_datetime = datetime.datetime.now()
current_time = current_datetime.strftime('%T CT')
# execute an SQL statement using the psycopg2 cursor object
cursor.execute(f"INSERT into {table_name} (time) VALUES ('{current_time}')")
# not the same time, but oh well
print(f"[INFO]: database up at {current_time}")
# actually have to commit changes, doh!
conn.commit()
time.sleep(1)
# close the cursor object to avoid memory leaks
cursor.close()
# close the connection as well
conn.close()