forked from MerginMaps/db-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlisten_test.py
58 lines (41 loc) · 1.5 KB
/
listen_test.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
import select
import psycopg2
import psycopg2.extensions
DSN = ""
conn = psycopg2.connect(DSN)
conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
curs = conn.cursor()
curs.execute("LISTEN geodiff;")
print("Waiting for notifications on channel 'geodiff'")
import dbsync
sleep_time = dbsync.config["daemon"]["sleep_time"]
while True:
if select.select([conn], [], [], 5,) == (
[],
[],
[],
):
print("Timeout")
print("Trying to pull")
dbsync.dbsync_pull()
else:
conn.poll()
while conn.notifies:
notify = conn.notifies.pop(0)
print(
"Got NOTIFY:",
notify.pid,
notify.channel,
notify.payload,
)
# new stuff in the database - let's push a new version
# how about if the transaction is not committed yet?
# Docs: "if a NOTIFY is executed inside a transaction, the notify events
# are not delivered until and unless the transaction is committed"
# TODO: need to wait before the changes are accessible?
print("Trying to push")
dbsync.dbsync_push()
# TODO: create on init
# CREATE RULE geodiff_rule_update_simple AS ON UPDATE TO gd_sync_base.simple DO ALSO NOTIFY geodiff;
# CREATE RULE geodiff_rule_insert_simple AS ON INSERT TO gd_sync_base.simple DO ALSO NOTIFY geodiff;
# CREATE RULE geodiff_rule_delete_simple AS ON DELETE TO gd_sync_base.simple DO ALSO NOTIFY geodiff;