Skip to content

Commit 67a5e08

Browse files
dmitsflantiga
authored andcommitted
Feat(plugins): adding KeepMachinesAfterStop flag propagation (#19660)
* Feat(plugins): adding KeepMachinesAfterStop flag propagation
1 parent 78265c6 commit 67a5e08

File tree

4 files changed

+19
-1
lines changed

4 files changed

+19
-1
lines changed

requirements/app/app.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
lightning-cloud == 0.5.64 # Must be pinned to ensure compatibility
1+
lightning-cloud == 0.5.65 # Must be pinned to ensure compatibility
22
packaging
33
typing-extensions >=4.4.0, <4.10.0
44
deepdiff >=5.7.0, <6.6.0

src/lightning/app/plugin/plugin.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ def __init__(self) -> None:
4646
self.cloudspace_id = ""
4747
self.cluster_id = ""
4848
self.source_app = ""
49+
self.keep_machines_after_stop = False
4950

5051
def run(self, *args: str, **kwargs: str) -> Externalv1LightningappInstance:
5152
"""Override with the logic to execute on the cloudspace."""
@@ -95,6 +96,7 @@ def run_job(self, name: str, app_entrypoint: str, env_vars: Dict[str, str] = {})
9596
name=name,
9697
cluster_id=self.cluster_id,
9798
source_app=self.source_app,
99+
keep_machines_after_stop=self.keep_machines_after_stop,
98100
)
99101

100102
def _setup(
@@ -103,11 +105,13 @@ def _setup(
103105
cloudspace_id: str,
104106
cluster_id: str,
105107
source_app: str,
108+
keep_machines_after_stop: bool,
106109
) -> None:
107110
self.source_app = source_app
108111
self.project_id = project_id
109112
self.cloudspace_id = cloudspace_id
110113
self.cluster_id = cluster_id
114+
self.keep_machines_after_stop = keep_machines_after_stop
111115

112116

113117
class _Run(BaseModel):
@@ -118,6 +122,7 @@ class _Run(BaseModel):
118122
cluster_id: str
119123
plugin_arguments: Dict[str, str]
120124
source_app: str
125+
keep_machines_after_stop: bool
121126

122127

123128
def _run_plugin(run: _Run) -> Dict[str, Any]:
@@ -191,6 +196,7 @@ def _run_plugin(run: _Run) -> Dict[str, Any]:
191196
cloudspace_id=run.cloudspace_id,
192197
cluster_id=run.cluster_id,
193198
source_app=run.source_app,
199+
keep_machines_after_stop=run.keep_machines_after_stop,
194200
)
195201
app_instance = plugin.run(**run.plugin_arguments)
196202
return _to_clean_dict(app_instance, True)

src/lightning/app/runners/cloud.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ def cloudspace_dispatch(
197197
name: str,
198198
cluster_id: str,
199199
source_app: Optional[str] = None,
200+
keep_machines_after_stop: Optional[bool] = None,
200201
) -> Externalv1LightningappInstance:
201202
"""Slim dispatch for creating runs from a cloudspace. This dispatch avoids resolution of some properties such
202203
as the project and cluster IDs that are instead passed directly.
@@ -206,6 +207,8 @@ def cloudspace_dispatch(
206207
cloudspace_id: The ID of the cloudspace.
207208
name: The name for the run.
208209
cluster_id: The ID of the cluster to run on.
210+
source_app: Name of the source app that triggered the run.
211+
keep_machines_after_stop: If true, machines will be left running after the run is finished and reused after
209212
210213
Raises:
211214
ApiException: If there was an issue in the backend.
@@ -278,6 +281,7 @@ def cloudspace_dispatch(
278281
queue_server_type,
279282
env_vars,
280283
source_app=source_app,
284+
keep_machines_after_stop=keep_machines_after_stop,
281285
)
282286

283287
def dispatch(
@@ -1016,6 +1020,7 @@ def _api_create_run_instance(
10161020
env_vars: Optional[List[V1EnvVar]] = None,
10171021
auth: Optional[V1LightningAuth] = None,
10181022
source_app: Optional[str] = None,
1023+
keep_machines_after_stop: Optional[bool] = None,
10191024
) -> Externalv1LightningappInstance:
10201025
"""Create a new instance of the given run with the given specification."""
10211026
return self.backend.client.cloud_space_service_create_lightning_run_instance(
@@ -1030,6 +1035,7 @@ def _api_create_run_instance(
10301035
env=env_vars,
10311036
auth=auth,
10321037
source_app=source_app,
1038+
keep_machines_after_stop=keep_machines_after_stop,
10331039
),
10341040
)
10351041

tests/tests_app/plugin/test_plugin.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ def run(self):
8585
cluster_id="any",
8686
plugin_arguments={},
8787
source_app="any",
88+
keep_machines_after_stop=False,
8889
),
8990
"Error downloading plugin source:",
9091
None,
@@ -99,6 +100,7 @@ def run(self):
99100
cluster_id="any",
100101
plugin_arguments={},
101102
source_app="any",
103+
keep_machines_after_stop=False,
102104
),
103105
"Error extracting plugin source:",
104106
None,
@@ -113,6 +115,7 @@ def run(self):
113115
cluster_id="any",
114116
plugin_arguments={},
115117
source_app="any",
118+
keep_machines_after_stop=False,
116119
),
117120
"Error loading plugin:",
118121
"plugin.py",
@@ -127,6 +130,7 @@ def run(self):
127130
cluster_id="any",
128131
plugin_arguments={},
129132
source_app="any",
133+
keep_machines_after_stop=False,
130134
),
131135
"Error running plugin:",
132136
"plugin.py",
@@ -175,6 +179,7 @@ def test_run_job(mock_requests, mock_cloud_runtime, mock_cloud_backend, mock_plu
175179
cluster_id="test_cluster_id",
176180
plugin_arguments={"name": "test_name", "entrypoint": "test_entrypoint"},
177181
source_app="test_source_app",
182+
keep_machines_after_stop=True,
178183
)
179184

180185
mock_app = mock.MagicMock()
@@ -207,6 +212,7 @@ def test_run_job(mock_requests, mock_cloud_runtime, mock_cloud_backend, mock_plu
207212
name="test_name",
208213
cluster_id=body.cluster_id,
209214
source_app=body.source_app,
215+
keep_machines_after_stop=body.keep_machines_after_stop,
210216
)
211217

212218

0 commit comments

Comments
 (0)