Skip to content

Commit

Permalink
feat: upgrade to ten runtime 0.3 (#5)
Browse files Browse the repository at this point in the history
* feat: upgrade ten 0.3

* feat: use ten's log

* feat: upgrade base image

* feat: upgrade version to 0.6.0
  • Loading branch information
wangyoucao577 authored Oct 28, 2024
1 parent a136539 commit 58f6a89
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 40 deletions.
3 changes: 1 addition & 2 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// README at: https://github.com/devcontainers/templates/tree/main/src/docker-existing-dockerfile
{
"name": "dev",
"image": "ghcr.io/ten-framework/ten_agent_build:0.1.0",
"image": "ghcr.io/ten-framework/ten_agent_build:0.2.1",
"customizations": {
"vscode": {
"extensions": [
Expand All @@ -13,7 +13,6 @@
]
}
},

// Features to add to the dev container. More info: https://containers.dev/features.
"features": {
"ghcr.io/devcontainers/features/git:1": {}
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
ci:
runs-on: ubuntu-latest
container:
image: ghcr.io/ten-framework/ten_agent_build:0.1.0
image: ghcr.io/ten-framework/ten_agent_build:0.2.1
steps:
- uses: actions/checkout@v4
with:
Expand All @@ -45,6 +45,6 @@ jobs:
diff manifest.gen.json manifest.json || true
mv manifest.gen.json manifest.json
jq . manifest.json
# publish to store
tman --verbose --user-token ${{ secrets.TEN_CLOUD_STORE }} publish
3 changes: 0 additions & 3 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
from . import http_server_addon
from .log import logger

logger.info("http_server_python extension loaded")
2 changes: 0 additions & 2 deletions http_server_addon.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
register_addon_as_extension,
TenEnv,
)
from .log import logger
from .http_server_extension import HTTPServerExtension


@register_addon_as_extension("http_server_python")
class HTTPServerExtensionAddon(Addon):
def on_create_instance(self, ten: TenEnv, addon_name: str, context):
logger.info("on_create_instance")
ten.on_create_instance_done(HTTPServerExtension(addon_name), context)
26 changes: 10 additions & 16 deletions http_server_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,24 @@
StatusCode,
CmdResult,
)
from .log import logger
from http.server import HTTPServer, BaseHTTPRequestHandler
import threading
from functools import partial


class HTTPHandler(BaseHTTPRequestHandler):
def __init__(self, ten: TenEnv, *args, directory=None, **kwargs):
logger.info("new handler: %s %s %s", directory, args, kwargs)
ten.log_debug(f"new handler: {directory} {args} {kwargs}")
self.ten = ten
super().__init__(*args, **kwargs)

def do_POST(self):
logger.info("post request incoming %s", self.path)
self.ten.log_debug(f"post request incoming {self.path}")
if self.path == "/cmd":
try:
content_length = int(self.headers["Content-Length"])
input = self.rfile.read(content_length).decode("utf-8")
logger.info("incoming request %s", input)
self.ten.log_info(f"incoming request {input}")

# processing by send_cmd
cmd_result_event = threading.Event()
Expand All @@ -32,7 +31,7 @@ def cmd_callback(_, result):
nonlocal cmd_result_event
nonlocal cmd_result
cmd_result = result
logger.info("cmd callback result: {}".format(cmd_result.to_json()))
self.ten.log_info("cmd callback result: {}".format(cmd_result.to_json()))
cmd_result_event.set()

self.ten.send_cmd(Cmd.create_from_json(input),cmd_callback)
Expand All @@ -48,11 +47,11 @@ def cmd_callback(_, result):
self.end_headers()
self.wfile.write(cmd_result.to_json().encode(encoding='utf_8'))
except Exception as e:
logger.warning("failed to handle request, err {}".format(e))
self.ten.log_warn("failed to handle request, err {}".format(e))
self.send_response_only(500)
self.end_headers()
else:
logger.warning("invalid path: %s", self.path)
self.ten.log_warn(f"invalid path: {self.path}")
self.send_response_only(404)
self.end_headers()

Expand All @@ -75,11 +74,8 @@ def on_start(self, ten: TenEnv):
self.cmd_white_list = white_list.split(",")
"""

logger.info(
"HTTPServerExtension on_start %s:%d, %s",
self.listen_addr,
self.listen_port,
self.cmd_white_list,
ten.log_info(
f"on_start {self.listen_addr}:{self.listen_port}, {self.cmd_white_list}"
)

self.server = HTTPServer(
Expand All @@ -91,14 +87,12 @@ def on_start(self, ten: TenEnv):
ten.on_start_done()

def on_stop(self, ten: TenEnv):
logger.info("on_stop")
self.server.shutdown()
self.thread.join()
ten.on_stop_done()

def on_cmd(self, ten: TenEnv, cmd: Cmd):
cmd_json = cmd.to_json()
logger.info("on_cmd json: " + cmd_json)
cmd_name = cmd.get_name()
ten.log_info("on_cmd {cmd_name}")
cmd_result = CmdResult.create(StatusCode.OK)
cmd_result.set_property_string("detail", "ok")
ten.return_result(cmd_result, cmd)
13 changes: 0 additions & 13 deletions log.py

This file was deleted.

4 changes: 2 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"type": "extension",
"name": "http_server_python",
"version": "0.5.2-rc1",
"version": "0.6.0",
"dependencies": [
{
"type": "system",
"name": "ten_runtime_python",
"version": "0.2"
"version": "0.3"
}
],
"package": {
Expand Down

0 comments on commit 58f6a89

Please sign in to comment.