Skip to content

feat: replace print calls with logging module #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ jobs:
- name: Setup PostgreSQL on macOS
if: startsWith(runner.os, 'macos')
run: |
brew services start postgresql
brew install postgresql@16
brew services start postgresql@16
export PATH="/opt/homebrew/opt/postgresql@16/bin:$PATH"
echo "Check PostgreSQL service is running"
i=10
COMMAND='pg_isready'
Expand Down
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