mirror of
https://github.com/arcan1s/ahriman.git
synced 2025-11-12 11:33:42 +00:00
Compare commits
2 Commits
10798b9ba3
...
a28589ec74
| Author | SHA1 | Date | |
|---|---|---|---|
| a28589ec74 | |||
| dfab5f56b2 |
@ -36,6 +36,7 @@ class Trigger(LazyLogging):
|
|||||||
CONFIGURATION_SCHEMA(ConfigurationSchema): (class attribute) configuration schema template
|
CONFIGURATION_SCHEMA(ConfigurationSchema): (class attribute) configuration schema template
|
||||||
CONFIGURATION_SCHEMA_FALLBACK(str | None): (class attribute) optional fallback option for defining
|
CONFIGURATION_SCHEMA_FALLBACK(str | None): (class attribute) optional fallback option for defining
|
||||||
configuration schema type used
|
configuration schema type used
|
||||||
|
REQUIRES_REPOSITORY(bool): (class attribute) either trigger requires loaded repository or not
|
||||||
configuration(Configuration): configuration instance
|
configuration(Configuration): configuration instance
|
||||||
repository_id(RepositoryId): repository unique identifier
|
repository_id(RepositoryId): repository unique identifier
|
||||||
|
|
||||||
@ -59,6 +60,7 @@ class Trigger(LazyLogging):
|
|||||||
|
|
||||||
CONFIGURATION_SCHEMA: ClassVar[ConfigurationSchema] = {}
|
CONFIGURATION_SCHEMA: ClassVar[ConfigurationSchema] = {}
|
||||||
CONFIGURATION_SCHEMA_FALLBACK: ClassVar[str | None] = None
|
CONFIGURATION_SCHEMA_FALLBACK: ClassVar[str | None] = None
|
||||||
|
REQUIRES_REPOSITORY: ClassVar[bool] = True
|
||||||
|
|
||||||
def __init__(self, repository_id: RepositoryId, configuration: Configuration) -> None:
|
def __init__(self, repository_id: RepositoryId, configuration: Configuration) -> None:
|
||||||
"""
|
"""
|
||||||
@ -79,6 +81,16 @@ class Trigger(LazyLogging):
|
|||||||
"""
|
"""
|
||||||
return self.repository_id.architecture
|
return self.repository_id.architecture
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_allowed_to_run(self) -> bool:
|
||||||
|
"""
|
||||||
|
whether trigger allowed to run or not
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
bool: ``True`` in case if trigger allowed to run and ``False`` otherwise
|
||||||
|
"""
|
||||||
|
return not (self.REQUIRES_REPOSITORY and self.repository_id.is_empty)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def configuration_schema(cls, configuration: Configuration | None) -> ConfigurationSchema:
|
def configuration_schema(cls, configuration: Configuration | None) -> ConfigurationSchema:
|
||||||
"""
|
"""
|
||||||
|
|||||||
@ -17,6 +17,7 @@
|
|||||||
# You should have received a copy of the GNU General Public License
|
# You should have received a copy of the GNU General Public License
|
||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
#
|
#
|
||||||
|
import atexit
|
||||||
import contextlib
|
import contextlib
|
||||||
import os
|
import os
|
||||||
|
|
||||||
@ -60,17 +61,8 @@ class TriggerLoader(LazyLogging):
|
|||||||
|
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
""""""
|
""""""
|
||||||
self._on_stop_requested = False
|
|
||||||
self.triggers: list[Trigger] = []
|
self.triggers: list[Trigger] = []
|
||||||
|
|
||||||
def __del__(self) -> None:
|
|
||||||
"""
|
|
||||||
custom destructor object which calls on_stop in case if it was requested
|
|
||||||
"""
|
|
||||||
if not self._on_stop_requested:
|
|
||||||
return
|
|
||||||
self.on_stop()
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def load(cls, repository_id: RepositoryId, configuration: Configuration) -> Self:
|
def load(cls, repository_id: RepositoryId, configuration: Configuration) -> Self:
|
||||||
"""
|
"""
|
||||||
@ -85,8 +77,9 @@ class TriggerLoader(LazyLogging):
|
|||||||
"""
|
"""
|
||||||
instance = cls()
|
instance = cls()
|
||||||
instance.triggers = [
|
instance.triggers = [
|
||||||
instance.load_trigger(trigger, repository_id, configuration)
|
trigger
|
||||||
for trigger in instance.selected_triggers(configuration)
|
for trigger_name in instance.selected_triggers(configuration)
|
||||||
|
if (trigger := instance.load_trigger(trigger_name, repository_id, configuration)).is_allowed_to_run
|
||||||
]
|
]
|
||||||
|
|
||||||
return instance
|
return instance
|
||||||
@ -250,10 +243,11 @@ class TriggerLoader(LazyLogging):
|
|||||||
run triggers on load
|
run triggers on load
|
||||||
"""
|
"""
|
||||||
self.logger.debug("executing triggers on start")
|
self.logger.debug("executing triggers on start")
|
||||||
self._on_stop_requested = True
|
|
||||||
for trigger in self.triggers:
|
for trigger in self.triggers:
|
||||||
with self.__execute_trigger(trigger):
|
with self.__execute_trigger(trigger):
|
||||||
trigger.on_start()
|
trigger.on_start()
|
||||||
|
# register on_stop call
|
||||||
|
atexit.register(self.on_stop)
|
||||||
|
|
||||||
def on_stop(self) -> None:
|
def on_stop(self) -> None:
|
||||||
"""
|
"""
|
||||||
|
|||||||
@ -3,6 +3,7 @@ from unittest.mock import MagicMock
|
|||||||
from ahriman.core.configuration import Configuration
|
from ahriman.core.configuration import Configuration
|
||||||
from ahriman.core.report import ReportTrigger
|
from ahriman.core.report import ReportTrigger
|
||||||
from ahriman.core.triggers import Trigger
|
from ahriman.core.triggers import Trigger
|
||||||
|
from ahriman.models.repository_id import RepositoryId
|
||||||
from ahriman.models.result import Result
|
from ahriman.models.result import Result
|
||||||
|
|
||||||
|
|
||||||
@ -13,6 +14,19 @@ def test_architecture(trigger: Trigger) -> None:
|
|||||||
assert trigger.architecture == trigger.repository_id.architecture
|
assert trigger.architecture == trigger.repository_id.architecture
|
||||||
|
|
||||||
|
|
||||||
|
def test_is_allowed_to_run(trigger: Trigger) -> None:
|
||||||
|
"""
|
||||||
|
must return flag correctly
|
||||||
|
"""
|
||||||
|
assert trigger.is_allowed_to_run
|
||||||
|
|
||||||
|
trigger.repository_id = RepositoryId("", "")
|
||||||
|
assert not trigger.is_allowed_to_run
|
||||||
|
|
||||||
|
trigger.REQUIRES_REPOSITORY = False
|
||||||
|
assert trigger.is_allowed_to_run
|
||||||
|
|
||||||
|
|
||||||
def test_configuration_schema(configuration: Configuration) -> None:
|
def test_configuration_schema(configuration: Configuration) -> None:
|
||||||
"""
|
"""
|
||||||
must return used configuration schema
|
must return used configuration schema
|
||||||
|
|||||||
@ -153,38 +153,12 @@ def test_on_start(trigger_loader: TriggerLoader, mocker: MockerFixture) -> None:
|
|||||||
"""
|
"""
|
||||||
upload_mock = mocker.patch("ahriman.core.upload.UploadTrigger.on_start")
|
upload_mock = mocker.patch("ahriman.core.upload.UploadTrigger.on_start")
|
||||||
report_mock = mocker.patch("ahriman.core.report.ReportTrigger.on_start")
|
report_mock = mocker.patch("ahriman.core.report.ReportTrigger.on_start")
|
||||||
|
atexit_mock = mocker.patch("atexit.register")
|
||||||
|
|
||||||
trigger_loader.on_start()
|
trigger_loader.on_start()
|
||||||
assert trigger_loader._on_stop_requested
|
|
||||||
report_mock.assert_called_once_with()
|
report_mock.assert_called_once_with()
|
||||||
upload_mock.assert_called_once_with()
|
upload_mock.assert_called_once_with()
|
||||||
|
atexit_mock.assert_called_once_with(trigger_loader.on_stop)
|
||||||
|
|
||||||
def test_on_stop_with_on_start(configuration: Configuration, mocker: MockerFixture) -> None:
|
|
||||||
"""
|
|
||||||
must call on_stop on exit if on_start was called
|
|
||||||
"""
|
|
||||||
mocker.patch("ahriman.core.upload.UploadTrigger.on_start")
|
|
||||||
mocker.patch("ahriman.core.report.ReportTrigger.on_start")
|
|
||||||
on_stop_mock = mocker.patch("ahriman.core.triggers.trigger_loader.TriggerLoader.on_stop")
|
|
||||||
_, repository_id = configuration.check_loaded()
|
|
||||||
|
|
||||||
trigger_loader = TriggerLoader.load(repository_id, configuration)
|
|
||||||
trigger_loader.on_start()
|
|
||||||
del trigger_loader
|
|
||||||
on_stop_mock.assert_called_once_with()
|
|
||||||
|
|
||||||
|
|
||||||
def test_on_stop_without_on_start(configuration: Configuration, mocker: MockerFixture) -> None:
|
|
||||||
"""
|
|
||||||
must call not on_stop on exit if on_start wasn't called
|
|
||||||
"""
|
|
||||||
on_stop_mock = mocker.patch("ahriman.core.triggers.trigger_loader.TriggerLoader.on_stop")
|
|
||||||
_, repository_id = configuration.check_loaded()
|
|
||||||
|
|
||||||
trigger_loader = TriggerLoader.load(repository_id, configuration)
|
|
||||||
del trigger_loader
|
|
||||||
on_stop_mock.assert_not_called()
|
|
||||||
|
|
||||||
|
|
||||||
def test_on_stop(trigger_loader: TriggerLoader, mocker: MockerFixture) -> None:
|
def test_on_stop(trigger_loader: TriggerLoader, mocker: MockerFixture) -> None:
|
||||||
|
|||||||
Reference in New Issue
Block a user