sanic-org / sanic

Accelerate your web app development | Build fast. Run fast.
https://sanic.dev
MIT License
18.02k stars 1.55k forks source link

Added support to Multiplexer and Manager to terminate durable workers #2913

Closed john-woolley closed 7 months ago

john-woolley commented 7 months ago

I had a usecase for this in a project I'm working on and I thought it might be useful. Happy to make any changes to be more in-line with the existing codebase or planned trajectory.

codecov[bot] commented 7 months ago

Codecov Report

Attention: 11 lines in your changes are missing coverage. Please review.

Comparison is base (acb29c9) 88.039% compared to head (0f22249) 87.892%.

:exclamation: Current head 0f22249 differs from pull request most recent head 062adaa. Consider uploading reports for the commit 062adaa to get more accurate results

Files Patch % Lines
sanic/worker/manager.py 31.250% 8 Missing and 3 partials :warning:
Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #2913 +/- ## ============================================= - Coverage 88.039% 87.892% -0.148% ============================================= Files 94 94 Lines 7433 7450 +17 Branches 1283 1287 +4 ============================================= + Hits 6544 6548 +4 - Misses 622 632 +10 - Partials 267 270 +3 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

john-woolley commented 7 months ago

________________________ test_terminate_durable_worker _________________________

manager = <sanic.worker.manager.WorkerManager object at 0x7f4f58e75190>
caplog = <_pytest.logging.LogCaptureFixture object at 0x7f4f58f30910>

    def test_terminate_durable_worker(manager: WorkerManager, caplog):
        worker = manager.manage("TEST", fake_serve, kwargs={})

        assert "Sanic-TEST-0" in worker.worker_state
        assert len(manager.transient) == 1
        assert len(manager.durable) == 1

        manager.terminate_durable_worker("WRONG")
        message = "Durable worker termination failed because WRONG not found."

        assert "Sanic-TEST-0" in worker.worker_state
        assert len(manager.transient) == 1
        assert len(manager.durable) == 1
>       assert ("sanic.error", 40, message) in caplog.record_tuples
E       AssertionError: assert ('sanic.error', 40, 'Durable worker termination failed because WRONG not found.') in [('sanic.error', 40, 'Durable worker termination failed (None not found).')]
E        +  where [('sanic.error', 40, 'Durable worker termination failed (None not found).')] = <_pytest.logging.LogCaptureFixture object at 0x7f4f58f30910>.record_tuples

/home/runner/work/sanic/sanic/tests/worker/test_manager.py:433: AssertionError```

Closing this for now until I figure this out.