Skip to content
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

VSCode Logging #506

Open
wants to merge 61 commits into
base: main
Choose a base branch
from
Open

VSCode Logging #506

wants to merge 61 commits into from

Conversation

mj023
Copy link

@mj023 mj023 commented Nov 29, 2023

Add additional logging for the VSCode Extension.

Changes

The logging information will be in JSON-Format and is sent to a port that is specified by the user, by setting an environment variable. If the environment variable is not set, no logging will happen. The communication happens in a new thread so that pytask is not blocked. Deamon Threads are not used, because we do not want the threads to be killed when the main pytask program ends, as this can result in them not being able to send the info if pytask finishes very quickly.

Info the extension needs

  • Collection: The ExitCode of the Collection and a list of the collected tasks with the name and path of the task.
    Example: {exitcode: "OK", tasks: [ {name: "task1", path: "C:\Documents\task_task1.py"}, ...]}

  • Execution: Every time a task finishes, the name, the outcome and if the task failed also the execution info eg. the error message.
    Example: {name: "task1", outcome: "FAIL", exc_info: "Rendered String of Error message"}

Copy link

codecov bot commented Dec 6, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.47%. Comparing base (ad3680e) to head (e5f1f99).
Report is 69 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #506      +/-   ##
==========================================
- Coverage   97.80%   97.47%   -0.34%     
==========================================
  Files         106      113       +7     
  Lines        8710     9411     +701     
==========================================
+ Hits         8519     9173     +654     
- Misses        191      238      +47     
Flag Coverage Δ
end_to_end 84.58% <86.79%> (+2.30%) ⬆️
integration 40.73% <43.39%> (+0.24%) ⬆️
unit 65.58% <64.15%> (-4.11%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@mj023
Copy link
Author

mj023 commented Dec 8, 2023

I added the logging code for the task execution to the hook in live.py, but this seems a bit odd considering the project structure. I could move it to a new implementation of the hook in logging.py that uses tryfirst or implement the hook a second time in execute.py, also with tryfirst. What do you think, @tobiasraabe ?

@tobiasraabe
Copy link
Member

tobiasraabe commented Dec 8, 2023

Hi @mj023, thanks for your effort!

  • Let us refactor the code related to the vscode extension to its own module. Could you create a new module called vscode.py and implement the two hooks there? To register the module, go to cli.py and see the hook implementation pytask_add_hooks. Play around with @hookimpl(tryfirst=True) if your hook specification is not called. As long as the function do not return anything, the following hook implementation will be executed.
  • Can we guard the execution in the two hooks somehow? I do not want the code executed when the plugin is not installed. What options do we have here?
  • It would be great if we wouldn't need another dependency. how about using urllib from the standard library?

for task in tasks
]
thread = Thread(
target=send_logging_vscode,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check out the daemon keyword for threads and see whether it might be helpful in our situation.

@@ -168,6 +171,27 @@ def pytask_execute_task_log_start(self, task: PTask) -> bool:
@hookimpl
def pytask_execute_task_log_end(self, report: ExecutionReport) -> bool:
"""Mark a task as being finished and update outcome."""
if report.outcome == TaskOutcome.FAIL and report.exc_info is not None:
with console.capture() as capture:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check out render_to_string from console.py

@mj023
Copy link
Author

mj023 commented Dec 8, 2023

  • Okay, I'll add a new module then.
  • The first solution to the guarding problem I can think of, would be to add this as an option in the pytask config that I can activate with a cli argument. But I will see if I can check whether the process that calls the script is VS Code or if I can maybe set some environment variable before running pytask.
  • I'll try to change it to the standard urllib, requests was just easier to use.

@mj023
Copy link
Author

mj023 commented Feb 7, 2024

I think everything should be ready now, but I'm having a little trouble writing the tests. I have moved the code to a new vscode.py module, replaced the requests library with the standard urllib and added a guard so the code is only executed if a specific environment variable is set. I then added two new tests, so that almost everything I added is covered, but I'm not sure if I did that correctly. Furthermore the tests on ubuntu 3.12 now failed and I can not figure out why.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants