Skip to content

Commit 1196caf

Browse files
authored
feat: replace print calls with logging module (#25)
* Replaced print calls with logging module * Fix action * Use postgres version 16
1 parent 3912ebd commit 1196caf

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

.github/workflows/release.yml

+3-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ jobs:
3333
- name: Setup PostgreSQL on macOS
3434
if: startsWith(runner.os, 'macos')
3535
run: |
36-
brew services start postgresql
36+
brew install postgresql@16
37+
brew services start postgresql@16
38+
export PATH="/opt/homebrew/opt/postgresql@16/bin:$PATH"
3739
echo "Check PostgreSQL service is running"
3840
i=10
3941
COMMAND='pg_isready'

postgresql_watcher/watcher.py

+16-9
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
from multiprocessing import Process, Pipe
44
import time
55
from select import select
6+
from logging import Logger, getLogger
67

78

89
POSTGRESQL_CHANNEL_NAME = "casbin_role_watcher"
910

1011

1112
def casbin_subscription(
1213
process_conn: Pipe,
14+
logger: Logger,
1315
host: str,
1416
user: str,
1517
password: str,
@@ -20,7 +22,7 @@ def casbin_subscription(
2022
sslmode: Optional[str] = None,
2123
sslrootcert: Optional[str] = None,
2224
sslcert: Optional[str] = None,
23-
sslkey: Optional[str] = None
25+
sslkey: Optional[str] = None,
2426
):
2527
# delay connecting to postgresql (postgresql connection failure)
2628
time.sleep(delay)
@@ -39,14 +41,14 @@ def casbin_subscription(
3941
conn.set_isolation_level(extensions.ISOLATION_LEVEL_AUTOCOMMIT)
4042
curs = conn.cursor()
4143
curs.execute(f"LISTEN {channel_name};")
42-
print("Waiting for casbin policy update")
44+
logger.debug("Waiting for casbin policy update")
4345
while True and not curs.closed:
4446
if not select([conn], [], [], 5) == ([], [], []):
45-
print("Casbin policy update identified..")
47+
logger.debug("Casbin policy update identified..")
4648
conn.poll()
4749
while conn.notifies:
4850
notify = conn.notifies.pop(0)
49-
print(f"Notify: {notify.payload}")
51+
logger.debug(f"Notify: {notify.payload}")
5052
process_conn.send(notify.payload)
5153

5254

@@ -63,7 +65,8 @@ def __init__(
6365
sslmode: Optional[str] = None,
6466
sslrootcert: Optional[str] = None,
6567
sslcert: Optional[str] = None,
66-
sslkey: Optional[str] = None
68+
sslkey: Optional[str] = None,
69+
logger: Optional[Logger] = None,
6770
):
6871
self.update_callback = None
6972
self.parent_conn = None
@@ -77,6 +80,9 @@ def __init__(
7780
self.sslrootcert = sslrootcert
7881
self.sslcert = sslcert
7982
self.sslkey = sslkey
83+
if logger is None:
84+
logger = getLogger()
85+
self.logger = logger
8086
self.subscribed_process = self.create_subscriber_process(start_process)
8187

8288
def create_subscriber_process(
@@ -91,6 +97,7 @@ def create_subscriber_process(
9197
target=casbin_subscription,
9298
args=(
9399
child_conn,
100+
self.logger,
94101
self.host,
95102
self.user,
96103
self.password,
@@ -101,7 +108,7 @@ def create_subscriber_process(
101108
self.sslmode,
102109
self.sslrootcert,
103110
self.sslcert,
104-
self.sslkey
111+
self.sslkey,
105112
),
106113
daemon=True,
107114
)
@@ -110,7 +117,7 @@ def create_subscriber_process(
110117
return p
111118

112119
def set_update_callback(self, fn_name: Callable):
113-
print("runtime is set update callback", fn_name)
120+
self.logger.debug(f"runtime is set update callback {fn_name}")
114121
self.update_callback = fn_name
115122

116123
def update(self):
@@ -138,10 +145,10 @@ def should_reload(self):
138145
try:
139146
if self.parent_conn.poll(None):
140147
message = self.parent_conn.recv()
141-
print(f"message:{message}")
148+
self.logger.debug(f"message:{message}")
142149
return True
143150
except EOFError:
144-
print(
151+
self.logger.warning(
145152
"Child casbin-watcher subscribe process has stopped, "
146153
"attempting to recreate the process in 10 seconds..."
147154
)

0 commit comments

Comments
 (0)