add traceback of each exception handler

This commit is contained in:
zhangxunhui 2021-08-21 12:31:40 +08:00
parent 55fdbe525a
commit f4793b5364
14 changed files with 58 additions and 31 deletions

View File

@ -1,4 +1,4 @@
import sys, pathlib
import sys, pathlib, traceback
sys.path.append(str(pathlib.Path(__file__).resolve().parents[3]))
from sqlalchemy.ext.asyncio import AsyncEngine, create_async_engine
from typing import Optional
@ -14,4 +14,5 @@ class BaseOperator:
url = "mysql+aiomysql://{user}:{password}@{host}:{port}/{db}".format(user=env["MYSQL"]["USER"], password=env["MYSQL"]["PASSWORD"], host=env["MYSQL"]["HOST"], port=env["MYSQL"]["PORT"], db=env["MYSQL"]["DB"])
self.engine = create_async_engine(url, echo=True)
except Exception as e:
print("error with initialization of BaseOperator: %s" % (repr(e)))
print("error with initialization of BaseOperator: %s" % (repr(e)))
print(traceback.format_exc())

View File

@ -1,4 +1,4 @@
import sys, pathlib, asyncio
import sys, pathlib, asyncio, traceback
sys.path.append(str(pathlib.Path(__file__).resolve().parents[3]))
from app.db.operators.base_operator import BaseOperator
@ -32,6 +32,7 @@ class PullRequestOperator(BaseOperator):
return result
except Exception as e:
print("error with func query_pull_request_id: %s" % (repr(e)))
print(traceback.format_exc())
@ -80,6 +81,7 @@ class PullRequestOperator(BaseOperator):
await connection.commit()
except Exception as e:
print("error with func insert_pull_request: %s" % (repr(e)))
print(traceback.format_exc())
async def update_pull_request_comment(self, pr: PullRequest, last_comment_at, comment_or_not:int, comment_id:int, comment_body:str) -> None:
@ -116,6 +118,7 @@ class PullRequestOperator(BaseOperator):
await connection.commit()
except Exception as e:
print("error with func update_pull_request_comment: %s" % (repr(e)))
print(traceback.format_exc())
async def query_prScheduler_4_scheduler(self) -> List[PRScheduler]:
@ -138,6 +141,7 @@ class PullRequestOperator(BaseOperator):
return result
except Exception as e:
print("error with func query_prScheduler_4_scheduler: %s" % (repr(e)))
print(traceback.format_exc())
if __name__ == "__main__":

View File

@ -1,5 +1,5 @@
from configparser import ConfigParser
import re, sys, pathlib, asyncio
import re, sys, pathlib, traceback
sys.path.append(str(pathlib.Path(__file__).resolve().parents[1]))
import json5 as json
from app.services.comments import return_pr_decision
@ -45,4 +45,5 @@ if __name__ == "__main__":
print("serving http on port {0}...".format(str(service_port)))
httpd.serve_forever()
except Exception as e:
print("error with the start of the application in main.py: %s" % (repr(e)))
print("error with the start of the application in main.py: %s" % (repr(e)))
print(traceback.format_exc())

View File

