1 # Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
2 # See https://llvm.org/LICENSE.txt for license information.
3 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4 """Tool for counting the number of currently running Github actions jobs.
6 This tool counts and enumerates the currently active jobs in Github actions
9 python3 ./count_running_jobs.py --token=<github token>
11 Note that the token argument is optional. If it is not specified, the queries
12 will be performed unauthenticated.
21 def main(token
, filter_gha_runners
):
23 github
.Github(args
.token
)
24 .get_repo("llvm/llvm-project")
25 .get_workflow_runs(status
="in_progress")
30 for workflow
in workflows
:
31 for job
in workflow
.jobs():
32 if job
.status
== "in_progress":
33 # TODO(boomanaiden154): Remove the try/except block once we are able
34 # to pull in a PyGithub release that has the runner_group_name property
37 if filter_gha_runners
and job
.runner_group_name
!= "GitHub Actions":
41 "Failed to filter runners. Your PyGithub version is "
42 "most likely too old."
44 print(f
"{workflow.name}/{job.name}")
47 print(f
"\nFound {in_progress_jobs} running jobs.")
49 return in_progress_jobs
52 if __name__
== "__main__":
53 parser
= argparse
.ArgumentParser(
54 description
="A tool for listing and counting Github actions jobs"
59 help="The Github token to use to authorize with the API",
66 help="The output file to write time-series data to",
71 "--data-collection-interval",
73 help="The number of seconds between data collection intervals",
78 "--filter-gha-runners",
79 help="Only consider jobs running on hosted Github actions runners",
83 "--no-filter-gha-runners",
84 dest
="filter_gha_runners",
86 help="Consider all running jobs",
88 parser
.set_defaults(filter_gha_runners
=False)
89 args
= parser
.parse_args()
91 # Perform some basic argument validation
93 # If an output file is specified, the user must also specify the data
94 # collection interval.
95 if bool(args
.output_file
) and not bool(args
.data_collection_interval
):
96 print("A data collection interval must be specified when --output_file is used")
99 if args
.data_collection_interval
:
101 current_time
= time
.localtime()
102 current_time_string
= time
.strftime("%Y/%m/%d %H:%M:%S", current_time
)
104 print(f
"Collecting data at {current_time_string}")
106 current_job_count
= main(args
.token
, args
.filter_gha_runners
)
109 with
open(args
.output_file
, "a") as output_file_handle
:
110 output_file_handle
.write(
111 f
"{current_time_string},{current_job_count}\n"
114 time
.sleep(args
.data_collection_interval
)
116 main(args
.token
, args
.filter_gha_runners
)