Skip to content

Commit

Permalink
Improve timer commands and checking types
Browse files Browse the repository at this point in the history
  • Loading branch information
longquanzheng committed Oct 30, 2024
1 parent c8cc65c commit cbf8629
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 26 deletions.
41 changes: 18 additions & 23 deletions iwf/command_request.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from dataclasses import dataclass
from datetime import timedelta
from typing import Optional, Union

from iwf.errors import WorkflowDefinitionError
from iwf.iwf_api.models import CommandWaitingType
from iwf.iwf_api.models.command_request import (
CommandRequest as IdlCommandRequest,
Expand All @@ -19,12 +19,10 @@ class TimerCommand:
duration_seconds: int

@classmethod
def timer_command_by_duration(
cls, duration: timedelta, command_id: Optional[str] = None
):
def by_seconds(cls, duration_seconds: int, command_id: Optional[str] = None):
return TimerCommand(
command_id if command_id is not None else "",
int(duration.total_seconds()),
duration_seconds,
)


Expand Down Expand Up @@ -52,7 +50,7 @@ def by_name(cls, channel_name: str, command_id: Optional[str] = None):
)


BaseCommand = Union[TimerCommand, InternalChannelCommand]
BaseCommand = Union[TimerCommand, InternalChannelCommand, SignalChannelCommand]


@dataclass
Expand Down Expand Up @@ -80,23 +78,20 @@ def _to_idl_command_request(request: CommandRequest) -> IdlCommandRequest:
command_waiting_type=request.command_waiting_type,
)

timer_commands = [
IdlTimerCommand(t.duration_seconds, t.command_id)
for t in request.commands
if isinstance(t, TimerCommand)
]

internal_channel_commands = [
IdlInternalChannelCommand(i.channel_name, i.command_id)
for i in request.commands
if isinstance(i, InternalChannelCommand)
]

signal_commands = [
IdlSignalCommand(i.channel_name, i.command_id)
for i in request.commands
if isinstance(i, SignalChannelCommand)
]
timer_commands = []
internal_channel_commands = []
signal_commands = []
for t in request.commands:
if isinstance(t, TimerCommand):
timer_commands.append(IdlTimerCommand(t.duration_seconds, t.command_id))
elif isinstance(t, InternalChannelCommand):
internal_channel_commands.append(
IdlInternalChannelCommand(t.channel_name, t.command_id)
)
elif isinstance(t, SignalChannelCommand):
signal_commands.append(IdlSignalCommand(t.channel_name, t.command_id))
else:
raise WorkflowDefinitionError(f"unknown command {t.__class__.__name__}")

if len(timer_commands) > 0:
req.timer_commands = timer_commands
Expand Down
5 changes: 2 additions & 3 deletions iwf/tests/test_timer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import inspect
import time
from datetime import timedelta

from iwf.client import Client
from iwf.command_request import CommandRequest, TimerCommand
Expand All @@ -24,8 +23,8 @@ def wait_until(
communication: Communication,
) -> CommandRequest:
return CommandRequest.for_all_command_completed(
TimerCommand.timer_command_by_duration(timedelta(hours=input)),
TimerCommand.timer_command_by_duration(timedelta(seconds=input)),
TimerCommand.by_seconds(input * 3600),
TimerCommand.by_seconds(input),
)

def execute(
Expand Down
2 changes: 2 additions & 0 deletions requirements.frozen
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
python-dateutil==2.9.0.post0 ; python_version >= "3.9" and python_version < "3.11"
six==1.16.0 ; python_version >= "3.9" and python_version < "3.11"
10 changes: 10 additions & 0 deletions requirements.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
blinker==1.8.2 ; python_version >= "3.9" and python_version < "4.0"
click==8.1.7 ; python_version >= "3.9" and python_version < "4.0"
colorama==0.4.6 ; python_version >= "3.9" and python_version < "4.0" and platform_system == "Windows"
flask==2.3.3 ; python_version >= "3.9" and python_version < "4.0"
importlib-metadata==8.5.0 ; python_version >= "3.9" and python_version < "3.10"
itsdangerous==2.2.0 ; python_version >= "3.9" and python_version < "4.0"
jinja2==3.1.4 ; python_version >= "3.9" and python_version < "4.0"
markupsafe==2.1.5 ; python_version >= "3.9" and python_version < "4.0"
werkzeug==3.0.4 ; python_version >= "3.9" and python_version < "4.0"
zipp==3.20.2 ; python_version >= "3.9" and python_version < "3.10"

0 comments on commit cbf8629

Please sign in to comment.