Skip to content

Commit

Permalink
Replaced print calls with logging module
Browse files Browse the repository at this point in the history
  • Loading branch information
trbtm committed Jul 2, 2024
1 parent 3912ebd commit 87709d6
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions postgresql_watcher/watcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
from multiprocessing import Process, Pipe
import time
from select import select
from logging import Logger, getLogger


POSTGRESQL_CHANNEL_NAME = "casbin_role_watcher"


def casbin_subscription(
process_conn: Pipe,
logger: Logger,
host: str,
user: str,
password: str,
Expand All @@ -20,7 +22,7 @@ def casbin_subscription(
sslmode: Optional[str] = None,
sslrootcert: Optional[str] = None,
sslcert: Optional[str] = None,
sslkey: Optional[str] = None
sslkey: Optional[str] = None,
):
# delay connecting to postgresql (postgresql connection failure)
time.sleep(delay)
Expand All @@ -39,14 +41,14 @@ def casbin_subscription(
conn.set_isolation_level(extensions.ISOLATION_LEVEL_AUTOCOMMIT)
curs = conn.cursor()
curs.execute(f"LISTEN {channel_name};")
print("Waiting for casbin policy update")
logger.debug("Waiting for casbin policy update")
while True and not curs.closed:
if not select([conn], [], [], 5) == ([], [], []):
print("Casbin policy update identified..")
logger.debug("Casbin policy update identified..")
conn.poll()
while conn.notifies:
notify = conn.notifies.pop(0)
print(f"Notify: {notify.payload}")
logger.debug(f"Notify: {notify.payload}")
process_conn.send(notify.payload)


Expand All @@ -63,7 +65,8 @@ def __init__(
sslmode: Optional[str] = None,
sslrootcert: Optional[str] = None,
sslcert: Optional[str] = None,
sslkey: Optional[str] = None
sslkey: Optional[str] = None,
logger: Optional[Logger] = None,
):
self.update_callback = None
self.parent_conn = None
Expand All @@ -77,6 +80,9 @@ def __init__(
self.sslrootcert = sslrootcert
self.sslcert = sslcert
self.sslkey = sslkey
if logger is None:
logger = getLogger()
self.logger = logger
self.subscribed_process = self.create_subscriber_process(start_process)

def create_subscriber_process(
Expand All @@ -91,6 +97,7 @@ def create_subscriber_process(
target=casbin_subscription,
args=(
child_conn,
self.logger,
self.host,
self.user,
self.password,
Expand All @@ -101,7 +108,7 @@ def create_subscriber_process(
self.sslmode,
self.sslrootcert,
self.sslcert,
self.sslkey
self.sslkey,
),
daemon=True,
)
Expand All @@ -110,7 +117,7 @@ def create_subscriber_process(
return p

def set_update_callback(self, fn_name: Callable):
print("runtime is set update callback", fn_name)
self.logger.debug(f"runtime is set update callback {fn_name}")
self.update_callback = fn_name

def update(self):
Expand Down Expand Up @@ -138,10 +145,10 @@ def should_reload(self):
try:
if self.parent_conn.poll(None):
message = self.parent_conn.recv()
print(f"message:{message}")
self.logger.debug(f"message:{message}")
return True
except EOFError:
print(
self.logger.warning(
"Child casbin-watcher subscribe process has stopped, "
"attempting to recreate the process in 10 seconds..."
)
Expand Down

0 comments on commit 87709d6

Please sign in to comment.