#!/bin/bash

# cPanel hook for Whostmgr Accounts::Create/Modify/Remove (see cpanel-install-hooks).
#
# CLOS-6977: this used to rebuild the map itself, then ask the daemon to rebuild
# it again, then restart db_governor -- once per account event. The restart is
# what made bulk operations expensive: db_governor rate-limits dbmap refreshes to
# one per DBMAPHOOK_ANTIDDOS seconds (renew_map_on_request(), src/dbtop_server.c),
# and restarting it re-armed that limiter on every single event.
#
# 'dbctl dbupdate' alone is enough, and is what mysql_hook has always done:
# it makes db_governor run 'mysqlgovernor.py --dbupdate' (which runs
# dbgovernor_map) under its own limiter, and db_governor picks the rebuilt map
# up in-process -- no restart is involved in propagating it.

if [ -x /usr/sbin/dbctl ]
then
    if /usr/sbin/dbctl dbupdate
    then
        exit 0
    fi
else
    echo "Failed to find dbctl utility"
fi

# db_governor is unreachable (stopped, or its socket is gone), so nothing will
# rebuild the map on our behalf. Do it directly to keep the map current for the
# daemon's next start.
if [ -x /usr/share/lve/dbgovernor/utils/dbgovernor_map ]
then
    /usr/share/lve/dbgovernor/utils/dbgovernor_map
else
    echo "Failed to find dbgovernor_map script"
fi
