403Webshell
Server IP : 66.29.132.122  /  Your IP : 3.148.108.112
Web Server : LiteSpeed
System : Linux business142.web-hosting.com 4.18.0-553.lve.el8.x86_64 #1 SMP Mon May 27 15:27:34 UTC 2024 x86_64
User : admazpex ( 531)
PHP Version : 7.2.34
Disable Function : NONE
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : ON  |  Sudo : OFF  |  Pkexec : OFF
Directory :  /proc/self/root/proc/self/root/proc/thread-self/root/proc/thread-self/root/opt/cloudlinux/venv/lib/python3.11/site-packages/lvestats/plugins/generic/burster/storage/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ Back ]     

Current File : /proc/self/root/proc/self/root/proc/thread-self/root/proc/thread-self/root/opt/cloudlinux/venv/lib/python3.11/site-packages/lvestats/plugins/generic/burster/storage/cleanup.py
# coding=utf-8
#
# Copyright © Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2023 All Rights Reserved
#
# Licensed under CLOUD LINUX LICENSE AGREEMENT
# http://cloudlinux.com/docs/LICENSE.TXT
import contextlib
import time
from datetime import timedelta, datetime, timezone
from typing import Generator
from threading import Event

import sqlalchemy as sa
import sqlalchemy.exc

from lvestats.orm import bursting_events_table

from ..common import Timestamp
from .._logs import logger
from .base import thread_running


def cleanup_old_events(
    engine: sa.engine.Engine,
    server_id: str,
    cutoff: Timestamp,
) -> None:
    stmt = sa.delete(bursting_events_table).where(sa.and_(
        bursting_events_table.c.timestamp <= cutoff,
        bursting_events_table.c.server_id == server_id,
    ))
    engine.execute(stmt)


@contextlib.contextmanager
def cleanup_running(
    engine: sa.engine.Engine,
    server_id: str,
    cleanup_interval: timedelta,
    history_window: timedelta,
    run_period: timedelta = timedelta(seconds=5),
    fail_fast: bool = True,
) -> Generator[None, None, None]:
    def main(terminate: Event):
        # FIXME(vlebedev): It will take  ~`dump_period` in the worst case for thread to respond to termination request.
        #                  Loop more frequently?
        prev_db_write_time = 0.0
        while not terminate.is_set():
            now = time.time()
            if (now - prev_db_write_time) > cleanup_interval.total_seconds():
                cutoff = Timestamp(int(now-history_window.total_seconds()))
                try:
                    cleanup_old_events(engine, server_id, cutoff)
                except sqlalchemy.exc.DBAPIError as e:
                    if fail_fast:
                        raise e
                    logger.error('Failed to cleanup bursting events from DB!', exc_info=e)
                else:
                    prev_db_write_time = now
            time.sleep(run_period.total_seconds())
        logger.debug('Stopping events cleanup thread.')

    with thread_running('bursting-cleanup', main):
        yield

Youez - 2016 - github.com/yon3zu
LinuXploit