Repository navigation
feat: execute registered flash tasks (CON-1833) #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
KAJdev
wants to merge
4
commits into
main
Choose a base branch
from
zeke/con-1833-secp1-high-developer-rw-api-key-injected-into-every-deployed
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,328
−39
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,117 @@ | ||
| """registered task loading and json-only execution.""" | ||
|
|
||
| import asyncio | ||
| import importlib | ||
| import inspect | ||
| import json | ||
| import os | ||
| import sys | ||
| import threading | ||
|
|
||
| from runpod_sdk_runtime import bootstrap | ||
| from runpod_sdk_runtime.executor import _await, execute_request | ||
|
|
||
| _loop = None | ||
| _loop_thread = None | ||
| _loop_lock = threading.Lock() | ||
|
|
||
|
|
||
| def _serve_loop(loop): | ||
| asyncio.set_event_loop(loop) | ||
| try: | ||
| loop.run_forever() | ||
| finally: | ||
| pending = asyncio.all_tasks(loop) | ||
| for task in pending: | ||
| task.cancel() | ||
| if pending: | ||
| loop.run_until_complete(asyncio.gather(*pending, return_exceptions=True)) | ||
| loop.run_until_complete(loop.shutdown_asyncgens()) | ||
| loop.run_until_complete(loop.shutdown_default_executor()) | ||
| loop.close() | ||
|
|
||
|
|
||
| def _run_awaitable(awaitable): | ||
| global _loop, _loop_thread | ||
| with _loop_lock: | ||
| if _loop is None: | ||
| _loop = asyncio.new_event_loop() | ||
| _loop_thread = threading.Thread( | ||
| target=_serve_loop, args=(_loop,), daemon=True | ||
| ) | ||
| _loop_thread.start() | ||
| future = asyncio.run_coroutine_threadsafe(_await(awaitable), _loop) | ||
| return future.result() | ||
|
|
||
|
|
||
| def close(): | ||
| global _loop, _loop_thread | ||
| with _loop_lock: | ||
| if _loop is None: | ||
| return | ||
| _loop.call_soon_threadsafe(_loop.stop) | ||
| _loop_thread.join() | ||
| _loop = None | ||
| _loop_thread = None | ||
|
|
||
|
|
||
| async def _initialize(handle): | ||
| result = handle._init_fn() | ||
| if inspect.isawaitable(result): | ||
| await result | ||
|
|
||
|
|
||
| def load_handle(): | ||
| bootstrap._validate_task_environment() | ||
| app_dir = os.environ.get("RUNPOD_APP_DIR", "/app") | ||
| entry = bootstrap._task_resource(bootstrap._manifest(app_dir)) | ||
| if app_dir not in sys.path: | ||
| sys.path.insert(0, app_dir) | ||
| module = importlib.import_module(entry["module"]) | ||
|
|
||
| from runpod.apps.handles import FunctionHandle | ||
| from runpod.apps.spec import ResourceKind | ||
|
|
||
| target = module | ||
| for name in entry["qualname"].split("."): | ||
| target = getattr(target, name, None) | ||
| handles = { | ||
| value | ||
| for value in [target, *vars(module).values()] | ||
| if isinstance(value, FunctionHandle) | ||
| and value.spec.name == entry["name"] | ||
| and value.spec.kind == ResourceKind.TASK | ||
| and value._app.name == os.environ["FLASH_APP_NAME"] | ||
| and value._fn.__qualname__ == entry["qualname"] | ||
| } | ||
| if len(handles) != 1: | ||
| raise RuntimeError("module must expose exactly one matching @app.task handle") | ||
| handle = handles.pop() | ||
| if handle._init_fn is not None: | ||
| _run_awaitable(_initialize(handle)) | ||
| return handle | ||
|
|
||
|
|
||
| def validate_request(request, *, timeout_only=False): | ||
| allowed = ( | ||
| {"timeout"} | ||
| if timeout_only | ||
| else {"args", "kwargs", "serialization_format", "timeout"} | ||
| ) | ||
| if request.keys() - allowed: | ||
| raise ValueError("deployed tasks accept only json args, kwargs and timeout") | ||
| if request.get("serialization_format", "json") != "json": | ||
| raise ValueError("deployed tasks require json serialization") | ||
| if not isinstance(request.get("args", []), list): | ||
| raise ValueError("args must be a json array") | ||
| if not isinstance(request.get("kwargs", {}), dict): | ||
| raise ValueError("kwargs must be a json object") | ||
| json.dumps(request, allow_nan=False) | ||
|
|
||
|
|
||
| def execute(handle, request): | ||
| return execute_request( | ||
| {"serialization_format": "json"}, | ||
| prepared=(handle._fn, request.get("args", []), request.get("kwargs", {})), | ||
| run_awaitable=_run_awaitable, | ||
| ) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
flashTaskCreateresolvesenvironment.activeBuildIdand approves the task's image, env, compute and timeout from that build'sresourceSpec, and the host then delivers "the environment's active artifact" at boot. If a deploy flipsactiveBuildIdbetween create and boot, the pod can run the new build's code under the old build's policy. The runner checks app and resource name but nothing compares the artifact toRUNPOD_BUILD_ID, so it only has to be non-empty. Is the host pinning delivery to the pod's build? If not, it might be worth having the manifest carry its build id (or the host pass the expected artifact hash) so_task_resourcecan reject a mismatch.