-
-
Notifications
You must be signed in to change notification settings - Fork 646
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
add lifecycle handlers to allow extensions to hook session lifecycle #10988
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ | |
import logging | ||
import os | ||
from dataclasses import dataclass, replace | ||
from typing import Mapping, Optional, Tuple | ||
from typing import List, Mapping, Optional, Tuple | ||
|
||
from pants.base.build_environment import get_buildroot | ||
from pants.base.exception_sink import ExceptionSink | ||
|
@@ -13,6 +13,7 @@ | |
from pants.base.specs_parser import SpecsParser | ||
from pants.base.workunit import WorkUnit | ||
from pants.build_graph.build_configuration import BuildConfiguration | ||
from pants.build_graph.lifecycle import SessionLifecycleHandler | ||
from pants.core.util_rules.pants_environment import PantsEnvironment | ||
from pants.engine.internals.native import Native | ||
from pants.engine.internals.scheduler import ExecutionError | ||
|
@@ -55,6 +56,7 @@ class LocalPantsRunner: | |
union_membership: UnionMembership | ||
profile_path: Optional[str] | ||
_run_tracker: RunTracker | ||
_session_lifecycle_handlers: List[SessionLifecycleHandler] | ||
|
||
@classmethod | ||
def parse_options( | ||
|
@@ -162,6 +164,16 @@ def create( | |
|
||
profile_path = env.get("PANTS_PROFILE") | ||
|
||
# Query registered extension lifecycle handlers to see if any wish to receive lifecycle events | ||
# for this session. | ||
session_lifecycle_handlers = [] | ||
for lifecycle_handler in build_config.lifecycle_handlers: | ||
session_lifecycle_handler = lifecycle_handler.on_session_create( | ||
build_root=build_root, options=options, specs=specs | ||
) | ||
if session_lifecycle_handler: | ||
session_lifecycle_handlers.append(session_lifecycle_handler) | ||
Comment on lines
+171
to
+175
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. See |
||
|
||
return cls( | ||
build_root=build_root, | ||
options=options, | ||
|
@@ -170,6 +182,7 @@ def create( | |
graph_session=graph_session, | ||
union_membership=union_membership, | ||
profile_path=profile_path, | ||
_session_lifecycle_handlers=session_lifecycle_handlers, | ||
_run_tracker=RunTracker.global_instance(), | ||
) | ||
|
||
|
@@ -274,10 +287,19 @@ def run(self, start_time: float) -> ExitCode: | |
return help_printer.print_help() | ||
|
||
with streaming_reporter.session(): | ||
# Invoke the session lifecycle handlers, if any. | ||
for session_lifecycle_handler in self._session_lifecycle_handlers: | ||
session_lifecycle_handler.on_session_start() | ||
|
||
engine_result = PANTS_FAILED_EXIT_CODE | ||
try: | ||
engine_result = self._run_v2() | ||
except Exception as e: | ||
ExceptionSink.log_exception(e) | ||
run_tracker_result = self._finish_run(engine_result) | ||
|
||
# Invoke the session lifecycle handlers, if any. | ||
for session_lifecycle_handler in self._session_lifecycle_handlers: | ||
session_lifecycle_handler.on_session_end(engine_result=engine_result) | ||
|
||
return self._merge_exit_codes(engine_result, run_tracker_result) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Copyright 2020 Pants project contributors (see CONTRIBUTORS.md). | ||
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. Should probably rename the file to |
||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
from typing import Optional | ||
|
||
from pants.base.exiter import ExitCode | ||
from pants.base.specs import Specs | ||
from pants.option.options import Options | ||
|
||
|
||
class SessionLifecycleHandler: | ||
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. If you envision multiple types of I think you can delete |
||
def on_session_start(self): | ||
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. A basic docstring would be helpful for both of these. Even though we're not documenting on the website yet, it will help future contributors to understand what's going on. 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. It might be fine to call these That will make it easier for us to factor the methods up into the general |
||
pass | ||
|
||
def on_session_end(self, *, engine_result: ExitCode): | ||
pass | ||
|
||
|
||
class ExtensionLifecycleHandler: | ||
# Returns a SessionLifecycleHandler that will receive lifecycle events for the session. | ||
def on_session_create( | ||
self, *, build_root: str, options: Options, specs: Specs | ||
) -> Optional[SessionLifecycleHandler]: | ||
pass | ||
Comment on lines
+20
to
+23
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. I think you can move this into Also it should be a classmethod, as it's a factory method to create a new instance. |
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.
This should probably be
Tuple[SessionLifecycleHandler, ...]
so that it's hashable. Even though the dataclass isn't frozen, we always bias towards immutability.