Skip to content
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

Add thread that trigger a refresh on wrong sources #1228

Merged
merged 1 commit into from
Feb 25, 2025
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
2 changes: 2 additions & 0 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ jobs:

- name: Checks
run: make checks
- run: git reset --hard
if: failure()

- name: Tests
run: make tests
Expand Down
46 changes: 46 additions & 0 deletions app/shared_config_manager/app.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import logging
import os
import time
from threading import Thread
from typing import Any

import c2cwsgiutils.pyramid
Expand All @@ -9,8 +12,11 @@

import shared_config_manager.security
import shared_config_manager.views
from shared_config_manager import slave_status
from shared_config_manager.sources import registry

_LOG = logging.getLogger(__name__)


def forbidden(request: pyramid.request.Request) -> pyramid.response.Response:
"""Redirect to the login page if the user is not authenticated."""
Expand All @@ -26,10 +32,50 @@ def forbidden(request: pyramid.request.Request) -> pyramid.response.Response:
)


def _watch_source():
"""Watch the source."""
while True:
try:
for key, source in registry.get_sources().items():
try:
slaves = slave_status.get_source_status(id_=key)
need_refresh = False
hash_ = source.get_hash()
for slave in slaves:
if slave is None or slave.get("filtered", False):
continue

if "hash" not in slave:
need_refresh = True
_LOG.warning(
"No hash in the slave '%s ' status for source '%s' -> refresh.",
slave.get("hostname"),
key,
)

if slave.get("hash") != hash_:
need_refresh = True
_LOG.warning(
"The hash in the slave '%s' status for source '%s' is different -> refresh.",
slave.get("hostname"),
key,
)

if need_refresh:
source.refresh()
except Exception: # pylint: disable=broad-exception-caught
_LOG.exception("Error while watching the source %s", key)
except Exception: # pylint: disable=broad-exception-caught
_LOG.exception("Error while watching the sources")
time.sleep(int(os.environ.get("WATCH_SOURCE_INTERVAL", "600")))


def main(_: Any, **settings: Any) -> Any:
"""Get the WSGI application."""
config = Configurator(settings=settings, route_prefix=os.environ.get("ROUTE_PREFIX", "/scm"))

Thread(target=_watch_source, daemon=True).start()

config.include(c2cwsgiutils.pyramid.includeme)
config.include("pyramid_mako")
config.set_security_policy(shared_config_manager.security.SecurityPolicy())
Expand Down