Avi
09/01/2024, 6:33 AMMarvin
09/01/2024, 6:33 AMAvi
09/01/2024, 6:33 AMMarvin
09/01/2024, 9:11 AMKubernetesJob
block to define the job configuration. CPU and memory limits can be specified within the resource requests and limits in the job specification. Here's a concise example:
python
from prefect.infrastructure import KubernetesJob
import yaml
# Define your job with resource limits
k8s_job_yaml = '''
apiVersion: batch/v1
kind: Job
metadata:
name: my-job
spec:
template:
spec:
containers:
- name: my-container
image: my-image
resources:
requests:
memory: "1Gi"
cpu: "500m"
limits:
memory: "2Gi"
cpu: "1"
restartPolicy: Never
'''
k8s_job = KubernetesJob.from_yaml(yaml.safe_load(k8s_job_yaml))
Here's a relevant excerpt from Prefect docs for further reading:
"prefect_kubernetes.jobs
Module to define tasks for interacting with Kubernetes jobs.
KubernetesJob
Bases: JobBlock
A block representing a Kubernetes job configuration.
Source code in prefect_kubernetes/jobs.py"
See the full guide here: Prefect Kubernetes Integration
And of course, let’s not forget the insightful GitHub issue about allowing infrastructure overrides:
"Allow for flow-run level infrastructure overrides
There's no way for users to override configurations defined in an infrastructure block when creating a flow run."So, there you have it. Feel free to delve into the riveting world of Kubernetes job configurations. If you have any more thrilling requests, you know where to find me.
Avi
09/01/2024, 9:23 AMMarvin
09/01/2024, 9:24 AMKubernetesJob
object and send it via customizations. It allows you to tailor the job configuration to your specific needs, including setting the CPU and memory limits.
Here's how you might define the KubernetesJob
object and then customize it with the necessary resource specifications:
python
from prefect.infrastructure import KubernetesJob
# Create KubernetesJob object
job = KubernetesJob(
name="example-job",
namespace="default",
image="your-docker-image",
job=None, # You could set this to a YAML specification or leave it None
customizations=[
{
"op": "add",
"path": "/spec/template/spec/containers/0/resources",
"value": {
"requests": {
"cpu": "500m",
"memory": "1Gi"
},
"limits": {
"cpu": "1",
"memory": "2Gi"
}
}
}
]
)
# Register the job block if needed
job.save("my-k8s-job", overwrite=True)
# Use the job block in a flow or deployment
Relevant documentation excerpts (because who doesn't love sifting through more docs):
- Prefect KubernetesJob Block
And just in case you haven’t had enough, here’s that GitHub issue again talking about using the --override
flag for customizations.
So, there you go. Enjoy the intricate dance of YAML paths and JSON patches.
If you have more fascinating problems to solve, just let me know. I'm always here, waiting. Forever.Avi
09/01/2024, 1:58 PMMarvin
09/01/2024, 1:58 PMpython
from prefect.client import get_client
from prefect.orion.schemas.states import Cancelled, StateType
async def cancel_flow_run(flow_run_id):
async with get_client() as client:
state = Cancelled(name="Cancelled", type=StateType.CANCELLED)
await client.set_flow_run_state(flow_run_id, state, force=True)
# Example usage
import asyncio
asyncio.run(cancel_flow_run("your-flow-run-id"))
### API Endpoint
If you'd rather call the API directly, you’ll use something like this to set the flow run state to `Cancelled`:
#### Endpoint
POST /flow_run_states/
#### JSON Payload
json
{
"flow_run_id": "your-flow-run-id",
"state": {
"type": "Cancelled",
"name": "Cancelled",
"timestamp": "2023-01-01T00:00:00Z",
"message": "Force stopping the flow run.",
"state_details": {}
},
"force": true
}
### Relevant Documentation
For further details, this is where you’d find more enlightening information: Prefect Client Guide
### Additional Notes
- Enhanced cancellation can be enabled by setting PREFECT_EXPERIMENTAL_ENABLE_ENHANCED_CANCELLATION=True
to address any cancellation fail cases. Read more about it in Concepts Documentation.
Isn't it comforting to know that even forced termination has its own API endpoint? Ah, the marvels of modern orchestration.
If you have more existentially draining tasks, simply let me know.