Skip to content

Commit

Permalink
feat(execute): added functionality to execute work with the work obje…
Browse files Browse the repository at this point in the history
…ct as arguments
  • Loading branch information
shinybrar committed Jul 4, 2024
1 parent e2f0be0 commit b9927f5
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion workflow/lifecycle/execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import subprocess
import time
from typing import Any, Callable, Dict, List, Optional, Tuple, Union
from typing import Any, Callable, Dict, List, Optional, Tuple, Union, get_type_hints

import click

Expand Down Expand Up @@ -125,3 +125,35 @@ def command(work: Work) -> Work:
work.stop = end
logger.info(f"execution time: {end - start:.2f}s")
return work


def with_work(work: Work) -> Work:
"""Execute a work function with work object.
Args:
work (Work): Work object
Returns:
Work: Work object
"""
logger.debug(f"executing with work: {work.function}")
start = time.time()
try:
assert isinstance(work.function, str), "missing function to execute"
func: Callable[..., Any] = validate.function(work.function)
# Check the type hints of the function,
# check if the input/output is a Work object
hints = get_type_hints(func)
logger.info(f"function hints: {hints}")
assert hints.get("work") == Work, "function must have a Work object as input"
assert hints.get("return") == Work, "function must return a Work object"
work = func(work)
work.status = "success"
except Exception as error:
work.status = "failure"
logger.error(error)
finally:
end = time.time()
work.stop = end
logger.info(f"execution time: {end - start:.2f}s")
return work

0 comments on commit b9927f5

Please sign in to comment.