|
1 | 1 | # The Serverless Worker |
2 | 2 |
|
3 | | -## Logging |
| 3 | +Both RunPod official endpoints as well as custom built endpoints function by means of a worker that fetches available jobs, passes them into a handler and then returns the output. |
4 | 4 |
|
5 | | -The worker outputs logs to the console at different points in the workers lifecycle. These logs can be used to debug issues with the worker or handler. There are four logging levels that can be used to control the verbosity of the logs: |
| 5 | +A worker entry point is a python file containing the command `runpod.serverless.start(config)`. An minimal worker file is shown below: |
6 | 6 |
|
7 | | - 0. `NOTSET` - Does not output any logs. |
8 | | - |
9 | | - 1. `DEBUG` (Default) - Outputs all logs, including debug logs. |
10 | | - |
11 | | - 2. `INFO` - Outputs all logs except debug logs. |
12 | | - |
13 | | - 3. `WARNING` - Outputs only warning and error logs. |
14 | | - |
15 | | - 4. `ERROR` - Outputs only error logs. |
16 | | - |
17 | | -### Setting the Logging Level |
18 | | - |
19 | | -There are two ways to set the logging level: |
20 | | - |
21 | | - 1. Set the `RUNPOD_DEBUG_LEVEL` environment variable to one of the above logging levels. |
| 7 | +```python |
| 8 | +import runpod |
22 | 9 |
|
23 | | - 2. Set the `rp_log_level` argument when calling the file with your handler. If this value is set, it will override the `RUNPOD_DEBUG_LEVEL` environment variable. |
| 10 | +def handler(job): |
| 11 | + # Handle the job and return the output |
| 12 | + return {"output": "Job completed successfully"} |
24 | 13 |
|
25 | | - ```python |
26 | | - python worker.py --rp_log_level='INFO' |
27 | | - ``` |
| 14 | +runpod.serverless.start({"handler": handler}) |
| 15 | +``` |
28 | 16 |
|
29 | | -## Error Handling |
| 17 | +## config |
30 | 18 |
|
31 | | -The worker is designed to handle errors raised by the handler gracefully. If the handler raises an error, the worker will capture this error and return it as the job output along with the stack trace. |
| 19 | +The `config` parameter is a dictionary containing the following keys: |
32 | 20 |
|
33 | | -If you want to return a custom error within the handler, this can be accomplished by returning a dictionary with a top-level key of `error` and a value of the error message. The worker will then return this error message as the job output. |
| 21 | +| Key | Type | Description | |
| 22 | +|-----------|------------|--------------------------------------------------------------| |
| 23 | +| `handler` | `function` | The handler function that will be called with the job input. | |
34 | 24 |
|
35 | | -### Example |
| 25 | +### handler |
36 | 26 |
|
37 | | -```python |
38 | | -def handler_with_custom_error(job): |
39 | | - if job["id"] == "invalid_job": |
40 | | - return {"error": "Invalid job ID"} |
41 | | - else: |
42 | | - # Handle the job and return the output |
43 | | - return {"output": "Job completed successfully"} |
44 | | -``` |
| 27 | +The handler function can either had a standard return or be a generator function. If the handler is a generator function, it will be called with the job input and the generator will be iterated over until it is exhausted. |
45 | 28 |
|
46 | 29 | ## Worker Refresh |
47 | 30 |
|
|
0 commit comments