1
0
mirror of https://github.com/dkmstr/openuds.git synced 2025-01-22 22:03:54 +03:00

Removed FixedOperation in favor of Operation

This commit is contained in:
Adolfo Gómez García 2024-04-02 21:59:41 +02:00
parent d264cffca2
commit 02fb954e29
No known key found for this signature in database
GPG Key ID: DD1ABF20724CDA23
4 changed files with 14 additions and 66 deletions

View File

@ -36,7 +36,7 @@ import typing
import collections.abc
from uds.core import services, types
from uds.core.types.services import FixedOperation as Operation
from uds.core.types.services import Operation
from uds.core.util import log, autoserializable
# Not imported at runtime, just for type checking
@ -107,7 +107,7 @@ class FixedUserService(services.UserService, autoserializable.AutoSerializable,
@typing.final
def _retry_later(self) -> str:
self._queue.insert(0, Operation.RETRY)
self._queue.insert(0, Operation.NOP)
return types.states.TaskState.RUNNING
def _error(self, reason: typing.Union[str, Exception]) -> types.states.TaskState:
@ -231,7 +231,7 @@ class FixedUserService(services.UserService, autoserializable.AutoSerializable,
return counter_state # Error or None
try:
check_function = _CHECKERS[op]
check_function = _CHECKERS[op] # If some operation not supported, will raise exception
# Invoke using instance, we have overrided methods
# and we want to use the overrided ones
@ -246,17 +246,6 @@ class FixedUserService(services.UserService, autoserializable.AutoSerializable,
logger.exception('Unexpected UserService check exception: %s', e)
return self._error(str(e))
@typing.final
def op_retry(self) -> None:
"""
Used to retry an operation
In fact, this will not be never invoked, unless we push it twice, because
check_state method will "pop" first item when a check operation returns State.FINISHED
At executeQueue this return value will be ignored, and it will only be used at check_state
"""
pass
@typing.final
def op_nop(self) -> None:
"""
@ -326,9 +315,6 @@ class FixedUserService(services.UserService, autoserializable.AutoSerializable,
"""
return types.states.TaskState.FINISHED
def op_retry_checker(self) -> types.states.TaskState:
return types.states.TaskState.FINISHED
def op_nop_checker(self) -> types.states.TaskState:
return types.states.TaskState.FINISHED
@ -413,22 +399,7 @@ class FixedUserService(services.UserService, autoserializable.AutoSerializable,
@staticmethod
def _op2str(op: Operation) -> str:
return {
Operation.CREATE: 'create',
Operation.START: 'start',
Operation.STOP: 'stop',
Operation.REMOVE: 'remove',
Operation.WAIT: 'wait',
Operation.ERROR: 'error',
Operation.FINISH: 'finish',
Operation.RETRY: 'retry',
Operation.SNAPSHOT_CREATE: 'snapshot_create',
Operation.SNAPSHOT_RECOVER: 'snapshot_recover',
Operation.PROCESS_TOKEN: 'process_token',
Operation.SHUTDOWN: 'soft_shutdown',
Operation.NOP: 'nop',
Operation.UNKNOWN: 'unknown',
}.get(op, '????')
return op.name
def _debug(self, txt: str) -> None:
# logger.debug('_name {0}: {1}'.format(txt, self._name))
@ -454,7 +425,6 @@ _EXECUTORS: typing.Final[
collections.abc.Mapping[Operation, collections.abc.Callable[[FixedUserService], None]]
] = {
Operation.CREATE: FixedUserService.op_create,
Operation.RETRY: FixedUserService.op_retry,
Operation.START: FixedUserService.op_start,
Operation.STOP: FixedUserService.op_stop,
Operation.WAIT: FixedUserService.op_nop, # Fixed assigned services has no cache 2, so no need to wait
@ -471,7 +441,6 @@ _CHECKERS: typing.Final[
collections.abc.Mapping[Operation, collections.abc.Callable[[FixedUserService], types.states.TaskState]]
] = {
Operation.CREATE: FixedUserService.op_create_checker,
Operation.RETRY: FixedUserService.op_retry_checker,
Operation.WAIT: FixedUserService.op_nop_checker, # Fixed assigned services has no cache 2, so no need to wait
Operation.START: FixedUserService.op_start_checker,
Operation.STOP: FixedUserService.op_stop_checker,

View File

@ -157,6 +157,8 @@ class Operation(enum.IntEnum):
# For example, on a future, all fixed userservice will be moved
# to this model, and we will need to "translate" the old values to the new ones
# So we will translate, for example SNAPSHOT_CREATE to CUSTOM_1, etc..
# Fixed user services does not allows custom operations, we use them
# to alias some fixed operations (like snapshot create, recover, etc..)
CUSTOM_1 = 20001
CUSTOM_2 = 20002
CUSTOM_3 = 20003
@ -166,6 +168,11 @@ class Operation(enum.IntEnum):
CUSTOM_7 = 20007
CUSTOM_8 = 20008
CUSTOM_9 = 20009
# Some alias for Fixed Services
SNAPSHOT_CREATE = CUSTOM_7
SNAPSHOT_RECOVER = CUSTOM_8
PROCESS_TOKEN = CUSTOM_9
def is_custom(self) -> bool:
"""
@ -183,32 +190,3 @@ class Operation(enum.IntEnum):
def as_str(self) -> str:
return self.name
# FixedOperation, currently used on FixedServices
# Will evolve to Operation on future
class FixedOperation(enum.IntEnum):
CREATE = 0
START = 1
STOP = 2
REMOVE = 3
WAIT = 4 # Here for compat, in fact, fixed services never waits
ERROR = 5
FINISH = 6
RETRY = 7
SNAPSHOT_CREATE = 8 # to recall process_snapshot
SNAPSHOT_RECOVER = 9 # to recall process_snapshot
PROCESS_TOKEN = 10
SHUTDOWN = 11
NOP = 98
UNKNOWN = 99
@staticmethod
def from_int(value: int) -> 'FixedOperation':
try:
return FixedOperation(value)
except ValueError:
return FixedOperation.UNKNOWN
def as_str(self) -> str:
return self.name

View File

@ -92,8 +92,8 @@ class FixedTestingService(fixed_service.FixedService):
# As we have snapshot on top of queue, we need to insert NOP -> STOP
# This way, NOP will be consumed right now, then start will be called and then
# this will be called again
userservice_instance._queue.insert(0, types.services.FixedOperation.STOP)
userservice_instance._queue.insert(0, types.services.FixedOperation.NOP)
userservice_instance._queue.insert(0, types.services.Operation.STOP)
userservice_instance._queue.insert(0, types.services.Operation.NOP)
self.first_process_called = True
def get_name(self, vmid: str) -> str:

View File

@ -378,6 +378,7 @@ def patched_provider(
client = create_client_mock()
provider = create_provider(**kwargs)
with mock.patch.object(provider, 'api') as api:
provider.do_log = mock.MagicMock() # Avoid logging
api.return_value = client
yield provider