eddie
eddie

Reputation: 33

Find Upcoming Rundeck Job name & ID using API Call?

I am creating a rundeck-api to findout which jobs will be running in next X hours I am executing the python script from Azure DevOps Pipeline

Below is the script

import requests
from datetime import datetime, timedelta

api_token = '$(api_token)$' 
project_name = '$(project_name)$'

api_url = "https://infosys:4443/api/19/project/{}/jobs".format(project_name)

headers = {
    "Accept": "application/json",
    "X-Rundeck-Auth-Token": api_token,
    "Content-Type": "application/json"
}

response = requests.get(api_url, headers=headers, verify=False)

if response.status_code == 200:
    scheduled_jobs = response.json()
    current_time = datetime.now()
    three_hours_from_now = current_time + timedelta(hours=3)
    upcoming_jobs = []
    for job in scheduled_jobs:
        if 'schedule' in job and 'nextRunAt' in job['schedule']:
            next_run_at = datetime.strptime(job['schedule']['nextRunAt'], "%Y-%m-%dT%H:%M:%S%z")
            if current_time <= next_run_at <= three_hours_from_now:
                upcoming_jobs.append((job['name'], next_run_at))
    if upcoming_jobs:
        print("Upcoming jobs in the next 3 hours:")
        for job_name, next_run_at in upcoming_jobs:
            print("Job Name: {}, Next Scheduled: {}".format(job_name, next_run_at))
    else:
        print("No jobs scheduled in the next 3 hours.")
else:
    print("Error fetching scheduled jobs:", response.text)

My ADO Pipeline

trigger: none
stages:
- stage: Create
  pool: 
   name: sadasdadsdasd
   demands:
    - agent.name -equals sadasdadsdasd
  jobs:
  - job: BuildJob
    steps:
    - script: echo Building!
    - task: Bash@3
      inputs:
        targetType: 'inline'
        script: |
          pip install requests
          pip install --upgrade requests
          pip3 install simplejson
      displayName: PIP
    - task: replacetokens@5
      inputs:
        rootDirectory: '$(Build.SourcesDirectory)/rdjob/'
        targetFiles: 'devcheck.py'
        encoding: 'auto'
        tokenPattern: 'custom'
        tokenPrefix: '$('
        tokenSuffix: ')$'
        writeBOM: true
        actionOnMissing: 'warn'
        keepToken: false
        actionOnNoFiles: 'continue'
        enableTransforms: false
        enableRecursion: false
        useLegacyPattern: false
        enableTelemetry: true

    - task: Bash@3
      inputs:
        targetType: 'inline'
        script: 'python devcheck.py'

I have few jobs which will be running in every 2 hour 40 min My expectation was to display that job name and ID . currently its saying No jobs scheduled in the next 3 hours. All my Jobs have timezone as UTC

Can you please let me know what changes I need to do achieve it.

PS:- I referred RunDeck Documentation

But no Luck

Upvotes: 0

Views: 113

Answers (0)

Related Questions