diff --git a/resallocserver/alembic/versions/b50e3f64fc2d_track_pool_max_value.py b/resallocserver/alembic/versions/b50e3f64fc2d_track_pool_max_value.py new file mode 100644 index 0000000..7a734a0 --- /dev/null +++ b/resallocserver/alembic/versions/b50e3f64fc2d_track_pool_max_value.py @@ -0,0 +1,17 @@ +""" +Track Pool max value +""" + +from alembic import op +import sqlalchemy as sa + +revision = 'b50e3f64fc2d' +down_revision = '78237445aff8' + +def upgrade(): + with op.batch_alter_table('pools', schema=None) as batch_op: + batch_op.add_column(sa.Column('max', sa.Integer(), nullable=True)) + +def downgrade(): + with op.batch_alter_table('pools', schema=None) as batch_op: + batch_op.drop_column('max') diff --git a/resallocserver/manager.py b/resallocserver/manager.py index 55e7d2e..35a5b7a 100644 --- a/resallocserver/manager.py +++ b/resallocserver/manager.py @@ -651,13 +651,7 @@ def _too_soon(self): last_start = 0.0 with session_scope() as session: dbinfo = session.query(models.Pool).get(self.name) - if not dbinfo: - dbinfo = models.Pool() - dbinfo.name = self.name - dbinfo.last_start = 0.0 - session.add(dbinfo) - else: - last_start = dbinfo.last_start + last_start = dbinfo.last_start is_too_soon = last_start + self.start_delay > time.time() if is_too_soon: @@ -1075,6 +1069,14 @@ def _decide_where_to_start_on_demand_instances(self, config, pools): app.log.debug("Ticket=%s can not start new resources, " "quotas reached", ticket_id) + def _init_pool_in_db(self, pool_config, session): + dbinfo = session.query(models.Pool).get(pool_config.name) + if not dbinfo: + dbinfo = models.Pool() + dbinfo.name = pool_config.name + dbinfo.last_start = 0.0 + session.add(dbinfo) + dbinfo.max = pool_config.max def _loop(self): app.log.info("Manager's loop.") @@ -1082,6 +1084,9 @@ def _loop(self): # Cleanup the old resources. cross_pool_config, pools = reload_config() + with session_scope() as session: + for _, config in pools.items(): + self._init_pool_in_db(config, session) for _, pool in pools.items(): pool.detect_closed_tickets(self.sync.ticket) diff --git a/resallocserver/models.py b/resallocserver/models.py index 735f317..b09f050 100644 --- a/resallocserver/models.py +++ b/resallocserver/models.py @@ -40,6 +40,7 @@ class Pool(Base): name = Column(String, primary_key=True) last_start = Column(Float, default=0) cleaning_unknown_resources = Column(DateTime, server_default=func.now()) + max = Column(Integer, default=0) class Ticket(Base, TagMixin): diff --git a/resallocwebui/app.py b/resallocwebui/app.py index a82b8d5..b025318 100644 --- a/resallocwebui/app.py +++ b/resallocwebui/app.py @@ -2,7 +2,7 @@ from flask import Flask, render_template from resallocserver.app import session_scope from resallocserver.logic import QResources -from resallocserver.manager import reload_config +from resallocserver import models from resalloc.helpers import RState from resallocwebui import staticdir, templatedir @@ -37,13 +37,11 @@ def pools(): # e.g. result["copr_hv_x86_64_01_prod"]["STARTING"] result = {} - # Read configuration from pools.yaml - _, pools_config = reload_config() - # Prepare the two-dimensional array, and fill it with zeros - for name, pool in pools_config.items(): - result[name] = dict.fromkeys(columns, 0) - result[name]["MAX"] = pools_config[name].max + with session_scope() as session: + for pool in session.query(models.Pool).all(): + result[pool.name] = dict.fromkeys(columns, 0) + result[pool.name]["MAX"] = pool.max with session_scope() as session: # Iterate over running resources and calculate how many is starting,