Skip to content

Commit 5ebbc7d

Browse files
committed
Add more features to struct poll command
1 parent b1bab59 commit 5ebbc7d

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

runtimepy/net/arbiter/housekeeping/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@ async def dispatch(self) -> bool:
5252

5353
# Handle any incoming commands.
5454
processors: list[Awaitable[None]] = []
55-
for mapping in self.app.connections.values(), self.app.tasks.values():
55+
for mapping in (
56+
self.app.connections.values(),
57+
self.app.tasks.values(),
58+
self.app.structs.values(),
59+
):
5660
for item in mapping:
5761
if isinstance(item, AsyncCommandProcessingMixin):
5862
processors.append(item.process_command_queue())

runtimepy/struct/__init__.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,31 @@
33
"""
44

55
# built-in
6+
from argparse import Namespace
7+
import asyncio
68
from logging import getLogger as _getLogger
9+
from typing import Optional
710

811
# third-party
912
from vcorelib.io import MarkdownMixin
1013
from vcorelib.io.types import JsonObject as _JsonObject
1114

1215
# internal
1316
from runtimepy import PKG_NAME
17+
from runtimepy.channel.environment.command import FieldOrChannel
1418
from runtimepy.channel.environment.command.processor import (
1519
ChannelCommandProcessor,
1620
)
21+
from runtimepy.mixins.async_command import AsyncCommandProcessingMixin
1722
from runtimepy.mixins.environment import ChannelEnvironmentMixin
1823
from runtimepy.mixins.logging import LoggerMixinLevelControl
1924

2025

2126
class RuntimeStructBase(
22-
LoggerMixinLevelControl, ChannelEnvironmentMixin, MarkdownMixin
27+
LoggerMixinLevelControl,
28+
ChannelEnvironmentMixin,
29+
AsyncCommandProcessingMixin,
30+
MarkdownMixin,
2331
):
2432
"""A base runtime structure."""
2533

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

53+
async def poll(args: Namespace, __: Optional[FieldOrChannel]) -> None:
54+
"""Handle a test command."""
55+
56+
count = 1
57+
delay = 0.0
58+
59+
if args.extra:
60+
count = int(args.extra[0])
61+
if len(args.extra) > 1:
62+
delay = float(args.extra[1])
63+
64+
for _ in range(count):
65+
self.poll()
66+
await asyncio.sleep(delay)
67+
68+
self._setup_async_commands(poll)
69+
4570
def poll(self) -> None:
4671
"""
4772
A method that other runtime entities can call to perform canonical

tests/net/server/__init__.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,17 @@ async def runtimepy_http_client_server(
139139
client.request_json(
140140
RequestHeader(method="POST", target="/sample1/custom/asdf")
141141
),
142+
client.request_json(
143+
RequestHeader(method="POST", target="/struct2/custom/poll")
144+
),
145+
client.request_json(
146+
RequestHeader(method="POST", target="/struct2/custom/poll/5")
147+
),
148+
client.request_json(
149+
RequestHeader(
150+
method="POST", target="/struct2/custom/poll/5/0.01"
151+
)
152+
),
142153
client.request_json(
143154
RequestHeader(
144155
method="POST", target="/sample1/custom/test_command"

0 commit comments

Comments
 (0)