-
Notifications
You must be signed in to change notification settings - Fork 8
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
Add dev_server library #173
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
de70c6a
Fix mocked lib tests not using real Inngest client
amh4r 04ef5bc
Tweak import
amh4r ec9463a
Create dev_server library
amh4r 8beaee7
Use latest
amh4r 6a97ad9
Maybe fix
amh4r 4b37f96
Refactor
amh4r 6e6b74b
Maybe fix hanging
amh4r af29362
Maybe fix hanging
amh4r 7b7a56f
try
amh4r d6f31e7
try
amh4r 9f11376
Add stop_event
amh4r 4a2dcdf
Skip test
amh4r File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# ruff: noqa: D104 | ||
|
||
from .dev_server import server | ||
|
||
__all__ = ["server"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
# ruff: noqa: S603, S607, T201 | ||
|
||
import os | ||
import subprocess | ||
import threading | ||
import time | ||
import typing | ||
|
||
import httpx | ||
|
||
|
||
class _Server: | ||
@property | ||
def origin(self) -> str: | ||
return f"http://0.0.0.0:{self.port}" | ||
|
||
def __init__(self) -> None: | ||
self._enabled = os.getenv("DEV_SERVER_ENABLED") != "0" | ||
self._output_thread: typing.Optional[threading.Thread] = None | ||
|
||
port: int | ||
dev_server_port_env_var = os.getenv("DEV_SERVER_PORT") | ||
if dev_server_port_env_var: | ||
port = int(dev_server_port_env_var) | ||
else: | ||
port = 8288 | ||
self.port = port | ||
|
||
self._process: typing.Optional[subprocess.Popen[str]] = None | ||
self._ready_event = threading.Event() | ||
self._stop_event = threading.Event() | ||
|
||
def start(self) -> None: | ||
if self._enabled is False: | ||
return | ||
|
||
print("Dev Server: starting") | ||
|
||
if self._process: | ||
raise Exception("Dev Server is already running") | ||
|
||
self._process = subprocess.Popen( | ||
[ | ||
"npx", | ||
"--yes", | ||
"inngest-cli@latest", | ||
"dev", | ||
"--no-discovery", | ||
"--no-poll", | ||
"--port", | ||
f"{self.port}", | ||
], | ||
bufsize=1, | ||
stderr=subprocess.STDOUT, | ||
stdout=subprocess.PIPE, | ||
text=True, | ||
universal_newlines=True, | ||
) | ||
|
||
self._ready_event.clear() | ||
self._output_thread = threading.Thread(target=self._print_output) | ||
self._output_thread.start() | ||
self._wait_for_server() | ||
|
||
def _print_output(self) -> None: | ||
if self._process is None: | ||
raise Exception("missing process") | ||
if self._process.stdout is None: | ||
raise Exception("missing stdout") | ||
|
||
for line in self._process.stdout: | ||
if self._stop_event.is_set(): | ||
break | ||
|
||
if self._ready_event.is_set() is False: | ||
print(line, end="") | ||
|
||
def _wait_for_server(self) -> None: | ||
print("Dev Server: waiting for start") | ||
|
||
while not self._ready_event.is_set(): | ||
try: | ||
httpx.get(f"http://127.0.0.1:{self.port}") | ||
self._ready_event.set() | ||
break | ||
except Exception: | ||
time.sleep(0.1) | ||
|
||
print("Dev Server: started") | ||
|
||
def stop(self) -> None: | ||
if self._enabled is False: | ||
return | ||
|
||
print("Dev Server: stopping") | ||
|
||
if self._output_thread is None: | ||
raise Exception("missing output thread") | ||
if self._process is None: | ||
raise Exception("missing process") | ||
|
||
self._process.terminate() | ||
|
||
# Try to gracefully stop but kill it if that fails. | ||
try: | ||
self._process.wait(timeout=5) | ||
except subprocess.TimeoutExpired: | ||
self._process.kill() | ||
self._process.wait(timeout=5) | ||
|
||
self._stop_event.set() | ||
self._output_thread.join() | ||
|
||
print("Dev Server: stopped") | ||
|
||
|
||
server = _Server() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
import pytest | ||
|
||
from . import dev_server | ||
from inngest.experimental import dev_server | ||
|
||
pytest.register_assert_rewrite("tests") | ||
|
||
|
||
def pytest_configure(config: pytest.Config) -> None: | ||
dev_server.singleton.start() | ||
dev_server.server.start() | ||
|
||
|
||
def pytest_unconfigure(config: pytest.Config) -> None: | ||
dev_server.singleton.stop() | ||
dev_server.server.stop() |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be good to move away from
npx
for the Python SDK when this comes out of experimental -npm
could be an extra tool folks have to install.Not sure what we've move to, though, outside of managing a
/tmp/
version where we install the binary directly.