Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add global shutdown handler for strategy factory #278

Merged
merged 1 commit into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions arroyo/processing/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ def run(self) -> None:

logger.info("Closing %r...", self.__consumer)
self.__consumer.close()
self.__processor_factory.shutdown()
logger.info("Processor terminated")
raise

Expand Down Expand Up @@ -446,6 +447,7 @@ def _shutdown(self) -> None:
logger.info("Stopping consumer")
self.__metrics_buffer.flush()
self.__consumer.close()
self.__processor_factory.shutdown()
logger.info("Stopped")

# if there was an active processing strategy, it should be shut down
Expand Down
8 changes: 8 additions & 0 deletions arroyo/processing/strategies/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,11 @@ def create_with_partitions(
:param partitions: A mapping of a ``Partition`` to it's most recent offset.
"""
raise NotImplementedError

def shutdown(self) -> None:
"""
Custom code to execute when the ``StreamProcessor`` shuts down entirely.

Note that this code will also be executed on crashes of the strategy.
"""
pass
16 changes: 10 additions & 6 deletions tests/processing/test_processor.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import time
from datetime import datetime, timedelta
from typing import Any, Mapping, Optional, Sequence, cast
from unittest import mock
import time

import pytest
import py.path
import pytest

from arroyo.backends.local.backend import LocalBroker
from arroyo.backends.local.storages.abstract import MessageStorage
Expand Down Expand Up @@ -123,7 +123,9 @@ def test_stream_processor_lifecycle() -> None:
with pytest.raises(InvalidStateError):
processor._run_once()

with assert_changes(lambda: int(consumer.close.call_count), 0, 1):
with assert_changes(lambda: int(consumer.close.call_count), 0, 1), assert_changes(
lambda: int(factory.shutdown.call_count), 0, 1
):
processor._shutdown()

assert list((type(call), call.name) for call in metrics.calls) == [
Expand Down Expand Up @@ -564,12 +566,14 @@ def test_healthcheck(tmpdir: py.path.local) -> None:
strategy.submit.side_effect = InvalidMessage(partition, 1)
factory = mock.Mock()
factory.create_with_partitions.return_value = Healthcheck(
healthcheck_file=str(tmpdir.join("health.txt")),
next_step=strategy
healthcheck_file=str(tmpdir.join("health.txt")), next_step=strategy
)

processor: StreamProcessor[int] = StreamProcessor(
consumer, topic, factory, IMMEDIATE,
consumer,
topic,
factory,
IMMEDIATE,
)

# Assignment
Expand Down
Loading