1- from datetime import datetime , timedelta
1+ from datetime import datetime
22from logging .config import dictConfig
33
44from flask import Flask , abort , request
1010 "version" : 1 ,
1111 "formatters" : {
1212 "default" : {
13- "format" : "[%(asctime)s]: %(levelname)s| %(message)s" ,
13+ "format" : "[%(asctime)s]: %(levelname)s | %(message)s" ,
1414 }
1515 },
1616 "handlers" : {
@@ -34,18 +34,6 @@ def parse_datetime(date: str) -> datetime:
3434 return datetime .strptime (date , exp )
3535
3636
37- def prune_jobs ():
38- # if the first job is older than 2 days, prune all jobs older than 2 days
39- # this might happen due to not completed jobs
40- if datetime .fromtimestamp (list (jobs .values ())[0 ]) > datetime .now () - timedelta (
41- days = 2
42- ):
43- app .logger .info ("Pruning jobs" )
44- for job_id , timestamp in jobs .items ():
45- if (datetime .now () - datetime .fromtimestamp (timestamp )).days >= 2 :
46- del jobs [job_id ]
47-
48-
4937def validate_origin_github () -> bool :
5038 userAgent = request .headers .get ("User-Agent" )
5139 if not userAgent .startswith ("GitHub-Hookshot" ):
@@ -63,6 +51,14 @@ def validate_origin_github() -> bool:
6351 return True
6452
6553
54+ def get_message (* args ):
55+ msg = list ()
56+ for variable in args :
57+ var_name = f"{ variable = } " .split ("=" )[0 ]
58+ msg .append (f'{ var_name } ="{ variable } "' )
59+ return " " .join (msg )
60+
61+
6662def process_workflow_job ():
6763 job = request .get_json ()
6864
@@ -75,8 +71,7 @@ def process_workflow_job():
7571 if action == "queued" :
7672 # add to memory as timestamp
7773 jobs [job_id ] = int (time_start .timestamp ())
78- msg = f"{ action = } { repository = } { workflow = } { job_id = } "
79- prune_jobs ()
74+ msg = get_message (action , repository , job_id , workflow )
8075
8176 elif action == "in_progress" :
8277 job_requested = jobs .get (job_id )
@@ -85,7 +80,7 @@ def process_workflow_job():
8580 time_to_start = 0
8681 else :
8782 time_to_start = (time_start - datetime .fromtimestamp (job_requested )).seconds
88- msg = f" { action = } { repository = } { workflow = } { job_id = } { time_to_start = } "
83+ msg = get_message ( action , repository , job_id , time_to_start , workflow )
8984
9085 elif action == "completed" :
9186 job_requested = jobs .get (job_id )
@@ -98,8 +93,11 @@ def process_workflow_job():
9893 ).seconds
9994 # delete from memory
10095 del jobs [job_id ]
101-
102- msg = f"{ action = } { repository = } { workflow = } { job_id = } { time_to_finish = } "
96+ msg = get_message (action , repository , job_id , time_to_finish , workflow )
97+ else :
98+ app .logger .warning (f"Unknown action { action } , removing from memory" )
99+ if job_id in jobs :
100+ del jobs [job_id ]
103101
104102 app .logger .info (msg )
105103 return True
0 commit comments