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

如何获取Metronome所有作业的当前运行状态?API仪表盘开发疑问

Solution to Fetch Metronome Job Statuses via REST API

Got it, let's break down how to get the latest run status for all your Metronome jobs using its REST API. Here's a step-by-step approach that combines the two endpoints you mentioned:

Step 1: Retrieve All Job IDs

First, you’ll need to fetch the full list of jobs to get their unique job_ids. Use the base jobs endpoint:

GET /v1/jobs

This returns an array of job objects, each containing the id field (your target job_id) along with the job configuration. For example, a single job entry might look like:

{
  "id": "my-scheduled-job-123",
  "name": "My Scheduled Job",
  "description": "Runs daily data sync",
  // ... other configuration fields
}

Step 2: Fetch Job History for Each ID

Once you have all job_ids, iterate through them and call the job-specific endpoint with the embed=history parameter to pull the run history:

GET /v1/jobs/{job_id}?embed=history

The response will include a history array that lists all past runs for the job. By default, this array is sorted newest to oldest, so the first entry (history[0]) will be the latest run. The status field in this entry will tell you if the run succeeded ("SUCCESS"), failed ("FAILED"), or is in another state like "RUNNING".

Example Response Snippet

{
  "id": "my-scheduled-job-123",
  "name": "My Scheduled Job",
  "history": [
    {
      "id": "my-scheduled-job-123-20240520T100000",
      "status": "SUCCESS",
      "createdAt": "2024-05-20T10:00:00.000Z",
      // ... other run details
    },
    {
      "id": "my-scheduled-job-123-20240519T100000",
      "status": "FAILED",
      "createdAt": "2024-05-19T10:00:00.000Z"
    }
  ]
}

Step 3: Aggregate Results

Combine the job metadata from Step 1 with the latest status from Step 2 to build your dashboard-ready dataset. Here’s a quick Python example using the requests library to automate this:

import requests

# Replace with your Metronome host/port
METRONOME_BASE_URL = "http://your-metronome-instance:8080/v1"

def get_all_job_statuses():
    # Fetch all jobs
    jobs_response = requests.get(f"{METRONOME_BASE_URL}/jobs")
    jobs_response.raise_for_status()  # Handle HTTP errors
    jobs = jobs_response.json()

    job_status_list = []
    for job in jobs:
        job_id = job["id"]
        # Fetch job with history
        history_response = requests.get(f"{METRONOME_BASE_URL}/jobs/{job_id}?embed=history")
        history_response.raise_for_status()
        job_with_history = history_response.json()

        # Extract latest status (handle jobs that never ran)
        if job_with_history.get("history"):
            latest_run = job_with_history["history"][0]
            latest_status = latest_run["status"]
            run_timestamp = latest_run["createdAt"]
        else:
            latest_status = "Never Ran"
            run_timestamp = None

        job_status_list.append({
            "job_id": job_id,
            "job_name": job.get("name", job_id),
            "latest_status": latest_status,
            "last_run_time": run_timestamp
        })
    return job_status_list

# Example usage
if __name__ == "__main__":
    statuses = get_all_job_statuses()
    for status in statuses:
        print(f"Job: {status['job_name']} | Latest Status: {status['latest_status']} | Last Run: {status['last_run_time'] or 'N/A'}")

Key Notes & Optimizations

  • Rate Limiting: If you have a large number of jobs, avoid making sequential requests one after another. Use parallel requests (e.g., with concurrent.futures in Python) to speed things up, but be mindful of Metronome’s API rate limits to avoid being throttled.
  • Error Handling: Add retry logic for failed requests (e.g., network issues, temporary API downtime) to make your dashboard more robust.
  • Job States: Metronome may return other statuses like "RUNNING" or "SCHEDULED"—make sure your dashboard handles these cases appropriately.
  • API Version: Confirm you’re using the correct Metronome API version (v1 in this case) for your deployment, as older versions might have different endpoint behavior.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 08:10:12