1

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

Marcin
  • 510
  • 6
  • 22

1 Answers1

1

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')
Bertrand Martel
  • 42,756
  • 16
  • 135
  • 159
  • I thought this script was broken until I realized that (of course) you need an access token for an account with permissions to view runner information, which a regular user typically isn't. Specifically, the access token needs the scope `read_api` (and nothing else) and needs to be for a user that has the correct permissions to view runner info, which I believe at instance level can only be administrators. – Torque May 06 '21 at 10:42