-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Neng Wan
committed
Oct 8, 2024
1 parent
de92a75
commit fc92dba
Showing
6 changed files
with
111 additions
and
4 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
[pytest] | ||
addopts = --strict-markers -vvl --cov=sea --cov-report=term-missing --cov-fail-under=85 | ||
markers = | ||
asyncio: mark a test as an asyncio test. |
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
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,60 @@ | ||
import signal | ||
import asyncio | ||
from concurrent import futures | ||
|
||
import grpc | ||
from grpc_reflection.v1alpha import reflection | ||
|
||
from sea import signals | ||
|
||
|
||
class Server: | ||
"""sea server implements | ||
:param app: application instance | ||
""" | ||
|
||
def __init__(self, app): | ||
self.app = app | ||
self.workers = self.app.config["GRPC_WORKERS"] | ||
self.host = self.app.config["GRPC_HOST"] | ||
self.port = self.app.config["GRPC_PORT"] | ||
self.server = grpc.aio.server(futures.ThreadPoolExecutor(max_workers=self.workers)) | ||
self.server.add_insecure_port("{}:{}".format(self.host, self.port)) | ||
self._stopped = False | ||
|
||
async def run(self): | ||
self.app.logger.warning("Starting server...") | ||
# run prometheus client | ||
if self.app.config["PROMETHEUS_SCRAPE"]: | ||
from prometheus_client import start_http_server | ||
|
||
start_http_server(self.app.config["PROMETHEUS_PORT"]) | ||
# register reflection service | ||
if self.app.config.get("GRPC_REFLECTION_SERVICES"): | ||
reflection.enable_server_reflection((reflection.SERVICE_NAME, *self.app.config["GRPC_REFLECTION_SERVICES"]), self.server) | ||
# run grpc server | ||
for _, (add_func, servicer) in self.app.servicers.items(): | ||
add_func(servicer(), self.server) | ||
await self.server.start() | ||
signals.server_started.send(self) | ||
self.register_signal() | ||
|
||
await self.server.wait_for_termination() | ||
# while not self._stopped: | ||
# await asyncio.sleep(1) | ||
# signals.server_stopped.send(self) | ||
# return True | ||
|
||
def register_signal(self): | ||
signal.signal(signal.SIGINT, self._stop_handler) | ||
signal.signal(signal.SIGHUP, self._stop_handler) | ||
signal.signal(signal.SIGTERM, self._stop_handler) | ||
signal.signal(signal.SIGQUIT, self._stop_handler) | ||
|
||
async def _stop_handler(self): | ||
self.app.logger.warning("Stopping server...") | ||
grace = self.app.config["GRPC_GRACE"] | ||
await self.server.stop(grace) | ||
await asyncio.sleep(grace or 1) | ||
self._stopped = True |
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 |
---|---|---|
|
@@ -13,3 +13,4 @@ prometheus_client | |
versioneer | ||
pre-commit | ||
gitlint | ||
pytest-asyncio |
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