Skip to content

Commit

Permalink
Merge pull request #283 from vkottler/dev/5.7.7
Browse files Browse the repository at this point in the history
5.7.7 - Minor improvements
  • Loading branch information
vkottler authored Oct 23, 2024
2 parents 0311186 + 5ebbc7d commit 74e6d38
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ jobs:
- run: |
mk python-release owner=vkottler \
repo=runtimepy version=5.7.6
repo=runtimepy version=5.7.7
if: |
matrix.python-version == '3.12'
&& matrix.system == 'ubuntu-latest'
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
=====================================
generator=datazen
version=3.1.4
hash=41ff8742602f69f2831092a4b666cddc
hash=0bf47d2ff3fb0ed78ed013f4fcbc59e2
=====================================
-->

# runtimepy ([5.7.6](https://pypi.org/project/runtimepy/))
# runtimepy ([5.7.7](https://pypi.org/project/runtimepy/))

[![python](https://img.shields.io/pypi/pyversions/runtimepy.svg)](https://pypi.org/project/runtimepy/)
![Build Status](https://github.com/vkottler/runtimepy/workflows/Python%20Package/badge.svg)
Expand Down
2 changes: 1 addition & 1 deletion local/variables/package.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
major: 5
minor: 7
patch: 6
patch: 7
entry: runtimepy
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta:__legacy__"

[project]
name = "runtimepy"
version = "5.7.6"
version = "5.7.7"
description = "A framework for implementing Python services."
readme = "README.md"
requires-python = ">=3.11"
Expand Down
4 changes: 2 additions & 2 deletions runtimepy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# =====================================
# generator=datazen
# version=3.1.4
# hash=cab7cff2cb194a61178fa386b80eae85
# hash=ed12f307211d1317e3ce6a7c4409b97f
# =====================================

"""
Expand All @@ -10,7 +10,7 @@

DESCRIPTION = "A framework for implementing Python services."
PKG_NAME = "runtimepy"
VERSION = "5.7.6"
VERSION = "5.7.7"

# runtimepy-specific content.
METRICS_NAME = "metrics"
Expand Down
2 changes: 1 addition & 1 deletion runtimepy/metrics/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def update(self, other: "ChannelMetrics") -> None:
def __str__(self) -> str:
"""Get metrics as a string."""

return "\t".join(
return " | ".join(
[
f"messages={self.messages.value}",
f"message_rate={self.message_rate.value:.2f}",
Expand Down
6 changes: 5 additions & 1 deletion runtimepy/net/arbiter/housekeeping/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ async def dispatch(self) -> bool:

# Handle any incoming commands.
processors: list[Awaitable[None]] = []
for mapping in self.app.connections.values(), self.app.tasks.values():
for mapping in (
self.app.connections.values(),
self.app.tasks.values(),
self.app.structs.values(),
):
for item in mapping:
if isinstance(item, AsyncCommandProcessingMixin):
processors.append(item.process_command_queue())
Expand Down
20 changes: 20 additions & 0 deletions runtimepy/net/arbiter/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from abc import ABC as _ABC
import asyncio as _asyncio
from contextlib import AsyncExitStack as _AsyncExitStack
from contextlib import contextmanager
from dataclasses import dataclass
from logging import getLogger as _getLogger
from re import compile as _compile
Expand Down Expand Up @@ -57,13 +58,29 @@ class RuntimeStruct(RuntimeStructBase, _ABC):

byte_order: ByteOrder = DEFAULT_BYTE_ORDER

# Set this for structs to automatically be polled when the application is
# going down.
final_poll = False

def init_env(self) -> None:
"""Initialize this sample environment."""

@contextmanager
def _final_poll(self) -> _Iterator[None]:
"""Poll when the context ends."""

try:
yield
finally:
self.poll()

async def build(self, app: "AppInfo", **kwargs) -> None:
"""Build a struct instance's channel environment."""

self.app = app
if self.final_poll:
self.app.stack.enter_context(self._final_poll())

self.init_env()
self.update_byte_order(self.byte_order, **kwargs)

Expand Down Expand Up @@ -102,6 +119,9 @@ def poll(self) -> None:
class SampleStruct(TrigStruct):
"""A sample runtime structure."""

# For fun.
final_poll = True

def init_env(self) -> None:
"""Initialize this sample environment."""
super().init_env()
Expand Down
27 changes: 26 additions & 1 deletion runtimepy/struct/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,31 @@
"""

# built-in
from argparse import Namespace
import asyncio
from logging import getLogger as _getLogger
from typing import Optional

# third-party
from vcorelib.io import MarkdownMixin
from vcorelib.io.types import JsonObject as _JsonObject

# internal
from runtimepy import PKG_NAME
from runtimepy.channel.environment.command import FieldOrChannel
from runtimepy.channel.environment.command.processor import (
ChannelCommandProcessor,
)
from runtimepy.mixins.async_command import AsyncCommandProcessingMixin
from runtimepy.mixins.environment import ChannelEnvironmentMixin
from runtimepy.mixins.logging import LoggerMixinLevelControl


class RuntimeStructBase(
LoggerMixinLevelControl, ChannelEnvironmentMixin, MarkdownMixin
LoggerMixinLevelControl,
ChannelEnvironmentMixin,
AsyncCommandProcessingMixin,
MarkdownMixin,
):
"""A base runtime structure."""

Expand All @@ -42,6 +50,23 @@ def __init__(
self.command = ChannelCommandProcessor(self.env, self.logger)
self.config = config

async def poll(args: Namespace, __: Optional[FieldOrChannel]) -> None:
"""Handle a test command."""

count = 1
delay = 0.0

if args.extra:
count = int(args.extra[0])
if len(args.extra) > 1:
delay = float(args.extra[1])

for _ in range(count):
self.poll()
await asyncio.sleep(delay)

self._setup_async_commands(poll)

def poll(self) -> None:
"""
A method that other runtime entities can call to perform canonical
Expand Down
11 changes: 11 additions & 0 deletions tests/net/server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,17 @@ async def runtimepy_http_client_server(
client.request_json(
RequestHeader(method="POST", target="/sample1/custom/asdf")
),
client.request_json(
RequestHeader(method="POST", target="/struct2/custom/poll")
),
client.request_json(
RequestHeader(method="POST", target="/struct2/custom/poll/5")
),
client.request_json(
RequestHeader(
method="POST", target="/struct2/custom/poll/5/0.01"
)
),
client.request_json(
RequestHeader(
method="POST", target="/sample1/custom/test_command"
Expand Down

0 comments on commit 74e6d38

Please sign in to comment.