-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #106 from conductor-sdk/feature/worker-as-python-c…
…allable [feature] Implement workers as functions
- Loading branch information
Showing
31 changed files
with
352 additions
and
247 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
name: Tests | ||
|
||
on: pull_request | ||
|
||
jobs: | ||
run-tests: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v3 | ||
- name: Set up Python | ||
uses: actions/setup-python@v3 | ||
with: | ||
python-version: "3.x" | ||
- name: Instal package from local files | ||
run: python3 -m pip install . | ||
env: | ||
CONDUCTOR_PYTHON_VERSION: v1.0.0 | ||
- name: Run Unit Tests | ||
run: python3 -m unittest discover --verbose --start-directory=./tests/unit | ||
- name: Run Integration Tests | ||
run: python3 ./tests/main.py | ||
env: | ||
PYTHON_INTEGRATION_TESTS_SERVER_API_URL: https://pg-staging.orkesconductor.com/api | ||
PYTHON_INTEGRATION_TESTS_SERVER_KEY_ID: 88659b04-acab-47a8-a876-2b19619a2587 | ||
PYTHON_INTEGRATION_TESTS_SERVER_KEY_SECRET: 6LSlOPLSh8bQFxR26kjGt6j6OM6zGynlHuQ6N5qzLenmH39i |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from conductor.client.http.models.task import Task | ||
from conductor.client.http.models.task_result import TaskResult | ||
from conductor.client.worker.worker_interface import WorkerInterface | ||
from typing import Any, Callable | ||
from typing_extensions import Self | ||
import inspect | ||
|
||
ExecuteTaskFunction = Callable[[Any], Any] | ||
|
||
|
||
def is_callable_input_parameter_a_task(execute_function: ExecuteTaskFunction) -> bool: | ||
parameters = inspect.signature(execute_function).parameters | ||
if len(parameters) != 1: | ||
return False | ||
parameter = parameters[list(parameters.keys())[0]] | ||
return parameter.annotation == Task | ||
|
||
|
||
def is_callable_return_value_a_task_result(execute_function: ExecuteTaskFunction) -> bool: | ||
return_annotation = inspect.signature(execute_function).return_annotation | ||
return return_annotation == TaskResult | ||
|
||
|
||
class Worker(WorkerInterface): | ||
def __init__(self, task_definition_name: str, execute_function: ExecuteTaskFunction, poll_interval: float) -> Self: | ||
super().__init__(task_definition_name) | ||
self.poll_interval = poll_interval | ||
self.execute_function = execute_function | ||
|
||
def execute(self, task: Task) -> TaskResult: | ||
return self.execute_function(task) | ||
|
||
def get_polling_interval_in_seconds(self) -> float: | ||
return self.poll_interval | ||
|
||
@property | ||
def execute_function(self) -> ExecuteTaskFunction: | ||
return self._execute_function | ||
|
||
@execute_function.setter | ||
def execute_function(self, execute_function: ExecuteTaskFunction) -> None: | ||
self._execute_function = execute_function | ||
self._is_execute_function_input_parameter_a_task = is_callable_input_parameter_a_task( | ||
execute_function | ||
) | ||
self._is_execute_function_return_value_a_task_result = is_callable_return_value_a_task_result( | ||
execute_function | ||
) |
Oops, something went wrong.