mirror of
https://github.com/dkmstr/openuds.git
synced 2025-01-12 09:17:56 +03:00
Adde generic factory and added module loader to jobs
This commit is contained in:
parent
07b0b00b90
commit
b9f57c47b0
@ -483,7 +483,7 @@ class Authenticator(Module):
|
||||
Returns:
|
||||
Nothing
|
||||
|
||||
:note: This method will be invoked whenever the webLogout is requested. It receibes request & response so auth cna
|
||||
:note: This method will be invoked whenever the webLogout is requested. It receives request & response so auth cna
|
||||
make changes (for example, on cookies) to it.
|
||||
|
||||
'''
|
||||
|
@ -34,7 +34,7 @@ import datetime
|
||||
import logging
|
||||
import typing
|
||||
|
||||
from uds.core.util import singleton
|
||||
from uds.core.util import factory
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@ -42,22 +42,7 @@ if typing.TYPE_CHECKING:
|
||||
from .job import Job
|
||||
|
||||
|
||||
class JobsFactory(metaclass=singleton.Singleton):
|
||||
_jobs: typing.MutableMapping[str, typing.Type['Job']]
|
||||
|
||||
def __init__(self):
|
||||
self._jobs = {}
|
||||
|
||||
def jobs(self) -> typing.Mapping[str, typing.Type['Job']]:
|
||||
return self._jobs
|
||||
|
||||
def insert(self, name: str, type_: typing.Type['Job']):
|
||||
logger.debug('Inserting job %s of type_ %s', name, type_)
|
||||
try:
|
||||
self._jobs[name] = type_
|
||||
except Exception as e:
|
||||
logger.debug('Exception at insert in JobsFactory: %s, %s', e.__class__, e)
|
||||
|
||||
class JobsFactory(factory.Factory['Job']):
|
||||
def ensureJobsInDatabase(self) -> None:
|
||||
"""
|
||||
Ensures that uds core workers are correctly registered in database and in factory
|
||||
@ -69,7 +54,7 @@ class JobsFactory(metaclass=singleton.Singleton):
|
||||
try:
|
||||
logger.debug('Ensuring that jobs are registered inside database')
|
||||
workers.initialize()
|
||||
for name, type_ in self._jobs.items():
|
||||
for name, type_ in self.objects().items():
|
||||
try:
|
||||
type_.setup()
|
||||
# We use database server datetime
|
||||
@ -99,6 +84,3 @@ class JobsFactory(metaclass=singleton.Singleton):
|
||||
e.__class__,
|
||||
e,
|
||||
)
|
||||
|
||||
def lookup(self, typeName: str) -> typing.Optional[typing.Type['Job']]:
|
||||
return self._jobs.get(typeName, None)
|
||||
|
@ -218,7 +218,7 @@ class Scheduler:
|
||||
"""
|
||||
# We ensure that the jobs are also in database so we can
|
||||
logger.debug('Run Scheduler thread')
|
||||
JobsFactory.factory().ensureJobsInDatabase()
|
||||
JobsFactory().ensureJobsInDatabase()
|
||||
logger.debug("At loop")
|
||||
while self._keepRunning:
|
||||
try:
|
||||
|
@ -93,7 +93,7 @@ class TaskManager(metaclass=singleton.Singleton):
|
||||
|
||||
def registerJob(self, jobType: typing.Type[jobs.Job]) -> None:
|
||||
jobName = jobType.friendly_name
|
||||
jobs.factory().insert(jobName, jobType)
|
||||
jobs.factory().put(jobName, jobType)
|
||||
|
||||
def registerScheduledTasks(self) -> None:
|
||||
logger.info("Registering sheduled tasks")
|
||||
|
@ -56,7 +56,7 @@ class Serializable:
|
||||
|
||||
:note: This method must be overridden
|
||||
"""
|
||||
raise Exception('Base marshaler called!!!')
|
||||
raise NotImplementedError('Base marshaler called!!!')
|
||||
|
||||
def unmarshal(self, data: bytes) -> None:
|
||||
"""
|
||||
@ -73,7 +73,7 @@ class Serializable:
|
||||
|
||||
:note: This method must be overridden
|
||||
"""
|
||||
raise Exception('Base unmarshaler called!!!')
|
||||
raise NotImplementedError('Base unmarshaler called!!!')
|
||||
|
||||
def serialize(self) -> str:
|
||||
"""
|
||||
|
@ -8,38 +8,34 @@ import logging
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
T = typing.TypeVar('T', bound=module.Module)
|
||||
V = typing.TypeVar('V')
|
||||
|
||||
|
||||
class ModuleFactory(typing.Generic[T], metaclass=singleton.Singleton):
|
||||
class Factory(typing.Generic[V], metaclass=singleton.Singleton):
|
||||
'''
|
||||
Module Factory class.
|
||||
Generic factory class.
|
||||
'''
|
||||
|
||||
_objects: typing.MutableMapping[str, typing.Type[T]]
|
||||
_objects: typing.MutableMapping[str, typing.Type[V]]
|
||||
|
||||
def __init__(self) -> None:
|
||||
self._objects = {}
|
||||
|
||||
def providers(self) -> typing.Mapping[str, typing.Type[T]]:
|
||||
def objects(self) -> typing.Mapping[str, typing.Type[V]]:
|
||||
'''
|
||||
Returns all providers.
|
||||
'''
|
||||
return self._objects
|
||||
|
||||
def insert(self, type_: typing.Type[T]) -> None:
|
||||
def put(self, typeName: str, type_: typing.Type[V]) -> None:
|
||||
'''
|
||||
Inserts an object into the factory.
|
||||
'''
|
||||
# logger.debug('Adding %s as %s', type_.type(), type_.__module__)
|
||||
typeName = type_.type().lower()
|
||||
|
||||
if typeName in self._objects:
|
||||
logger.debug('%s already registered as %s', type_, self._objects[typeName])
|
||||
return
|
||||
|
||||
self._objects[typeName] = type_
|
||||
|
||||
def get(self, typeName: str) -> typing.Optional[typing.Type[T]]:
|
||||
def get(self, typeName: str) -> typing.Optional[typing.Type[V]]:
|
||||
'''
|
||||
Returns an object from the factory.
|
||||
'''
|
||||
@ -48,3 +44,22 @@ class ModuleFactory(typing.Generic[T], metaclass=singleton.Singleton):
|
||||
# aliases for get
|
||||
lookup = get
|
||||
__getitem__ = get
|
||||
|
||||
|
||||
|
||||
class ModuleFactory(Factory[T]):
|
||||
'''
|
||||
Module Factory class.
|
||||
'''
|
||||
def providers(self) -> typing.Mapping[str, typing.Type[T]]:
|
||||
'''
|
||||
Returns all providers.
|
||||
'''
|
||||
return super().objects()
|
||||
|
||||
def insert(self, type_: typing.Type[T]) -> None:
|
||||
'''
|
||||
Inserts an object into the factory.
|
||||
'''
|
||||
# logger.debug('Adding %s as %s', type_.type(), type_.__module__)
|
||||
super().put(type_.type().lower(), type_)
|
||||
|
@ -1,28 +0,0 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
#
|
||||
# Copyright (c) 2012-2020 Virtual Cable S.L.U.
|
||||
# All rights reserved.
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without modification,
|
||||
# are permitted provided that the following conditions are met:
|
||||
#
|
||||
# * Redistributions of source code must retain the above copyright notice,
|
||||
# this list of conditions and the following disclaimer.
|
||||
# * Redistributions in binary form must reproduce the above copyright notice,
|
||||
# this list of conditions and the following disclaimer in the documentation
|
||||
# and/or other materials provided with the distribution.
|
||||
# * Neither the name of Virtual Cable S.L. nor the names of its contributors
|
||||
# may be used to endorse or promote products derived from this software
|
||||
# without specific prior written permission.
|
||||
#
|
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
Loading…
Reference in New Issue
Block a user