Skip to content

Commit e6e49f1

Browse files
fix: Run noop loop in scheduler under dispatcherd flag (#1332)
This PR changes the approach to handle the dispatcherd flag in the scheduler. Instead or returning an error and creating noise for users, that also need to modify their deployments to not deploy the scheduler which would be restarting all the time in default deployments, it applies a less disruptive approach by just warning about it and running a noop loop. Signed-off-by: Alex <[email protected]>
1 parent a7642fe commit e6e49f1

File tree

3 files changed

+37
-14
lines changed

3 files changed

+37
-14
lines changed

src/aap_eda/core/exceptions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,7 @@ class DuplicateEnvKeyError(Exception):
4343

4444
class InvalidEnvKeyError(Exception):
4545
pass
46+
47+
48+
class GracefulExit(Exception):
49+
pass

src/aap_eda/core/management/commands/scheduler.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@
7070
https://github.com/rq/rq-scheduler/blob/master/README.rst
7171
"""
7272
import logging
73+
import signal
74+
import time
7375
import typing
7476
from datetime import datetime
7577

@@ -79,6 +81,7 @@
7981
from django_rq.management.commands import rqscheduler
8082

8183
from aap_eda.core import tasking
84+
from aap_eda.core.exceptions import GracefulExit
8285
from aap_eda.settings import features
8386
from aap_eda.utils.logging import startup_logging
8487

@@ -167,15 +170,27 @@ class Command(rqscheduler.Command):
167170

168171
def handle(self, *args, **options) -> None:
169172
if features.DISPATCHERD:
170-
self.stderr.write(
171-
self.style.ERROR(
172-
"This command is not supported when "
173-
f"{settings.DISPATCHERD_FEATURE_FLAG_NAME} is enabled. "
174-
"Please disable the feature flag in your settings "
175-
"or update your deployment.",
176-
)
173+
self.stdout.write(
174+
self.style.WARNING(
175+
f"{settings.DISPATCHERD_FEATURE_FLAG_NAME} flag "
176+
"is enabled. This command is not required in this setup. "
177+
"You may update your deployment configuration to omit "
178+
"this process. \nRunning in background (noop mode)...",
179+
),
177180
)
178-
raise SystemExit(1)
181+
182+
def handle_exit(signum, frame):
183+
raise GracefulExit()
184+
185+
signal.signal(signal.SIGTERM, handle_exit)
186+
signal.signal(signal.SIGINT, handle_exit)
187+
188+
try:
189+
while True:
190+
time.sleep(60)
191+
except GracefulExit:
192+
self.stdout.write(self.style.NOTICE("Exiting noop mode."))
193+
return
179194

180195
# interval can be set through the command line
181196
# but we want to manage it through settings

tests/unit/commands/test_rq_cmd_wrapper.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import signal
1516
from unittest import mock
1617

1718
import pytest
@@ -67,13 +68,16 @@ def test_rqscheduler_command_feature_flag_disabled(
6768
@mock.patch(
6869
"aap_eda.core.management.commands.scheduler.features.DISPATCHERD", True
6970
)
70-
@mock.patch.object(rqscheduler.Command, "handle", return_value=None)
71+
@mock.patch("time.sleep")
7172
def test_rqscheduler_command_feature_flag_enabled(
72-
mock_rqscheduler_handle,
73+
mock_sleep,
7374
capsys,
7475
):
75-
with pytest.raises(SystemExit) as exc_info:
76-
call_command("scheduler")
77-
assert exc_info.value.code == 1
76+
def fake_sleep(_):
77+
signal.raise_signal(signal.SIGTERM)
78+
79+
mock_sleep.side_effect = fake_sleep
80+
call_command("scheduler")
7881
captured = capsys.readouterr()
79-
assert "This command is not supported" in captured.err
82+
assert "This command is not required" in captured.out
83+
assert "Exiting noop mode." in captured.out

0 commit comments

Comments
 (0)