[IMP] maintenance_service_http_monitoring: add double check on HTTP errors

to reduce "noise" from transient HTTP errors
This commit is contained in:
Stéphan Sainléger
2026-06-15 16:10:09 +02:00
parent cb3ed485b8
commit b9b8662bad
3 changed files with 121 additions and 31 deletions

View File

@@ -1,4 +1,5 @@
import logging
import time
from odoo import api, fields, models
@@ -10,6 +11,7 @@ except ImportError:
_logger = logging.getLogger(__name__)
HTTP_CHECK_TIMEOUT = 10 # seconds
HTTP_RETRY_DELAY = 2 # seconds between pass 1 and pass 2
class ServiceInstance(models.Model):
@@ -31,11 +33,18 @@ class ServiceInstance(models.Model):
)
def check_http_status(self):
"""
Perform HTTP check for each record and return the KO recordset.
Writes last_http_status_code, last_http_check_date and http_status_ok on every
checked record. Does NOT create maintenance.request — that decision belongs to
the caller (cron) after optional retry logic.
"""
ko_records = self.browse()
for rec in self:
if not rec.service_url or not rec.equipment_id:
continue
equipment = rec.equipment_id
if getattr(equipment, "maintenance_mode", False):
if rec.equipment_id.maintenance_mode:
continue
status_ok = False
status_code = -1
@@ -57,20 +66,36 @@ class ServiceInstance(models.Model):
}
)
if not status_ok:
# Delegate maintenance.request creation to equipment
if hasattr(equipment, "create_http_maintenance_request"):
equipment.create_http_maintenance_request([rec])
ko_records |= rec
return ko_records
@api.model
def cron_check_http_services(self):
"""
Check all active services with a URL, with one retry on failure.
Pass 1: test every eligible service.
If any fail, wait HTTP_RETRY_DELAY seconds then retest only the KO ones.
maintenance.request is created only for services that fail both passes,
reducing noise from transient HTTP errors.
"""
domain = [
("active", "=", True),
("service_url", "!=", False),
("equipment_id", "!=", False),
]
services = self.search(domain)
for service in services:
equipment = service.equipment_id
if getattr(equipment, "maintenance_mode", False):
continue
service.check_http_status()
services = self.search(domain).filtered(
lambda s: not s.equipment_id.maintenance_mode
)
ko_after_pass1 = services.check_http_status()
if not ko_after_pass1:
return
time.sleep(HTTP_RETRY_DELAY)
ko_confirmed = ko_after_pass1.check_http_status()
for service in ko_confirmed:
service.equipment_id.create_http_maintenance_request([service])