mirror of
https://github.com/arcan1s/ahriman.git
synced 2025-08-28 20:39:56 +00:00
feat: use atexit instead of del for triggers
This commit is contained in:
@ -17,6 +17,7 @@
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
import atexit
|
||||
import contextlib
|
||||
import os
|
||||
|
||||
@ -60,17 +61,8 @@ class TriggerLoader(LazyLogging):
|
||||
|
||||
def __init__(self) -> None:
|
||||
""""""
|
||||
self._on_stop_requested = False
|
||||
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
|
||||
def load(cls, repository_id: RepositoryId, configuration: Configuration) -> Self:
|
||||
"""
|
||||
@ -250,10 +242,11 @@ class TriggerLoader(LazyLogging):
|
||||
run triggers on load
|
||||
"""
|
||||
self.logger.debug("executing triggers on start")
|
||||
self._on_stop_requested = True
|
||||
for trigger in self.triggers:
|
||||
with self.__execute_trigger(trigger):
|
||||
trigger.on_start()
|
||||
# register on_stop call
|
||||
atexit.register(self.on_stop)
|
||||
|
||||
def on_stop(self) -> None:
|
||||
"""
|
||||
|
@ -153,38 +153,12 @@ def test_on_start(trigger_loader: TriggerLoader, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
upload_mock = mocker.patch("ahriman.core.upload.UploadTrigger.on_start")
|
||||
report_mock = mocker.patch("ahriman.core.report.ReportTrigger.on_start")
|
||||
atexit_mock = mocker.patch("atexit.register")
|
||||
|
||||
trigger_loader.on_start()
|
||||
assert trigger_loader._on_stop_requested
|
||||
report_mock.assert_called_once_with()
|
||||
upload_mock.assert_called_once_with()
|
||||
|
||||
|
||||
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()
|
||||
atexit_mock.assert_called_once_with(trigger_loader.on_stop)
|
||||
|
||||
|
||||
def test_on_stop(trigger_loader: TriggerLoader, mocker: MockerFixture) -> None:
|
||||
|
Reference in New Issue
Block a user