Skip to content

Commit

Permalink
Release/0.0.115 (#55)
Browse files Browse the repository at this point in the history
* introduce should_render

* introduce recurring task
  • Loading branch information
goFrendiAsgard authored Nov 28, 2023
1 parent b4ed9f0 commit 120d7bd
Show file tree
Hide file tree
Showing 24 changed files with 628 additions and 354 deletions.
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 = "flit_core.buildapi"

[project]
name = "zrb"
version = "0.0.114"
version = "0.0.115"
authors = [
{ name="Go Frendi Gunawan", email="gofrendiasgard@gmail.com" },
]
Expand Down
9 changes: 8 additions & 1 deletion src/zrb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@
from zrb.task.base_remote_cmd_task import BaseRemoteCmdTask, RemoteConfig
from zrb.task.remote_cmd_task import RemoteCmdTask
from zrb.task.rsync_task import RsyncTask
from zrb.task.checker import Checker
from zrb.task.http_checker import HTTPChecker
from zrb.task.port_checker import PortChecker
from zrb.task.path_checker import PathChecker
from zrb.task.path_watcher import PathWatcher
from zrb.task.time_watcher import TimeWatcher
from zrb.task.resource_maker import (
ResourceMaker, Replacement, ReplacementMutator
)
from zrb.task.flow_task import FlowTask
from zrb.task.triggered_task import TriggeredTask
from zrb.task.recurring_task import RecurringTask
from zrb.task_input.any_input import AnyInput
from zrb.task_input.task_input import Input
from zrb.task_input.bool_input import BoolInput
Expand All @@ -39,11 +42,15 @@
assert RemoteConfig
assert RemoteCmdTask
assert RsyncTask
assert Checker
assert HTTPChecker
assert PortChecker
assert PathChecker
assert PathWatcher
assert TimeWatcher
assert ResourceMaker
assert FlowTask
assert RecurringTask
assert Replacement
assert ReplacementMutator
assert AnyInput
Expand Down
9 changes: 6 additions & 3 deletions src/zrb/builtin/schedule.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from zrb.task.triggered_task import TriggeredTask
from zrb.task.recurring_task import RecurringTask
from zrb.task.cmd_task import CmdTask
from zrb.task.time_watcher import TimeWatcher
from zrb.task_input.str_input import StrInput
from zrb.runner import runner


schedule = TriggeredTask(
schedule = RecurringTask(
name='schedule',
description='Show message/run command periodically',
inputs=[
Expand All @@ -15,7 +16,9 @@
description='Schedule cron pattern to show the message'
),
],
schedule='{{input.schedule}}',
triggers=[
TimeWatcher(name='watch-schedule', schedule='{{input.schedule}}')
],
task=CmdTask(
name='run-task',
inputs=[
Expand Down
9 changes: 6 additions & 3 deletions src/zrb/builtin/watch.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from zrb.task.triggered_task import TriggeredTask
from zrb.task.recurring_task import RecurringTask
from zrb.task.cmd_task import CmdTask
from zrb.task.path_watcher import PathWatcher
from zrb.task_input.str_input import StrInput
from zrb.runner import runner


watch = TriggeredTask(
watch = RecurringTask(
name='watch',
description='Watch changes and show message/run command',
inputs=[
Expand All @@ -15,7 +16,9 @@
description='File pattern to be watched'
),
],
watched_path='{{input.pattern}}',
triggers=[
PathWatcher(name='watch-path', path='{{input.pattern}}')
],
task=CmdTask(
name='run-task',
inputs=[
Expand Down
11 changes: 5 additions & 6 deletions src/zrb/task/base_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,18 +253,17 @@ async def _set_local_keyval(
self.log_info('Set input map')
for task_input in self._get_combined_inputs():
input_name = self._get_normalized_input_key(task_input.get_name())
input_value = self.render_any(
kwargs.get(input_name, task_input.get_default())
)
input_value = kwargs.get(input_name, task_input.get_default())
if task_input.should_render():
input_value = self.render_any(input_value)
self._set_input_map(input_name, input_value)
self._set_input_map('_execution_id', self._execution_id)
self.log_debug(
'Input map:\n' + map_to_str(self.get_input_map(), item_prefix=' ')
)
self.log_info('Merging task envs, task env files, and native envs')
for env_name, env in self._get_combined_env().items():
env_value = env.get(env_prefix)
if env.renderable:
if env.should_render:
env_value = self.render_any(env_value)
self._set_env_map(env_name, env_value)
self._set_env_map('_ZRB_EXECUTION_ID', self._execution_id)
Expand All @@ -278,7 +277,7 @@ def _get_combined_env(self) -> Mapping[str, Env]:
if env_name in RESERVED_ENV_NAMES:
continue
all_envs[env_name] = Env(
name=env_name, os_name=env_name, renderable=False
name=env_name, os_name=env_name, should_render=False
)
for env_file in self._get_env_files():
for env in env_file.get_envs():
Expand Down
89 changes: 89 additions & 0 deletions src/zrb/task/checker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
from zrb.helper.typing import Any, Callable, Iterable, Optional, Union
from zrb.helper.typecheck import typechecked
from zrb.task.base_task import BaseTask
from zrb.task.any_task import AnyTask
from zrb.task.any_task_event_handler import (
OnTriggered, OnWaiting, OnSkipped, OnStarted, OnReady, OnRetry, OnFailed
)
from zrb.task_env.env import Env
from zrb.task_env.env_file import EnvFile
from zrb.task_group.group import Group
from zrb.task_input.any_input import AnyInput

import asyncio


@typechecked
class Checker(BaseTask):

def __init__(
self,
name: str = 'port-check',
group: Optional[Group] = None,
inputs: Iterable[AnyInput] = [],
envs: Iterable[Env] = [],
env_files: Iterable[EnvFile] = [],
icon: Optional[str] = None,
color: Optional[str] = None,
description: str = '',
upstreams: Iterable[AnyTask] = [],
on_triggered: Optional[OnTriggered] = None,
on_waiting: Optional[OnWaiting] = None,
on_skipped: Optional[OnSkipped] = None,
on_started: Optional[OnStarted] = None,
on_ready: Optional[OnReady] = None,
on_retry: Optional[OnRetry] = None,
on_failed: Optional[OnFailed] = None,
checking_interval: Union[int, float] = 0.1,
progress_interval: Union[int, float] = 30,
expected_result: bool = True,
should_execute: Union[bool, str, Callable[..., bool]] = True
):
BaseTask.__init__(
self,
name=name,
group=group,
inputs=inputs,
envs=envs,
env_files=env_files,
icon=icon,
color=color,
description=description,
upstreams=upstreams,
on_triggered=on_triggered,
on_waiting=on_waiting,
on_skipped=on_skipped,
on_started=on_started,
on_ready=on_ready,
on_retry=on_retry,
on_failed=on_failed,
checkers=[],
checking_interval=checking_interval,
retry=0,
retry_interval=0,
should_execute=should_execute,
)
self._progress_interval = progress_interval
self._expected_result = expected_result
self._should_show_progress = False

async def run(self, *args: Any, **kwargs: Any) -> bool:
wait_time = 0
while True:
self._should_show_progress = wait_time >= self._progress_interval
inspect_result = await self.inspect(*args, **kwargs)
if inspect_result == self._expected_result:
return True
if wait_time >= self._progress_interval:
wait_time = 0
await asyncio.sleep(self._checking_interval)
wait_time += self._checking_interval

async def inspect(self, *args: Any, **kwargs: Any) -> bool:
return False

def show_progress(self, message: str):
if self._should_show_progress:
self.print_out_dark(message)
return
self.log_debug(message)
Loading

0 comments on commit 120d7bd

Please sign in to comment.