<commit_before>#
# ovirt-engine-setup -- ovirt engine setup
# Copyright (C) 2013-2015 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#


""" DB Async tasks handling plugin."""


import distutils.version
import gettext
import time

from otopi import constants as otopicons
from otopi import base, plugin, util

from async_tasks_map import ASYNC_TASKS_MAP
from ovirt_engine_setup import constants as osetupcons
from ovirt_engine_setup import dialog
from ovirt_engine_setup.engine import constants as oenginecons
from ovirt_engine_setup.engine import vdcoption
from ovirt_engine_setup.engine_common import constants as oengcommcons
from ovirt_engine_setup.engine_common import database


def _(m):
    return gettext.dgettext(message=m, domain='ovirt-engine-setup')


@util.export
class Plugin(plugin.PluginBase):
    """ DB Async tasks handling plugin."""

    class _engineInMaintenance(base.Base):

        def __init__(
            self,
            parent,
            dbstatement,
        ):
            self._parent = parent
            self._origTimeout = 0
            self._dbstatement = dbstatement

        def _getCurrentTimeout(self):
            return vdcoption.VdcOption(
                statement=self._dbstatement,
            ).getVdcOption(
                name='AsyncTaskZombieTaskLifeInMinutes',
                ownConnection=True,
            )

        def _setEngineMode(self, maintenance, timeout=0):
            mode = (
                'MAINTENANCE' if maintenance
                else 'ACTIVE'
            )
            try:
                self._parent.logger.debug(
                    'Setting engine into {mode} mode'.format(
                        mode=mode,
                    )
                )

                vdcoption.VdcOption(
                    statement=self._dbstatement,
                ).updateVdcOptions(
                    options=(
                        {
                            'name': 'EngineMode',
                            'value': mode,
                        },
                        {
                            'name': 'AsyncTaskZombieTaskLifeInMinutes',
                            'value': timeout,
                        },
                    ),
                    ownConnection=True,
                )
            except Exception as e:
                self._parent.logger.debug(
                    'Cannot set engine mode',
                    exc_info=True,
                )
                raise RuntimeError(
                    _(
                        'Failed to set engine to {mode} mode.\n'
                        'Error: {error}\n'
                        'Please check that engine is in correct state '
                        'and try running upgrade again.'
                    ).format(
                        mode=mode,
                        error=e,
                    )
                )

        def __enter__(self):
            self._origTimeout = self._getCurrentTimeout()
            self._setEngineMode(
                maintenance=True,
            )

            self._parent.services.state(
                name=oenginecons.Const.ENGINE_SERVICE_NAME,
                state=True,
            )

        def __exit__(self, exc_type, exc_value, traceback):
            self._parent.services.state(
                name=oenginecons.Const.ENGINE_SERVICE_NAME,
                state=False,
            )
            self._setEngineMode(
                maintenance=False,
                timeout=self._origTimeout,
            )

    def _clearZombies(self):
        inst_v = distutils.version.LooseVersion(
            self.environment[
                osetupcons.CoreEnv.ORIGINAL_GENERATED_BY_VERSION
            ]
        ).version[:2]
        _args_base = (
            oenginecons.FileLocations.OVIRT_ENGINE_TASKCLEANER,
            '-l', self.environment[otopicons.CoreEnv.LOG_FILE_NAME],
            '-u', self.environment[oenginecons.EngineDBEnv.USER],
            '-s', self.environment[oenginecons.EngineDBEnv.HOST],
            '-p', str(self.environment[oenginecons.EngineDBEnv.PORT]),
            '-d', self.environment[oenginecons.EngineDBEnv.DATABASE],
            '-q',
        )
        envPwd = {
            'DBFUNC_DB_PGPASSFILE': self.environment[
                oenginecons.EngineDBEnv.PGPASS_FILE
            ]
        }

        if inst_v > [3, 4]:
            # removing zombie commands if present (just for 3.5 and upper)
            _args_commands = _args_base + ('-r', '-Z',)<commit_msg>packaging: setup: avoid setting AsyncTaskZombieTaskLifeInMinutes to 0<commit_after>#
# ovirt-engine-setup -- ovirt engine setup
# Copyright (C) 2013-2015 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#


""" DB Async tasks handling plugin."""


import distutils.version
import gettext
import time

from otopi import constants as otopicons
from otopi import base, plugin, util

from async_tasks_map import ASYNC_TASKS_MAP
from ovirt_engine_setup import constants as osetupcons
from ovirt_engine_setup import dialog
from ovirt_engine_setup.engine import constants as oenginecons
from ovirt_engine_setup.engine_common import constants as oengcommcons
from ovirt_engine_setup.engine_common import database


def _(m):
    return gettext.dgettext(message=m, domain='ovirt-engine-setup')


@util.export
class Plugin(plugin.PluginBase):
    """ DB Async tasks handling plugin."""

    class _engineInMaintenance(base.Base):

        def __init__(
            self,
            parent,
            dbstatement,
        ):
            self._parent = parent
            self._origTimeout = 0
            self._dbstatement = dbstatement

        def _setEngineMode(self, maintenance):
            mode = (
                'MAINTENANCE' if maintenance
                else 'ACTIVE'
            )
            try:
                self._parent.logger.debug(
                    'Setting engine into {mode} mode'.format(
                        mode=mode,
                    )
                )

            except Exception as e:
                self._parent.logger.debug(
                    'Cannot set engine mode',
                    exc_info=True,
                )
                raise RuntimeError(
                    _(
                        'Failed to set engine to {mode} mode.\n'
                        'Error: {error}\n'
                        'Please check that engine is in correct state '
                        'and try running upgrade again.'
                    ).format(
                        mode=mode,
                        error=e,
                    )
                )

        def __enter__(self):
            self._setEngineMode(
                maintenance=True,
            )

            self._parent.services.state(
                name=oenginecons.Const.ENGINE_SERVICE_NAME,
                state=True,
            )

        def __exit__(self, exc_type, exc_value, traceback):
            self._parent.services.state(
                name=oenginecons.Const.ENGINE_SERVICE_NAME,
                state=False,
            )
            self._setEngineMode(
                maintenance=False,
            )

    def _clearZombies(self):
        inst_v = distutils.version.LooseVersion(
            self.environment[
                osetupcons.CoreEnv.ORIGINAL_GENERATED_BY_VERSION
            ]
        ).version[:2]
        _args_base = (
            oenginecons.FileLocations.OVIRT_ENGINE_TASKCLEANER,
            '-l', self.environment[otopicons.CoreEnv.LOG_FILE_NAME],
            '-u', self.environment[oenginecons.EngineDBEnv.USER],
            '-s', self.environment[oenginecons.EngineDBEnv.HOST],
            '-p', str(self.environment[oenginecons.EngineDBEnv.PORT]),
            '-d', self.environment[oenginecons.EngineDBEnv.DATABASE],
            '-q',
        )
        envPwd = {
            'DBFUNC_DB_PGPASSFILE': self.environment[
                oenginecons.EngineDBEnv.PGPASS_FILE
            ]
        }

        if inst_v > [3, 4]:
            # removing zombie commands if present (just for 3.5 and upper)
            _args_commands = _args_base + ('-r', '-Z',)
{'fraction_non_alphanumeric': 0.07859403995197031, 'fraction_numerical': 0.0041480187752428775, 'mean_word_length': 1.6214592274678112, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7252565', 'n_tokens_mistral': 2425, 'n_tokens_neox': 2302, 'n_words': 598}