-
Notifications
You must be signed in to change notification settings - Fork 2
Refactor collect and send algorithm #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
14646fc
5d0cd81
74f2ec3
016c42a
f5b8708
3d83f2a
ebac11d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,33 +11,31 @@ | |
|
||
|
||
def add_source_code(source_path, source_code_dict, source_path_dict, line): | ||
try: | ||
the_id = source_path_dict[source_path] | ||
except KeyError: | ||
the_id = str(uuid.uuid4()) | ||
source_path_dict[source_path] = the_id | ||
source_code_dict[the_id] = { | ||
|
||
if source_path in source_path_dict: | ||
source_code_info = source_code_dict[source_path] | ||
if line < source_code_info["minLine"]: | ||
source_code_info["minLine"] = line | ||
if line > source_code_info["maxLine"]: | ||
source_code_info["maxLine"] = line | ||
|
||
else: | ||
source_code_dict[source_path] = { | ||
"minLine": line, | ||
"maxLine": line, | ||
"path": source_path, | ||
} | ||
return the_id | ||
|
||
if line < source_code_dict[the_id]["minLine"]: | ||
source_code_dict[the_id]["minLine"] = line | ||
if line > source_code_dict[the_id]["maxLine"]: | ||
source_code_dict[the_id]["maxLine"] = line | ||
return the_id | ||
return source_path | ||
|
||
|
||
def process_frame(tb_frame, line, source_code_dict, source_path_dict): | ||
source_file = os.path.abspath(tb_frame.f_code.co_filename) | ||
frame = { | ||
"funcName": tb_frame.f_code.co_name, | ||
"line": line, | ||
"sourceCode": add_source_code( | ||
source_file, source_code_dict, source_path_dict, line | ||
), | ||
"library": source_file, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe it would be better to use filename here only? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The source code integration (with the source code service and source code injection in the client side) won't work |
||
"sourceCode": source_file, | ||
} | ||
return frame | ||
|
||
|
@@ -70,6 +68,7 @@ def __init__(self): | |
self.source_code = {} | ||
self.source_path_dict = {} | ||
entry_source_code_id = None | ||
self.attachments = [] | ||
import __main__ | ||
|
||
cwd_path = os.path.abspath(os.getcwd()) | ||
|
@@ -155,9 +154,18 @@ def log(self, line): | |
} | ||
) | ||
|
||
def add_attachment(self, attachment_path): | ||
self.attachments.append(attachment_path) | ||
|
||
def get_attachments(self): | ||
return self.attachments | ||
|
||
def get_data(self): | ||
return self.report | ||
|
||
def send(self): | ||
if len(self.log_lines) != 0 and "Log" not in self.report["annotations"]: | ||
self.report["annotations"]["Log"] = self.log_lines | ||
from backtracepython.client import send_worker_report | ||
from backtracepython.client import send | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we use global imports? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unfortunately, this would require breaking the existing API. Our current interface allows the record to be sent directly via the send method. Since client has to have a reference to report (creating a report on unhandled exception or sending a message), and report has a reference to the client due to the send method, we have a circular reference |
||
|
||
send_worker_report(self.report, self.source_code) | ||
send(self) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import sys | ||
import threading | ||
|
||
if sys.version_info.major >= 3: | ||
import queue | ||
else: | ||
import Queue as queue | ||
|
||
|
||
class ReportQueue: | ||
def __init__(self, request_handler, source_code_handler): | ||
self.request_handler = request_handler | ||
self.source_code_handler = source_code_handler | ||
|
||
# report submission tasks queue | ||
self.report_queue = queue.Queue() | ||
|
||
# Create and start a single worker thread | ||
self.worker_thread = threading.Thread(target=self._worker) | ||
self.worker_thread.daemon = True | ||
self.active = True | ||
self.worker_thread.start() | ||
|
||
def _worker(self): | ||
while True: | ||
report_data = self.report_queue.get() | ||
if report_data is None or self.active == False: | ||
self.report_queue.task_done() | ||
break | ||
report, attachments = report_data | ||
self.process(report, attachments) | ||
self.report_queue.task_done() | ||
|
||
def add(self, report, attachments): | ||
self.report_queue.put((report, attachments)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you'll want to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that's true. Changing it. Thanks |
||
|
||
# Immediately process the report and skip the queue process | ||
# Use this method to handle importa data before application exit | ||
def process(self, report, attachments): | ||
self.source_code_handler.collect(report) | ||
self.request_handler.send(report, attachments) | ||
|
||
def __del__(self): | ||
self.dispose() | ||
|
||
def dispose(self): | ||
# Put a sentinel value to stop the worker thread | ||
self.active = False | ||
self.report_queue.put_nowait(None) | ||
self.report_queue.join() | ||
self.worker_thread.join() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
instead of using
kwargs
, why not define the arguments directly with default values?e.g.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will handle it in a different pull request to reduce the scope of this change