|
| 1 | +""" Core functionality for the runpod serverless worker. """ |
| 2 | + |
| 3 | +import ctypes |
| 4 | +import inspect |
| 5 | +import json |
| 6 | +import os |
| 7 | +import pathlib |
| 8 | +import asyncio |
| 9 | +from ctypes import CDLL, byref, c_char_p, c_int |
| 10 | +from typing import Any, Callable, List, Dict, Optional |
| 11 | + |
| 12 | +from runpod.serverless.modules.rp_logger import RunPodLogger |
| 13 | + |
| 14 | + |
| 15 | +log = RunPodLogger() |
| 16 | + |
| 17 | + |
| 18 | +class CGetJobResult(ctypes.Structure): # pylint: disable=too-few-public-methods |
| 19 | + """ |
| 20 | + result of _runpod_sls_get_jobs. |
| 21 | + ## fields |
| 22 | + - `res_len` the number bytes were written to the `dst_buf` passed to _runpod_sls_get_jobs. |
| 23 | + - `status_code` tells you what happened. |
| 24 | + see CGetJobResult.status_code for more information. |
| 25 | + """ |
| 26 | + |
| 27 | + _fields_ = [("status_code", ctypes.c_int), ("res_len", ctypes.c_int)] |
| 28 | + |
| 29 | + def __str__(self) -> str: |
| 30 | + return f"CGetJobResult(res_len={self.res_len}, status_code={self.status_code})" |
| 31 | + |
| 32 | + |
| 33 | +class Hook: # pylint: disable=too-many-instance-attributes |
| 34 | + """ Singleton class for interacting with sls_core.so""" |
| 35 | + |
| 36 | + _instance = None |
| 37 | + |
| 38 | + # C function pointers |
| 39 | + _get_jobs: Callable = None |
| 40 | + _progress_update: Callable = None |
| 41 | + _stream_output: Callable = None |
| 42 | + _post_output: Callable = None |
| 43 | + _finish_stream: Callable = None |
| 44 | + |
| 45 | + def __new__(cls): |
| 46 | + if Hook._instance is None: |
| 47 | + Hook._instance = object.__new__(cls) |
| 48 | + Hook._initialized = False |
| 49 | + return Hook._instance |
| 50 | + |
| 51 | + def __init__(self, rust_so_path: Optional[str] = None) -> None: |
| 52 | + if self._initialized: |
| 53 | + return |
| 54 | + |
| 55 | + if rust_so_path is None: |
| 56 | + default_path = os.path.join( |
| 57 | + pathlib.Path(__file__).parent.absolute(), "sls_core.so" |
| 58 | + ) |
| 59 | + self.rust_so_path = os.environ.get("RUNPOD_SLS_CORE_PATH", str(default_path)) |
| 60 | + else: |
| 61 | + self.rust_so_path = rust_so_path |
| 62 | + |
| 63 | + rust_library = CDLL(self.rust_so_path) |
| 64 | + buffer = ctypes.create_string_buffer(1024) # 1 KiB |
| 65 | + num_bytes = rust_library._runpod_sls_crate_version(byref(buffer), c_int(len(buffer))) |
| 66 | + |
| 67 | + self.rust_crate_version = buffer.raw[:num_bytes].decode("utf-8") |
| 68 | + |
| 69 | + # Get Jobs |
| 70 | + self._get_jobs = rust_library._runpod_sls_get_jobs |
| 71 | + self._get_jobs.restype = CGetJobResult |
| 72 | + |
| 73 | + # Progress Update |
| 74 | + self._progress_update = rust_library._runpod_sls_progress_update |
| 75 | + self._progress_update.argtypes = [ |
| 76 | + c_char_p, c_int, # id_ptr, id_len |
| 77 | + c_char_p, c_int # json_ptr, json_len |
| 78 | + ] |
| 79 | + self._progress_update.restype = c_int # 1 if success, 0 if failure |
| 80 | + |
| 81 | + # Stream Output |
| 82 | + self._stream_output = rust_library._runpod_sls_stream_output |
| 83 | + self._stream_output.argtypes = [ |
| 84 | + c_char_p, c_int, # id_ptr, id_len |
| 85 | + c_char_p, c_int, # json_ptr, json_len |
| 86 | + ] |
| 87 | + self._stream_output.restype = c_int # 1 if success, 0 if failure |
| 88 | + |
| 89 | + # Post Output |
| 90 | + self._post_output = rust_library._runpod_sls_post_output |
| 91 | + self._post_output.argtypes = [ |
| 92 | + c_char_p, c_int, # id_ptr, id_len |
| 93 | + c_char_p, c_int, # json_ptr, json_len |
| 94 | + ] |
| 95 | + self._post_output.restype = c_int # 1 if success, 0 if failure |
| 96 | + |
| 97 | + # Finish Stream |
| 98 | + self._finish_stream = rust_library._runpod_sls_finish_stream |
| 99 | + self._finish_stream.argtypes = [c_char_p, c_int] # id_ptr, id_len |
| 100 | + self._finish_stream.restype = c_int # 1 if success, 0 if failure |
| 101 | + |
| 102 | + rust_library._runpod_sls_crate_version.restype = c_int |
| 103 | + |
| 104 | + rust_library._runpod_sls_init.argtypes = [] |
| 105 | + rust_library._runpod_sls_init.restype = c_int |
| 106 | + rust_library._runpod_sls_init() |
| 107 | + |
| 108 | + self._initialized = True |
| 109 | + |
| 110 | + def _json_serialize_job_data(self, job_data: Any) -> bytes: |
| 111 | + return json.dumps(job_data, ensure_ascii=False).encode("utf-8") |
| 112 | + |
| 113 | + def get_jobs(self, max_concurrency: int, max_jobs: int) -> List[Dict[str, Any]]: |
| 114 | + """Get a job or jobs from the queue. The jobs are returned as a list of Job objects.""" |
| 115 | + buffer = ctypes.create_string_buffer(1024 * 1024 * 20) # 20MB buffer to store jobs in |
| 116 | + destination_length = len(buffer.raw) |
| 117 | + result: CGetJobResult = self._get_jobs( |
| 118 | + c_int(max_concurrency), c_int(max_jobs), |
| 119 | + byref(buffer), c_int(destination_length) |
| 120 | + ) |
| 121 | + if result.status_code == 1: # success! the job was stored bytes 0..res_len of buf.raw |
| 122 | + return list(json.loads(buffer.raw[: result.res_len].decode("utf-8"))) |
| 123 | + |
| 124 | + if result.status_code not in [0, 1]: |
| 125 | + raise RuntimeError(f"get_jobs failed with status code {result.status_code}") |
| 126 | + |
| 127 | + return [] # Status code 0, still waiting for jobs |
| 128 | + |
| 129 | + def progress_update(self, job_id: str, json_data: bytes) -> bool: |
| 130 | + """ |
| 131 | + send a progress update to AI-API. |
| 132 | + """ |
| 133 | + id_bytes = job_id.encode("utf-8") |
| 134 | + return bool(self._progress_update( |
| 135 | + c_char_p(id_bytes), c_int(len(id_bytes)), |
| 136 | + c_char_p(json_data), c_int(len(json_data)) |
| 137 | + )) |
| 138 | + |
| 139 | + def stream_output(self, job_id: str, job_output: bytes) -> bool: |
| 140 | + """ |
| 141 | + send part of a streaming result to AI-API. |
| 142 | + """ |
| 143 | + json_data = self._json_serialize_job_data(job_output) |
| 144 | + id_bytes = job_id.encode("utf-8") |
| 145 | + return bool(self._stream_output( |
| 146 | + c_char_p(id_bytes), c_int(len(id_bytes)), |
| 147 | + c_char_p(json_data), c_int(len(json_data)) |
| 148 | + )) |
| 149 | + |
| 150 | + def post_output(self, job_id: str, job_output: bytes) -> bool: |
| 151 | + """ |
| 152 | + send the result of a job to AI-API. |
| 153 | + Returns True if the task was successfully stored, False otherwise. |
| 154 | + """ |
| 155 | + json_data = self._json_serialize_job_data(job_output) |
| 156 | + id_bytes = job_id.encode("utf-8") |
| 157 | + return bool(self._post_output( |
| 158 | + c_char_p(id_bytes), c_int(len(id_bytes)), |
| 159 | + c_char_p(json_data), c_int(len(json_data)) |
| 160 | + )) |
| 161 | + |
| 162 | + def finish_stream(self, job_id: str) -> bool: |
| 163 | + """ |
| 164 | + tell the SLS queue that the result of a streaming job is complete. |
| 165 | + """ |
| 166 | + id_bytes = job_id.encode("utf-8") |
| 167 | + return bool(self._finish_stream( |
| 168 | + c_char_p(id_bytes), c_int(len(id_bytes)) |
| 169 | + )) |
| 170 | + |
| 171 | + |
| 172 | +# -------------------------------- Process Job ------------------------------- # |
| 173 | +def _process_job(handler: Callable, job: Dict[str, Any]) -> Dict[str, Any]: |
| 174 | + """ Process a single job. """ |
| 175 | + hook = Hook() |
| 176 | + |
| 177 | + try: |
| 178 | + result = handler(job) |
| 179 | + except Exception as err: |
| 180 | + raise RuntimeError( |
| 181 | + f"run {job['id']}: user code raised an {type(err).__name__}") from err |
| 182 | + |
| 183 | + if inspect.isgeneratorfunction(handler): |
| 184 | + for part in result: |
| 185 | + hook.stream_output(job['id'], part) |
| 186 | + |
| 187 | + hook.finish_stream(job['id']) |
| 188 | + |
| 189 | + else: |
| 190 | + hook.post_output(job['id'], result) |
| 191 | + |
| 192 | + |
| 193 | +# -------------------------------- Run Worker -------------------------------- # |
| 194 | +async def run(config: Dict[str, Any]) -> None: |
| 195 | + """ Run the worker. |
| 196 | +
|
| 197 | + Args: |
| 198 | + config: A dictionary containing the following keys: |
| 199 | + handler: A function that takes a job and returns a result. |
| 200 | + """ |
| 201 | + handler = config['handler'] |
| 202 | + max_concurrency = config.get('max_concurrency', 4) |
| 203 | + max_jobs = config.get('max_jobs', 4) |
| 204 | + |
| 205 | + hook = Hook() |
| 206 | + |
| 207 | + while True: |
| 208 | + jobs = hook.get_jobs(max_concurrency, max_jobs) |
| 209 | + |
| 210 | + if len(jobs) == 0: |
| 211 | + continue |
| 212 | + |
| 213 | + for job in jobs: |
| 214 | + asyncio.create_task(_process_job(handler, job)) |
| 215 | + await asyncio.sleep(0) |
| 216 | + |
| 217 | + await asyncio.sleep(0) |
| 218 | + |
| 219 | + |
| 220 | +def main(config: Dict[str, Any]) -> None: |
| 221 | + """Run the worker in an asyncio event loop.""" |
| 222 | + try: |
| 223 | + work_loop = asyncio.new_event_loop() |
| 224 | + asyncio.ensure_future(run(config), loop=work_loop) |
| 225 | + work_loop.run_forever() |
| 226 | + finally: |
| 227 | + work_loop.close() |
0 commit comments