Skip to content

Commit

Permalink
[global] Logger is now global
Browse files Browse the repository at this point in the history
  • Loading branch information
adfaure committed Dec 12, 2023
1 parent 59e209e commit 4da7995
Show file tree
Hide file tree
Showing 81 changed files with 406 additions and 406 deletions.
10 changes: 4 additions & 6 deletions oar/api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
from sqlalchemy.orm import scoped_session, sessionmaker

from oar.lib.configuration import Configuration
from oar.lib.globals import init_config, init_oar
from oar.lib.globals import get_logger, init_config, init_oar

from .routers import frontend, job, media, proxy, resource, stress_factor

# from oar.api import API_VERSION
logger = get_logger("rest_api")

default_config = {
"API_TRUST_IDENT": 1,
Expand Down Expand Up @@ -47,18 +48,15 @@ def create_app(
config: Optional[Configuration] = None,
engine=None,
root_path: Optional[str] = None,
logger=None,
):
"""Return the OAR API application instance."""
app = FastAPI(root_path=root_path)

if not config:
config = init_config()

if engine is None and logger is None:
config, engine, logger = init_oar(config=config)
elif engine is None:
config, engine, _ = init_oar(config=config)
if engine is None:
config, engine = init_oar(config=config)

logger.info("creating app")

Expand Down
2 changes: 1 addition & 1 deletion oar/api/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from oar.lib.basequery import BaseQuery, BaseQueryCollection

# from oar.lib.models import (db, Job, Resource)
from oar.lib.utils import cached_property, row2dict
from oar.lib.utils import row2dict

# from flask import abort, current_app
# TODO: This whole file is to review since it has been adapted from flask and now use in fastapi.
Expand Down
2 changes: 1 addition & 1 deletion oar/cli/oaraccounting.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def cli(ctx, reinitialize, delete_before, version):
if ctx.obj:
(session, config) = ctx.obj
else:
config, engine, log = init_oar()
config, engine = init_oar()

session_factory = sessionmaker(bind=engine)
scoped = scoped_session(session_factory)
Expand Down
2 changes: 1 addition & 1 deletion oar/cli/oarconnect.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def cli(ctx, job_id):
if ctx.obj:
(session, config) = ctx.obj
else:
config, engine, log = init_oar()
config, engine = init_oar()

session_factory = sessionmaker(bind=engine)
scoped = scoped_session(session_factory)
Expand Down
2 changes: 1 addition & 1 deletion oar/cli/oardel.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ def cli(
session, config = ctx.obj

else:
config, engine, log = init_oar()
config, engine = init_oar()
session_factory = sessionmaker(bind=engine)
scoped = scoped_session(session_factory)
session = scoped()
Expand Down
2 changes: 1 addition & 1 deletion oar/cli/oarhold.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def cli(job_id, running, array, sql, version):
if ctx.obj:
(session, config) = ctx.obj
else:
config, engine, log = init_oar()
config, engine = init_oar()
session_factory = sessionmaker(bind=engine)
scoped = scoped_session(session_factory)

Expand Down
2 changes: 1 addition & 1 deletion oar/cli/oarnodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ def cli(
if ctx.obj:
(session, config) = ctx.obj
else:
config, engine, log = init_oar()
config, engine = init_oar()

session_factory = sessionmaker(bind=engine)
scoped = scoped_session(session_factory)
Expand Down
2 changes: 1 addition & 1 deletion oar/cli/oarnodesetting.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ def cli(
if ctx.obj:
(session, config) = ctx.obj
else:
config, engine, log = init_oar()
config, engine = init_oar()
session_factory = sessionmaker(bind=engine)
scoped = scoped_session(session_factory)
session = scoped()
Expand Down
2 changes: 1 addition & 1 deletion oar/cli/oarproperty.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def cli(list, type, add, varchar, delete, rename, quiet, version):
(session, engine, config) = ctx.obj
# engine = session.get_bind()
else:
config, engine, log = init_oar()
config, engine = init_oar()
session_factory = sessionmaker(bind=engine)
scoped = scoped_session(session_factory)
session = scoped()
Expand Down
3 changes: 2 additions & 1 deletion oar/cli/oarproxycleaner.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
def cli(period):
"""Proxy's rules cleaner which checks if jobs associated to Traefik proxy rule is running if not
rule is deleted from rules file"""
config, engine, log = init_oar()
config, engine = init_oar()

session_factory = sessionmaker(bind=engine)
scoped = scoped_session(session_factory)
session = scoped()
Expand Down
2 changes: 1 addition & 1 deletion oar/cli/oarqueue.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def cli(list, enable, disable, enable_all, disable_all, add, change, remove, ver
if ctx.obj:
(session, config) = ctx.obj
else:
config, engine, log = init_oar()
config, engine = init_oar()
session_factory = sessionmaker(bind=engine)
scoped = scoped_session(session_factory)
session = scoped()
Expand Down
4 changes: 2 additions & 2 deletions oar/cli/oarremoveresource.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ def cli(resource):
"""
ctx = click.get_current_context()
if ctx.obj:
(session, config) = ctx.obj
(session, _) = ctx.obj
else:
config, engine, log = init_oar()
_, engine = init_oar()
session_factory = sessionmaker(bind=engine)
scoped = scoped_session(session_factory)
session = scoped()
Expand Down
2 changes: 1 addition & 1 deletion oar/cli/oarresume.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def cli(job_id, array, sql, version):
if ctx.obj:
(session, config) = ctx.obj
else:
config, engine, log = init_oar()
config, engine = init_oar()
session_factory = sessionmaker(bind=engine)
scoped = scoped_session(session_factory)
session = scoped()
Expand Down
2 changes: 1 addition & 1 deletion oar/cli/oarstat.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ def cli(
if ctx.obj:
session = ctx.obj
else:
config, engine, log = init_oar()
_, engine = init_oar()
session_factory = sessionmaker(bind=engine)
scoped = scoped_session(session_factory)
session = scoped()
Expand Down
4 changes: 2 additions & 2 deletions oar/cli/oarsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,9 +441,9 @@ def cli(

ctx = click.get_current_context()
if ctx.obj:
(session, config) = ctx.obj
session, config = ctx.obj
else:
config, engine, log = init_oar()
config, engine = init_oar()

session_factory = sessionmaker(bind=engine)
scoped = scoped_session(session_factory)
Expand Down
2 changes: 1 addition & 1 deletion oar/cli/oarwalltime.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def cli(job_id, new_walltime, force, delay_next_jobs, version):
if ctx.obj:
session, config = ctx.obj
else:
config, engine, log = init_oar()
config, engine = init_oar()
session_factory = sessionmaker(bind=engine)
scoped = scoped_session(session_factory)
session = scoped()
Expand Down
4 changes: 2 additions & 2 deletions oar/kao/kamelot.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# Constant duration time of a besteffort job *)
besteffort_duration = 300 # TODO conf ???

config, db, log = init_oar(no_db=True)
config, db = init_oar(no_db=True)
logger = get_logger("oar.kamelot")


Expand Down Expand Up @@ -204,7 +204,7 @@ def schedule_cycle(session, config, plt, now, queues=["default"]):
#
def main(session=None, config=None):
if not session:
config, engine, log = init_oar(config)
config, engine = init_oar(config)

session_factory = sessionmaker(bind=engine)
scoped = scoped_session(session_factory)
Expand Down
2 changes: 1 addition & 1 deletion oar/kao/kamelot_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def schedule_cycle(session, config, plt, queues=["default"]):
#
def main(session=None, config=None):
if not session:
config, engine, log = init_oar(config)
config, engine = init_oar(config)

session_factory = sessionmaker(bind=engine)
scoped = scoped_session(session_factory)
Expand Down
4 changes: 2 additions & 2 deletions oar/kao/kamelot_fifo.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from oar.kao.scheduling_basic import find_resource_hierarchies_job
from oar.lib.globals import get_logger, init_oar

config, db, log = init_oar(no_db=True)
config, db = init_oar(no_db=True)


logger = get_logger("oar.kamelot_fifo")
Expand Down Expand Up @@ -112,7 +112,7 @@ def schedule_fifo_cycle(session, config, plt, queue="default", hierarchy_use=Fal
#
def main(session=None, config=None):
if not session:
config, engine, log = init_oar(config)
config, engine = init_oar(config)

session_factory = sessionmaker(bind=engine)
scoped = scoped_session(session_factory)
Expand Down
2 changes: 1 addition & 1 deletion oar/kao/kao.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

def main(session=None, config=None):
if not session:
config, engine, log = init_oar(config)
config, engine = init_oar(config)

session_factory = sessionmaker(bind=engine)
scoped = scoped_session(session_factory)
Expand Down
2 changes: 1 addition & 1 deletion oar/kao/multifactor_priority.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from oar.kao.karma import evaluate_jobs_karma
from oar.lib.globals import get_logger, init_oar

config, db, log = init_oar(no_db=True)
config, db = init_oar(no_db=True)
logger = get_logger("oar.kao.priorty")


Expand Down
2 changes: 1 addition & 1 deletion oar/kao/quotas.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"sun": 6,
}

config, db, log = init_oar(no_db=True)
config, db = init_oar(no_db=True)
logger = get_logger("oar.kao.quotas")


Expand Down
2 changes: 1 addition & 1 deletion oar/kao/scheduling.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# for quotas
from oar.lib.resource import ResourceSet

config, db, log = init_oar(no_db=True)
config, db = init_oar(no_db=True)
logger = get_logger("oar.kamelot", forward_stderr=True)


Expand Down
2 changes: 1 addition & 1 deletion oar/kao/walltime_change.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from oar.lib.tools import duration_to_sql, duration_to_sql_signed
from oar.lib.walltime import get_conf, update_walltime_change_request

config, db, log = init_oar(no_db=True)
config, db = init_oar(no_db=True)

logger = get_logger("oar.kao.walltime_change")

Expand Down
5 changes: 2 additions & 3 deletions oar/lib/globals.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,11 @@ def init_oar(config=None, no_db=False, no_reflect=False):
if not config:
config = init_config()

logger = create_logger(config)
if no_db:
return config, None, logger
return config, None
else:
engine = init_db(config, no_reflect=no_reflect)
return config, engine, logger
return config, engine


def init_and_get_session(config=None):
Expand Down
2 changes: 1 addition & 1 deletion oar/lib/walltime.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from oar.lib.models import WalltimeChange
from oar.lib.tools import duration_to_sql, duration_to_sql_signed, hms_to_duration

config, engine, log = init_oar(no_db=True)
config, engine = init_oar(no_db=True)

logger = get_logger("oar.lib.walltime")

Expand Down
2 changes: 1 addition & 1 deletion oar/modules/bipbip.py
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ def call_server_prologue(self, session, job, config, env={}):


def main(): # pragma: no cover
config, engine, log = init_oar()
config, engine = init_oar()

# Create a session maker
session_factory = sessionmaker(bind=engine)
Expand Down
2 changes: 1 addition & 1 deletion oar/modules/finaud.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
update_node_nextFinaudDecision,
)

config, db, log = init_oar(no_db=True)
config, db = init_oar(no_db=True)
logger = get_logger("oar.modules.finaud", forward_stderr=True)
logger.debug("Start Finaud")

Expand Down
2 changes: 1 addition & 1 deletion oar/modules/greta.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
get_nodes_with_given_sql,
)

config, db, log = init_oar(no_db=True)
config, db = init_oar(no_db=True)
logger = get_logger("oar.modules.greta", forward_stderr=True)


Expand Down
2 changes: 1 addition & 1 deletion oar/modules/leon.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ def run(self, session):


def main(): # pragma: no cover
config, engine, log = init_oar()
config, engine = init_oar()

leon = Leon(config, logger, sys.argv[1:])

Expand Down
2 changes: 1 addition & 1 deletion oar/modules/node_change_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ def suspend_job(self, session, job, event):


def main():
config, engine, log = init_oar()
config, engine = init_oar()

session_factory = sessionmaker(bind=engine)
# Legacy call
Expand Down
2 changes: 1 addition & 1 deletion oar/modules/sarko.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ def run(self, session):


def main(): # pragma: no cover
config, engine, log = init_oar()
config, engine = init_oar()

# Create a session maker
session_factory = sessionmaker(bind=engine)
Expand Down
4 changes: 2 additions & 2 deletions scripts/ci/docker/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ touch /etc/oar/oar.conf

chown $CONTAINER_UID:$CONTAINER_GID /etc/oar

wait-for-it -t 120 ${POSTGRES_HOST}:5432
# wait-for-it -t 120 ${POSTGRES_HOST}:5432

setpriv --reuid=$CONTAINER_UID --regid=$CONTAINER_GID --init-groups "$@"
"$@"
9 changes: 4 additions & 5 deletions tests/api/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def write_banned_file(config):

@pytest.fixture()
def fastapi_app(setup_config):
config, logger, engine = setup_config
config, engine = setup_config

tempdir = tempfile.mkdtemp()
# Config for jwt
Expand All @@ -78,13 +78,13 @@ def fastapi_app(setup_config):
config["API_REVOKED_TOKENS"] = os.path.join(tempdir, "tokens_revocation.json")
write_banned_file(config)

app = create_app(config=config, engine=engine, logger=logger)
app = create_app(config=config, engine=engine)
yield app


@pytest.fixture()
def user_tokens(setup_config):
config, _, _ = setup_config
config, _ = setup_config
tokens = {}

now = datetime.utcnow()
Expand All @@ -107,7 +107,6 @@ def user_tokens(setup_config):

@pytest.fixture(scope="function")
def client(fastapi_app, minimal_db_initialization, setup_config):
config, _, db = setup_config
with TestClient(fastapi_app) as app:
# override the get_db dependency to inject the test session
fastapi_app.dependency_overrides[get_db] = lambda: minimal_db_initialization
Expand Down Expand Up @@ -138,7 +137,7 @@ def monkeypatch_tools(request, monkeypatch):

@pytest.fixture(scope="function")
def minimal_db_initialization(setup_config, monkeypatch_tools):
_, _, engine = setup_config
_, engine = setup_config
session_factory = sessionmaker(bind=engine)
scoped = scoped_session(session_factory)

Expand Down
Loading

0 comments on commit 4da7995

Please sign in to comment.