base.py
94 lines
| 3.5 KiB
| text/x-python
|
PythonLexer
r5325 | # Copyright (C) 2010-2023 RhodeCode GmbH | |||
# | ||||
# This program is free software: you can redistribute it and/or modify | ||||
# it under the terms of the GNU Affero General Public License, version 3 | ||||
# (only), as published by the Free Software Foundation. | ||||
# | ||||
# This program is distributed in the hope that it will be useful, | ||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
# GNU General Public License for more details. | ||||
# | ||||
# You should have received a copy of the GNU Affero General Public License | ||||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
# | ||||
# This program is dual-licensed. If you wish to learn more about the | ||||
# RhodeCode Enterprise Edition, including its added features, Support services, | ||||
# and proprietary license terms, please see https://rhodecode.com/licenses/ | ||||
r5459 | ||||
r5325 | import os | |||
import time | ||||
import logging | ||||
r5589 | from rhodecode.lib.config_utils import get_app_config_lightweight | |||
r5459 | ||||
from rhodecode.lib.svn_txn_utils import get_txn_id_from_store | ||||
r5325 | ||||
log = logging.getLogger(__name__) | ||||
class BaseHooksCallbackDaemon: | ||||
""" | ||||
Basic context manager for actions that don't require some extra | ||||
""" | ||||
def __init__(self): | ||||
pass | ||||
def __enter__(self): | ||||
log.debug('Running `%s` callback daemon', self.__class__.__name__) | ||||
return self | ||||
def __exit__(self, exc_type, exc_val, exc_tb): | ||||
log.debug('Exiting `%s` callback daemon', self.__class__.__name__) | ||||
class HooksModuleCallbackDaemon(BaseHooksCallbackDaemon): | ||||
def __init__(self, module): | ||||
super().__init__() | ||||
self.hooks_module = module | ||||
r5459 | def __repr__(self): | |||
return f'HooksModuleCallbackDaemon(hooks_module={self.hooks_module})' | ||||
r5325 | ||||
def prepare_callback_daemon(extras, protocol, host, txn_id=None): | ||||
r5459 | ||||
r5325 | match protocol: | |||
case 'http': | ||||
from rhodecode.lib.hook_daemon.http_hooks_deamon import HttpHooksCallbackDaemon | ||||
r5459 | port = 0 | |||
if txn_id: | ||||
# read txn-id to re-use the PORT for callback daemon | ||||
repo_path = os.path.join(extras['repo_store'], extras['repository']) | ||||
txn_details = get_txn_id_from_store(repo_path, txn_id) | ||||
port = txn_details.get('port', 0) | ||||
r5325 | callback_daemon = HttpHooksCallbackDaemon( | |||
txn_id=txn_id, host=host, port=port) | ||||
case 'celery': | ||||
from rhodecode.lib.hook_daemon.celery_hooks_deamon import CeleryHooksCallbackDaemon | ||||
r5589 | ||||
config = get_app_config_lightweight(extras['config']) | ||||
task_queue = config.get('celery.broker_url') | ||||
task_backend = config.get('celery.result_backend') | ||||
callback_daemon = CeleryHooksCallbackDaemon(task_queue, task_backend) | ||||
r5325 | case 'local': | |||
from rhodecode.lib.hook_daemon.hook_module import Hooks | ||||
callback_daemon = HooksModuleCallbackDaemon(Hooks.__module__) | ||||
case _: | ||||
log.error('Unsupported callback daemon protocol "%s"', protocol) | ||||
raise Exception('Unsupported callback daemon protocol.') | ||||
extras['hooks_uri'] = getattr(callback_daemon, 'hooks_uri', '') | ||||
extras['task_queue'] = getattr(callback_daemon, 'task_queue', '') | ||||
extras['task_backend'] = getattr(callback_daemon, 'task_backend', '') | ||||
extras['hooks_protocol'] = protocol | ||||
extras['time'] = time.time() | ||||
# register txn_id | ||||
extras['txn_id'] = txn_id | ||||
log.debug('Prepared a callback daemon: %s', | ||||
callback_daemon.__class__.__name__) | ||||
return callback_daemon, extras | ||||