runbot/runbot/models/host.py
Xavier-Do 02d2cc4528 [WIP] runbot: monitoring tools
Add a new model runbot.host to keep info and configuration about
hosts (worker servers), like number of worker, reserved or not,
ping times (last start loop, successful iteration, end loop, ...)
and also last errors, number of testing per host, psql connection
count, ...

A new monitoring frontend page is created, similar to glances
but with additionnal information like hosts states and
last_monitored builds (for nightly)

Later this model will be used for runbot_build host instead of char.

Host are automaticaly created when running _scheduler.
2019-09-03 11:10:38 +02:00

61 lines
2.4 KiB
Python

import logging
from odoo import models, fields, api
from ..common import fqdn, local_pgadmin_cursor
_logger = logging.getLogger(__name__)
class RunboHost(models.Model):
_name = "runbot.host"
_order = 'id'
_inherit = 'mail.thread'
name = fields.Char('Host name', required=True, unique=True)
disp_name = fields.Char('Display name')
active = fields.Boolean('Active', default=True)
last_start_loop = fields.Datetime('Last start')
last_end_loop = fields.Datetime('Last end')
last_success = fields.Datetime('Last success')
assigned_only = fields.Boolean('Only accept assigned build', default=False)
nb_worker = fields.Integer('Number of max paralel build', help="0 to use icp value", default=0)
nb_testing = fields.Integer(compute='_compute_nb')
nb_running = fields.Integer(compute='_compute_nb')
last_exception = fields.Char('Last exception')
exception_count = fields.Integer('Exception count')
psql_conn_count = fields.Integer('SQL connections count', default=0)
def _compute_nb(self):
groups = self.env['runbot.build'].read_group(
[('host', 'in', self.mapped('name')), ('local_state', 'in', ('testing', 'running'))],
['host', 'local_state'],
['host', 'local_state'],
lazy=False
)
count_by_host_state = {host.name: {} for host in self}
for group in groups:
count_by_host_state[group['host']][group['local_state']] = group['__count']
for host in self:
host.nb_testing = count_by_host_state[host.name].get('testing', 0)
host.nb_running = count_by_host_state[host.name].get('running', 0)
@api.model
def create(self, values):
if not 'disp_name' in values:
values['disp_name'] = values['name']
return super().create(values)
@api.model
def _get_current(self):
name = fqdn()
return self.search([('name', '=', name)]) or self.create({'name': name})
def get_nb_worker(self):
icp = self.env['ir.config_parameter']
return self.nb_worker or int(icp.sudo().get_param('runbot.runbot_workers', default=6))
def set_psql_conn_count(self):
self.ensure_one()
with local_pgadmin_cursor() as local_cr:
local_cr.execute("SELECT sum(numbackends) FROM pg_stat_database;")
res = local_cr.fetchone()
self.psql_conn_count = res and res[0] or 0