@ -1,5 +1,5 @@
# used to get all factors for prediction
import sys, pathlib, requests, json, datetime
import sys, pathlib, requests, json, datetime, traceback
sys.path.append(str(pathlib.Path(__file__).resolve().parents[2]))
from app.models.pull_request import PullRequest
from app.models.installation import Installation
@ -35,6 +35,7 @@ class FactorGetter():
return int((datetime.datetime.utcnow() - self.pr.created_at).total_seconds() / 60.0)
except Exception as e:
print("error with func lifetime_minutes: %s" % (repr(e)))
print(traceback.format_exc())
def has_comments(self):
try:
@ -48,6 +49,7 @@ class FactorGetter():
print("pause")
except Exception as e:
print("error with func has_comments: %s" % (repr(e)))
print(traceback.format_exc())
def query_pr_infos(self):
try:
@ -356,6 +358,7 @@ class FactorGetter():
except Exception as e:
print("error with func query_pr_infos: %s" % (repr(e)))
print(traceback.format_exc())
if __name__ == "__main__":
factorGetter = FactorGetter(

View File

@ -1,6 +1,6 @@
# predict using the trained model
import sys, pathlib
import sys, pathlib, traceback
from typing import List, Dict
sys.path.append(str(pathlib.Path(__file__).resolve().parents[2]))
@ -34,6 +34,7 @@ class Predictor():
return s
except Exception as e:
print("error with func _factor_cut_suffix: %s" % (repr(e)))
print(traceback.format_exc())
def predict(self, pr: PullRequest, installation: Installation) -> bool:
'''
@ -56,7 +57,10 @@ class Predictor():
if predictions[0] == 1:
return True
else:
elif predictions[0] == 0:
return False
else:
raise Exception("error with the prediction result of func predict.")
except Exception as e:
print("error with func predict: %s" % (repr(e)))
print("error with func predict: %s" % (repr(e)))
print(traceback.format_exc())

View File

@ -8,14 +8,10 @@ sys.path.append(str(pathlib.Path(__file__).resolve().parents[2]))
from typing import Optional, List
from app.utils.config_loader import ConfigLoader
# for training the model
import pickle
import pickle, traceback
import pandas as pd
from sklearn import model_selection
from sklearn.linear_model import LogisticRegression
from app.models.installation import Installation
from app.models.pull_request import PullRequest
from app.models.user import User
from app.models.repository import Repository
class Trainer():
@ -99,6 +95,7 @@ class Trainer():
return model
except Exception as e:
print("error with func _train_one_model: %s" % (repr(e)))
print(traceback.format_exc())
def train(self):
@ -131,6 +128,7 @@ class Trainer():
except Exception as e:
print("error with func train: %s" % (repr(e)))
print(traceback.format_exc())
if __name__ == "__main__":

View File

@ -1,12 +1,12 @@
# this is the authentication service
import sys, time, pathlib
import sys, time, pathlib, traceback
sys.path.append(str(pathlib.Path(__file__).resolve().parents[2]))
import jwt
from app.utils.global_variables import GlobalVariable
from app.models.jwt_query import JWTQuery
from app.models.installation import Installation
from app.services.queries import query_access_token, query_app_id
from app.services.queries import query_access_token
from app.utils.config_loader import ConfigLoader
ALGORITHM = "RS256"
@ -38,6 +38,7 @@ def getToken(installation: Installation) -> str:
except Exception as e:
print("error with func getToken: %s" % repr(e))
print(traceback.format_exc())
finally:
return result

View File

@ -1,5 +1,5 @@
# the services related to labels
import sys, requests, pathlib, json, asyncio, datetime
import sys, requests, pathlib, json, asyncio, datetime, traceback
sys.path.append(str(pathlib.Path(__file__).resolve().parents[2]))
from app.services.authentication import getToken
from app.models.pr_comment import PRComment
@ -8,6 +8,9 @@ from app.db.operators.pull_request_operator import PullRequestOperator
from app.utils.global_variables import GlobalVariable
from app.prediction_service.predictor import Predictor
DECISION_ACCEPT = "This pull request can be merged"
DECISION_REJECT = "This pull request cannot be merged"
def return_pr_decision(prTrigger: PRTrigger) -> bool:
try:
# insert/update db: pull_requests
@ -19,7 +22,7 @@ def return_pr_decision(prTrigger: PRTrigger) -> bool:
# predict the result:
decision = Predictor(trainer=GlobalVariable.trainer, type="submission").predict(pr=prTrigger.pr, installation=prTrigger.installation)
decision_comment = "This pull request can be merged" if decision else "This pull request cannot be merged"
decision_comment = DECISION_ACCEPT if decision else DECISION_REJECT
token = getToken(prTrigger.installation)
comment = PRComment(pr=prTrigger.pr, body=decision_comment)
@ -40,13 +43,14 @@ def return_pr_decision(prTrigger: PRTrigger) -> bool:
loop.run_until_complete(update_pull_request_comment())
except Exception as e:
print("error with func return_pr_decision: %s" % (repr(e)))
print(traceback.format_exc())
async def return_pr_decision_schedular(prSchedulerTrigger:PRSchedulerTrigger) -> bool:
try:
# predict the result:
decision = Predictor(trainer=GlobalVariable.trainer, type="process").predict(pr=prSchedulerTrigger.pr, installation=prSchedulerTrigger.installation)
decision_comment = "Prediction: this pull request can be merged" if decision else "Prediction: this pull request cannot be merged"
decision_comment = DECISION_ACCEPT if decision else DECISION_REJECT
token = getToken(prSchedulerTrigger.installation)
comment = PRComment(pr=prSchedulerTrigger.pr, body=decision_comment)
@ -64,6 +68,7 @@ async def return_pr_decision_schedular(prSchedulerTrigger:PRSchedulerTrigger) ->
await prOp.update_pull_request_comment(pr=prSchedulerTrigger.pr, last_comment_at=datetime.datetime.utcnow(), comment_or_not=1, comment_id=comment_id, comment_body=comment_body)
except Exception as e:
print("error with func return_pr_decision_schedular: %s" % (repr(e)))
print(traceback.format_exc())
if __name__ == "__main__":

View File

@ -2,7 +2,7 @@
import sys, requests, pathlib
sys.path.append(str(pathlib.Path(__file__).resolve().parents[2]))
import json, time, jwt
import json, time, jwt, traceback
from typing import Dict, List
from app.models.installation import Installation
@ -52,6 +52,7 @@ def query_installations() -> Dict:
page += 1
except Exception as e:
print("error with func query_installations: %s" % (repr(e)))
print(traceback.format_exc())
finally:
return result
@ -66,6 +67,7 @@ def query_access_token(query: JWTQuery) -> str:
result = response.json()["token"]
except Exception as e:
print("error with func query_access_token: %s" % (repr(e)))
print(traceback.format_exc())
finally:
return result
@ -83,4 +85,5 @@ def query_app_id() -> int:
result = response.json()["id"]
return result
except Exception as e:
print("error with func query_app_id: %s" % (repr(e)))
print("error with func query_app_id: %s" % (repr(e)))
print(traceback.format_exc())

View File

@ -1,5 +1,4 @@
from app.models.jwt_query import JWTQuery
import sys, pathlib, asyncio, os
import sys, pathlib, asyncio, traceback
sys.path.append(str(pathlib.Path(__file__).resolve().parents[2]))
from apscheduler.schedulers.background import BackgroundScheduler
from app.db.operators.pull_request_operator import PullRequestOperator
@ -22,6 +21,7 @@ class Scheduler():
print("the schedular is started.")
except Exception as e:
print("error with the initialization of Scheduler: %s" % (repr(e)))
print(traceback.format_exc())
def job_make_decision(self) -> None:
try:
@ -37,4 +37,5 @@ class Scheduler():
# if user remove the installation, we also need to remove it......
asyncio.run(return_pr_decision_schedular(PRSchedulerTrigger(installation=installationDict[task.pr.owner.login], pr=task.pr)))
except Exception as e:
print("error with func job_make_decision: %s" % (repr(e)))
print("error with func job_make_decision: %s" % (repr(e)))
print(traceback.format_exc())

View File

@ -1,6 +1,5 @@
# handle triggers
from gettext import install
import sys, pathlib, asyncio
import sys, pathlib, asyncio, traceback
sys.path.append(str(pathlib.Path(__file__).resolve().parents[2]))
from app.models.trigger import *
@ -16,6 +15,8 @@ def parseTriggers(response: Dict) -> Trigger:
try:
if 'repository' not in response or "installation" not in response:
return None # this is not a right trigger, e.g., the trigger will also be touched when install the app
# check whether the permissions have been admitted by the installation
repo = Repository(name=response['repository']['name'])
owner = User(login=response['repository']['full_name'].split("/")[0])
sender = User(login=response['sender']['login'])
@ -36,4 +37,5 @@ def parseTriggers(response: Dict) -> Trigger:
prTrigger = PRTrigger(installation=installation, repo=repo, sender=sender, pr=pr, action=response['action'])
return prTrigger
except Exception as e:
print("error with func parseTriggers: %s" % (repr(e)))
print("error with func parseTriggers: %s" % (repr(e)))
print(traceback.format_exc())

View File

@ -1,5 +1,5 @@
from typing import Dict
import yaml, sys, pathlib
import yaml, sys, pathlib, traceback
sys.path.append(str(pathlib.Path(__file__).resolve().parents[2]))
class ConfigLoader():
@ -11,6 +11,7 @@ class ConfigLoader():
result = yaml.load(f, Loader=yaml.FullLoader)
except Exception as e:
print("error with func load_env: %s" % (repr(e)))
print(traceback.format_exc())
finally:
return result
@ -21,5 +22,6 @@ class ConfigLoader():
result = yaml.load(f, Loader=yaml.FullLoader)
except Exception as e:
print("error with func load_prediction_service_config: %s" % (repr(e)))
print(traceback.format_exc())
finally:
return result

View File

@ -1,4 +1,4 @@
import datetime
import datetime, traceback
class TimeOperator():
@ -9,5 +9,6 @@ class TimeOperator():
result = datetime.datetime.strptime(utc, UTC_FORMAT)
except Exception as e:
print('error with func convertTZTime2TimeStamp: %s' % (repr(e)))
print(traceback.format_exc())
finally:
return result

View File

@ -1,5 +1,5 @@
from typing import List
import re
import re, traceback
class WordCounter():
@ -13,4 +13,5 @@ class WordCounter():
result += len(re.findall(r'\w+', s))
return result
except Exception as e:
print("error with func count: %s" % (repr(e)))
print("error with func count: %s" % (repr(e)))
print(traceback.format_exc())