ok

Mini Shell

Direktori : /opt/cloudlinux/venv/lib/python3.11/site-packages/xray/reconfiguration/
Upload File :
Current File : //opt/cloudlinux/venv/lib/python3.11/site-packages/xray/reconfiguration/migrate.py

# -*- coding: utf-8 -*-

# Copyright © Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2021 All Rights Reserved
#
# Licensed under CLOUD LINUX LICENSE AGREEMENT
# http://cloudlinux.com/docs/LICENSE.TXT

import logging
import os
import time

from xray.apiclient import get_client
from xray.internal.exceptions import XRayError
from xray.internal.utils import read_sys_id
from xray.manager import initialize_manager
from xray.reconfiguration.system_id_shift import (
    remove_xray_inis,
    clear_dbm_file
)

storage = '/usr/share/alt-php-xray/running_tasks_list'
marker = '/usr/share/alt-php-xray/migrated'
migration_log = '/var/log/alt-php-xray/migration.log'

logger = logging.getLogger('migrate')


def running_tasks() -> list:
    """
    Get list of running tasks and save it into file
    """
    client = get_client('lists')(system_id=read_sys_id())
    try:
        tasks = client.get_task_list()
        logger.info('Loaded tasks: %s', tasks)
    except XRayError:
        return list()
    running = [t['tracing_task_id'] for t in tasks['result'] if
               t['status'] == 'running']
    logger.info('Running tasks: %s', running)
    with open(storage, 'w') as f:
        f.write('\n'.join(running))
    return running


def stop_tasks() -> None:
    """
    Stop running tasks.
    Try to remove ini and clear dbm storage after
    """
    manager = initialize_manager(read_sys_id())
    for task in running_tasks():
        logger.info('Stopping task %s', task)
        try:
            manager.stop(task)
        except XRayError:
            pass
        time.sleep(1)
    # clear if some garbage remains
    remove_xray_inis()
    clear_dbm_file()


def continue_tasks() -> None:
    """"""
    manager = initialize_manager(read_sys_id())
    try:
        with open(storage) as f:
            tasks = [l for l in f.read().split('\n') if l]
    except OSError:
        return

    logger.info('Saved task list loaded: %s', tasks)
    os.unlink(storage)
    for task in tasks:
        logger.info('Continue task %s', task)
        try:
            manager.continue_(task)
        except XRayError:
            pass
        time.sleep(1)
    # leave marker
    open(marker, 'w').close()

Zerion Mini Shell 1.0