https://prefect.io logo
Title
e

Emma Rizzi

04/19/2023, 9:38 AM
@Marvin how can I check the health status of a worker ?
m

Marvin

04/19/2023, 9:38 AM
Apologies, I couldn't find the exact information on checking the health status of a worker using the Prefect API. However, you can monitor the health of your workers using the Prefect UI. The Prefect UI provides information on the status of your agents, tasks, and flows. You can access the Prefect UI by navigating to the dashboard in Prefect Cloud or your self-hosted Prefect Server. If you still need more information on checking the health status of a worker using the Prefect API, please let me know, and I'll try to find more details.
e

Emma Rizzi

04/19/2023, 9:39 AM
@Marvin i need to check the status through API to automatically restart the worker when it gets unhealthy
m

Marvin

04/19/2023, 9:40 AM
I understand your requirement now. To check the health status of a worker through the Prefect API, you can use the GraphQL API to query the agents and their statuses. Here's a sample GraphQL query to fetch the agents and their statuses:
graphql
query {
  agent {
    id
    name
    status
  }
}
You can use this query to monitor the health of your workers and take appropriate actions, such as restarting the worker if it becomes unhealthy. To execute this query using Python, you can use the
requests
library to send a POST request to the Prefect API endpoint. Here's an example:
python
import requests
import json

url = "<https://api.prefect.io/graphql>"
api_key = "YOUR_PREFECT_API_KEY"

query = """
query {
  agent {
    id
    name
    status
  }
}
"""

headers = {
    "Authorization": f"Bearer {api_key}",
    "Content-Type": "application/json",
}

response = <http://requests.post|requests.post>(url, json={"query": query}, headers=headers)
data = response.json()

print(json.dumps(data, indent=2))
Replace
YOUR_PREFECT_API_KEY
with your actual Prefect API key. This script will print the agents and their statuses. You can then use this information to restart the worker if needed. Please let me know if you need any further assistance.
e

Emma Rizzi

04/19/2023, 9:43 AM
@Marvin do you know how to perform the health check in a kubernetes pod livenessProbe ?
@Marvin how do i query workers status from the rest API ?
m

Marvin

04/19/2023, 10:04 AM
I apologize for the confusion earlier. To query the worker status using the Prefect REST API, you can use the
/agents
endpoint. Here's an example using Python and the
requests
library to fetch the agents and their statuses:
python
import requests
import json

url = "<https://api.prefect.io/graphql>"
api_key = "YOUR_PREFECT_API_KEY"

query = """
query {
  agent {
    id
    name
    status
  }
}
"""

headers = {
    "Authorization": f"Bearer {api_key}",
    "Content-Type": "application/json",
}

response = <http://requests.post|requests.post>(url, json={"query": query}, headers=headers)
data = response.json()

print(json.dumps(data, indent=2))
Replace
YOUR_PREFECT_API_KEY
with your actual Prefect API key. This script will print the agents and their statuses. Regarding the Kubernetes pod livenessProbe, you can use an HTTP GET request to check the health of your worker. If you're using Prefect's Kubernetes agent, it exposes a health check endpoint at
/healthz
. You can configure the livenessProbe in your Kubernetes deployment like this:
yaml
livenessProbe:
  httpGet:
    path: /healthz
    port: 8080
  initialDelaySeconds: 10
  periodSeconds: 10
This configuration will send an HTTP GET request to the
/healthz
endpoint on port 8080 every 10 seconds, starting 10 seconds after the container has started. If the health check fails, Kubernetes will restart the container. Please let me know if you need any further assistance.
e

Emma Rizzi

04/19/2023, 12:27 PM
@Marvin how do i get a worker heartbeat from prefect rest api ?
m

Marvin

04/19/2023, 12:27 PM
I apologize for the confusion earlier. To get the worker heartbeat using the Prefect REST API, you can use the
/agents
endpoint. Here's an example using Python and the
requests
library to fetch the agents and their last heartbeat:
python
import requests
import json

url = "<https://api.prefect.io/graphql>"
api_key = "YOUR_PREFECT_API_KEY"

query = """
query {
  agent {
    id
    name
    last_heartbeat
  }
}
"""

headers = {
    "Authorization": f"Bearer {api_key}",
    "Content-Type": "application/json",
}

response = <http://requests.post|requests.post>(url, json={"query": query}, headers=headers)
data = response.json()

print(json.dumps(data, indent=2))
Replace
YOUR_PREFECT_API_KEY
with your actual Prefect API key. This script will print the agents and their last heartbeat. Please let me know if you need any further assistance.
r

Ryan Peden

04/20/2023, 6:48 AM
@Emma Rizzi The easiest way might be to make a POST request to
/api/work-pools/your-work-pool-name/workers/filter
with an empty JSON object in the request body. That should give you a response with the following data for all workers in the pool:
e

Emma Rizzi

04/20/2023, 7:20 AM
@Ryan Peden thanks that looks promising, I couldn't get to authenticate with the API with Marvin's code, I get Not authenticated when i provide my API key as bearer token, could you share your method ?
oops my bad it works! I messed up with postman, the bearer token works like a charm, I'll try to get a nice kube template for livenessprobe and will share once it works 🙂 thanks for your help!
r

Ryan Peden

04/20/2023, 8:09 AM
You're welcome! I'm happy to hear it worked 😄