Skip to content

Commit 76532c5

Browse files
committed
feat: add global support for refresh_worker
1 parent a54d9ff commit 76532c5

3 files changed

Lines changed: 9 additions & 89 deletions

File tree

docs/serverless/infer_file.md

Lines changed: 0 additions & 27 deletions
This file was deleted.

docs/serverless/worker.md

Lines changed: 5 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,10 @@
1-
# RunPod Pod Worker
1+
# The Serverless Worker
22

3-
To convert a Pod to a Worker, you need to add the following annotations to the Pod:
43

5-
## Environment Variables
64

7-
```bash
8-
# Development
9-
RUNPOD_DEBUG= # Set to 'true' to enable debug mode, otherwise leave blank
10-
RUNPOD_DEBUG_LEVEL= # ERROR, WARN, INFO, DEBUG
5+
## Worker Refresh
116

12-
# Realtime
13-
RUNPOD_REALTIME_PORT= # Port to listen on for realtime connections (Default: None)
14-
RUNPOD_REALTIME_CONCURRENCY= # Number of workers to spawn (Default: 1)
15-
```
7+
For more complex operations where you are downloading files or making changes to the worker it can be beneficial to refresh the worker between jobs. This can be accomplished by enabling a refresh_worker worker flag in 1 of two ways:
168

17-
The following environment variables are set automatically by RunPod, you DO NOT need to set them manually:
18-
19-
```bash
20-
# API Endpoints
21-
RUNPOD_AI_API_KEY= # Serverless API Key
22-
RUNPOD_WEBHOOK_GET_JOB= # URL to get job work from
23-
RUNPOD_WEBHOOK_POST_OUTPUT= # URL to post output to
24-
RUNPOD_WEBHOOK_PING= # URL to ping
25-
RUNPOD_PING_INTERVAL= # Interval in milliseconds to ping the API (Default: 10000)
26-
27-
RUNPOD_ENDPOINT_ID= # Endpoint ID
28-
RUNPOD_ENDPOINT_SECRET= # Endpoint Secret (SET BY RUNPOD Replaces RUNPOD_AI_API_KEY, not implemented yet)
29-
```
30-
31-
### Additional Variables
32-
33-
These are variables that are accessed from the RunPod container and not required to be set manually:
34-
35-
```bash
36-
# Pod Information
37-
RUNPOD_POD_ID= # Pod ID
38-
```
39-
40-
## Error Handling
41-
42-
If an error occurs, the worker will send a message to the API with the error message and the job will be marked as failed.
43-
44-
To report a job error call `job.error(worker_id, job_id, error_message)`.
45-
46-
---
47-
48-
## Worker Lifecycle Management
49-
50-
The duration of the worker is managed within the [lifecycle](../PodWorker/modules/lifecycle.py) module.
51-
52-
The worker starts with a TTL as specified by the environment variable `TERMINATE_IDLE_TIME` or defaults to 60 seconds as specified with `self.ttl`. When a new job is received, a `work_in_progress` flag is set. When the job is completed, the `work_in_progress` flag is cleared and the TTL is reset. If the `work_in_progress` flag is not cleared within the `work_timeout` period, the worker will exit.
53-
54-
If the worker does not receive a new job within idle period, the worker will exit.
55-
56-
## Local Testing
57-
58-
To test locally, create the file `test_input.json` in the root directory that contains the following:
59-
60-
```json
61-
{
62-
"id": "LOCAL-TEST",
63-
"input":{}
64-
}
65-
```
66-
67-
If the required webhook environment variables are not set, the worker will default to local testing.
9+
1. Enable on start with `runpod.serverless.start({"handler": handler, "refresh_worker": True})`, this will refresh the worker after every job return, even if the handler raises an error.
10+
2. Return `refresh_worker=True` as a top level dictionary key in the handler return. This can selectively be used to refresh the worker based on the job return.

runpod/serverless/work_loop.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ async def start_worker(config):
4848

4949
job_result = run_job(config["handler"], job)
5050

51+
# If refresh_worker is set, pod will be reset after job is complete.
52+
if config.get("refresh_worker", False):
53+
job_result["stopPod"] = True
54+
5155
await send_result(session, job_result, job)
5256

5357
set_job_id(None)

0 commit comments

Comments
 (0)