You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从Job file中收取usage charge并运行指定URL?

Hey there! Let's figure out how you can handle collecting usage charges via that URL directly from your Job file. I’ve worked through similar scenarios before, so here are some practical, actionable approaches based on common job scheduling setups:

1. Embed a Direct Script Call in Your Job File

Most job systems (cron, Jenkins, Airflow, you name it) let you run inline commands or scripts right in the job definition. Tools like curl (for shell) or requests (for Python) make hitting your usage charge URL straightforward:

  • For shell-based jobs (cron, bash scripts):
    Add this line after your main job task finishes (customize the URL and payload to match your endpoint's requirements):

    # Submit usage charge after job completes
    curl -X POST "https://your-usage-charge-endpoint.com/api/submit-charge" \
      -H "Content-Type: application/json" \
      -H "Authorization: Bearer YOUR_SECRET_TOKEN" \
      -d '{
          "job_id": "PROD_JOB_001",
          "usage": {
              "duration_hours": 2.75,
              "resource_usage_gb": 12
          }
      }'
    

    Pro tip: Store your auth token in an environment variable instead of hardcoding it—something like $CHARGE_API_TOKEN keeps things secure.

  • For Python-based jobs (Airflow DAGs, Python scripts):
    Use the requests library to make the call after your job logic runs:

    import requests
    import os
    
    def send_usage_charge():
        # Fetch auth token from env var for security
        auth_token = os.getenv("CHARGE_API_TOKEN")
        payload = {
            "job_id": "PROD_JOB_001",
            "usage": {"duration_hours": 2.75, "resource_usage_gb": 12}
        }
        headers = {"Authorization": f"Bearer {auth_token}", "Content-Type": "application/json"}
        
        try:
            response = requests.post("https://your-usage-charge-endpoint.com/api/submit-charge", json=payload, headers=headers)
            response.raise_for_status()  # Throw error if request fails
            print("Usage charge submitted successfully!")
        except requests.exceptions.RequestException as e:
            print(f"Failed to submit charge: {str(e)}")
            # Add retry logic here if needed (e.g., using tenacity library)
    
    # Call this function after your main job task
    send_usage_charge()
    
2. Use Job Scheduler Hooks/Callbacks

If you want to keep your job logic clean, use built-in hooks from your scheduler that trigger after the job finishes (success or failure):

  • Jenkins: Add a "Post-build Action" → "Execute shell" or "Execute Python script" step to run your URL call.
  • Airflow: Attach on_success_callback or on_failure_callback to your task definition to trigger the charge request only when the job completes as expected.
  • AWS Batch: Set up a job notification to trigger an AWS Lambda function, which can pull job metrics from Batch's API and send them to your charge URL.
3. Wrap Your Job in a Wrapper Script

If your job is a standalone executable, create a wrapper script that runs the job first, then handles the charge submission. This is great for keeping your original job file untouched:

#!/bin/bash

# Run your main job task
echo "Starting main job..."
/path/to/your/original_job.sh

# Capture the job's exit code to decide if we should submit a charge
JOB_EXIT_CODE=$?

# Only submit charge if the job succeeded (adjust this logic if needed)
if [ $JOB_EXIT_CODE -eq 0 ]; then
    echo "Job succeeded, submitting usage charge..."
    curl -X POST "https://your-usage-charge-endpoint.com/api/submit-charge" \
      -H "Authorization: Bearer $CHARGE_API_TOKEN" \
      -d '{"job_id": "PROD_JOB_001", "status": "success"}'
else
    echo "Job failed, skipping charge submission."
fi

# Exit with the original job's exit code so your scheduler tracks the correct status
exit $JOB_EXIT_CODE

Then update your scheduler to run this wrapper script instead of the original job file.

Quick Best Practices
  • Error Handling: Add retry logic for transient failures (e.g., network issues) so you don't miss charges. Tools like curl --retry 3 or Python's tenacity library work well here.
  • Idempotency: Make sure your charge endpoint is idempotent—calling it multiple times shouldn't result in duplicate charges. Use a unique job_id as a key to avoid this.
  • Logging: Log the outcome of the charge request (success/failure) so you can debug issues later.

内容的提问来源于stack exchange,提问作者Ronak Panchal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 06:53:33