Marcin
Marcin

Reputation: 600

How to check if runner is currently running a job using jobs API

I would like to check which runners are currently running jobs but i fail to find anything that would give me this information using API.

I know which ones are active and can take jobs but not the ones that are actually running them at the current time.

So my question is, how can i determine which runners are currently processing a job

Upvotes: 2

Views: 3022

Answers (1)

Bertrand Martel
Bertrand Martel

Reputation: 45382

You can list all the runners, get theirs ids and then for each runner check if there are jobs with status running:

The following script uses and :

#!/bin/bash

token=YOUR_TOKEN
domain=your.domain.com
ids=$(curl -s -H "PRIVATE-TOKEN: $token" "https://$domain/api/v4/runners/all" | \
    jq '.[].id')
set -- $ids
for i
do
    result=$(curl -s \
    -H "PRIVATE-TOKEN: $token" \
    "https://$domain/api/v4/runners/$i/jobs?status=running" | jq '. | length')
    if [ $result -eq 0 ]; then
        echo "runner $i is not running jobs"
    else
        echo "runner $i is running $result jobs"
    fi
done

Output:

runner 6 is not running jobs

runner 7 is running 1 jobs

runner 8 is not running jobs

Using :

import requests 
import json

token = "YOUR_TOKEN"
domain = "your.domain.com"

r = requests.get(
    f'https://{domain}/api/v4/runners/all',
    headers = { "PRIVATE-TOKEN": token }
)
ids = [ i["id"] for i in json.loads(r.text) ]

for i in ids:
    r = requests.get(
        f'https://{domain}/api/v4/runners/{i}/jobs?status=running',
        headers = { "PRIVATE-TOKEN": token }
    )
    num_jobs = len(json.loads(r.text))
    if num_jobs > 0:
        print(f'runner {i} is running {num_jobs} jobs')
    else:
        print(f'runner {i} is not running jobs')

Upvotes: 2

Related Questions