Skip to content

Commit d2ca1fd

Browse files
authored
CMR-10723 bump up urllib to a modern version (#2282)
* bump up urllib to a supported version * added command line flag for running one job as a test * output moved to logs * fixed end point function to construct a localhost url, for testing
1 parent 81be257 commit d2ca1fd

4 files changed

Lines changed: 57 additions & 14 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[*]
2+
end_of_line = lf
3+
indent_size = 2
4+
indent_style = space
5+
6+
[*.{md,py}]
7+
indent_size = 4 #this is important, the markdown for API docs will not see 2 spaces
8+
indent_style = space
9+
insert_final_newline = true
10+
trim_trailing_whitespace = true

job-utilities/local_development/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ Cron jobs are assumed to be run once every day at a given time. Most jobs are li
1010
### Running
1111

1212
Ensure you have the right dependencies by running `pip3 install -r requirements.txt` and then simply run the local_scheduler.py program.
13-
Note that the program currently does not run jobs immediately upon scheduling.
13+
Note that the program currently does not run jobs immediately upon scheduling. To run a job right away as a test, use the -t flag.
Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,54 @@
1+
#!/usr/bin/env python3
2+
13
"""
24
local_scheduler takes the job details json file to create
35
a simple schedule with the Python schedule library.
46
Interval jobs are simply translated unto the library,
57
cron jobs are assumed to run at a given HH:MM every day.
68
"""
9+
import argparse
10+
import logging
711
import time
812
import json
913
import os
14+
import sys
15+
16+
# pylint: disable=import-error
1017
import schedule
1118
import urllib3
1219

20+
# setup logger
21+
logging.basicConfig(level=logging.INFO,
22+
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
23+
filename=f'{__file__}.log')
24+
logger: logging.Logger = logging.getLogger(__name__)
25+
1326
service_ports_file_name = os.getenv("SERVICE_PORTS_FILE", "service-ports.json")
1427
job_details_file_name = os.getenv("JOB_DETAILS_FILE", "../job-details.json")
28+
cmr_host_name: str = os.getenv("CMR_HOST_NAME", "localhost")
1529

16-
pool_manager = urllib3.PoolManager(headers={"Authorization" : "mock-echo-system-token"})
30+
pool_manager = urllib3.PoolManager(headers={"Authorization" : "mock-echo-system-token",
31+
"client-id": f'{__file__}'})
1732

1833
with open(service_ports_file_name, encoding="UTF-8") as service_ports_file:
1934
service_port_map = json.load(service_ports_file)
2035

21-
def build_endpoint(job):
36+
def build_endpoint(host_name, job):
2237
"""
2338
Takes the job details and builds the local job endpoint
2439
"""
25-
url = "http://{}:{}"
40+
url = "http://{}:{}/{}"
2641
port = service_port_map[job["target"]["service"]]
27-
return url.format(port, job["target"]["endpoint"])
42+
return url.format(host_name, port, job["target"]["endpoint"])
2843

29-
def run_job(details, name):
44+
def run_job(job_details: dict, job_name :str):
3045
"""
3146
Takes the job details and runs a REST request on the job endpoint.
3247
"""
33-
print('send ' + details["target"]["request-type"] + \
34-
' to ' + details["target"]["endpoint"] + ' for job ' + name)
35-
pool_manager.request(details["target"]["request-type"], build_endpoint(details))
48+
logger.info('send ' + job_details["target"]["request-type"] + \
49+
' to ' + job_details["target"]["endpoint"] + ' for job ' + job_name)
50+
url: str = build_endpoint(cmr_host_name, job_details)
51+
pool_manager.request(job_details["target"]["request-type"], url)
3652

3753
def create_schedule():
3854
"""
@@ -46,22 +62,39 @@ def create_schedule():
4662
if job_details["scheduling"]["type"] == "cron":
4763
hours = str(job_details["scheduling"]["timing"]["hours"]).zfill(2)
4864
minutes = str(job_details["scheduling"]["timing"]["minutes"]).zfill(2)
49-
print("Scheduling job " + job_name + " at " + hours + ":" + minutes)
65+
logger.info("Scheduling job " + job_name + " at " + hours + ":" + minutes)
5066

5167
schedule.every().day.at(hours + ":" + minutes).do(run_job,
5268
job_details=job_details, job_name=job_name)
5369
elif job_details["scheduling"]["type"] == "interval":
5470
minutes = job_details["scheduling"]["timing"].get("minutes", 0)
5571
hours = job_details["scheduling"]["timing"].get("hours", 0)
5672
total_minutes = minutes + hours*60
57-
print("Scheduling job for every " + str(total_minutes) + " minutes")
73+
logger.info("Scheduling job for every %d minutes", total_minutes)
5874

5975
schedule.every(total_minutes).seconds.do(run_job, job_details=job_details,
6076
job_name=job_name)
6177

62-
if __name__ == '__main__':
63-
create_schedule()
78+
def main():
79+
""" The primary interface for this script. """
80+
parser = argparse.ArgumentParser(description="External CMR scheduler")
81+
parser.add_argument('-t', '--test', action='store_true',
82+
help='Do a test run of RefreashKMSCache and exit.')
83+
args = parser.parse_args()
6484

85+
if args.test:
86+
# use these next lines to force a test on a very specific job
87+
test_detail = {"target": {"request-type": "POST",
88+
"service": "bootstrap",
89+
"single-target": True,
90+
"endpoint": "caches/refresh/kms"}}
91+
run_job(test_detail, "RefreshKMSCache")
92+
sys.exit()
93+
94+
create_schedule()
6595
while True:
6696
schedule.run_pending()
6797
time.sleep(1)
98+
99+
if __name__ == '__main__':
100+
main()
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
urllib3==1.26.19
1+
urllib3~=2.5.0
22
schedule

0 commit comments

Comments
 (0)