Skip to content

Commit e54f4f5

Browse files
authored
Fix detection of whether app is running in cloud (#16045)
1 parent 22b254f commit e54f4f5

File tree

4 files changed

+23
-10
lines changed

4 files changed

+23
-10
lines changed

src/lightning_app/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
2121

2222
### Changed
2323

24-
-
24+
25+
- The utility `lightning.app.utilities.cloud.is_running_in_cloud` now returns `True` during loading of the app locally when running with `--cloud` ([#16045](https://github.com/Lightning-AI/lightning/pull/16045))
2526

2627

2728
### Deprecated

src/lightning_app/runners/cloud.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import fnmatch
22
import json
3+
import os
34
import random
45
import re
56
import string
@@ -589,6 +590,10 @@ def _project_has_sufficient_credits(self, project: V1Membership, app: Optional[L
589590
@classmethod
590591
def load_app_from_file(cls, filepath: str) -> "LightningApp":
591592
"""Load a LightningApp from a file, mocking the imports."""
593+
594+
# Pretend we are running in the cloud when loading the app locally
595+
os.environ["LAI_RUNNING_IN_CLOUD"] = "1"
596+
592597
try:
593598
app = load_app_from_file(filepath, raise_exception=True, mock_imports=True)
594599
except FileNotFoundError as e:
@@ -599,6 +604,8 @@ def load_app_from_file(cls, filepath: str) -> "LightningApp":
599604
# Create a generic app.
600605
logger.info("Could not load the app locally. Starting the app directly on the cloud.")
601606
app = LightningApp(EmptyFlow())
607+
finally:
608+
del os.environ["LAI_RUNNING_IN_CLOUD"]
602609
return app
603610

604611
@staticmethod

src/lightning_app/utilities/cloud.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,4 @@ def _sigterm_flow_handler(*_, app: "lightning_app.LightningApp"):
3939

4040
def is_running_in_cloud() -> bool:
4141
"""Returns True if the Lightning App is running in the cloud."""
42-
return "LIGHTNING_APP_STATE_URL" in os.environ
42+
return bool(int(os.environ.get("LAI_RUNNING_IN_CLOUD", "0"))) or "LIGHTNING_APP_STATE_URL" in os.environ

tests/tests_app/utilities/test_cloud.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,18 @@
44
from lightning_app.utilities.cloud import is_running_in_cloud
55

66

7-
@mock.patch.dict(os.environ, clear=True)
8-
def test_is_running_locally():
9-
"""We can determine if Lightning is running locally."""
10-
assert not is_running_in_cloud()
11-
12-
13-
@mock.patch.dict(os.environ, {"LIGHTNING_APP_STATE_URL": "127.0.0.1"})
147
def test_is_running_cloud():
158
"""We can determine if Lightning is running in the cloud."""
16-
assert is_running_in_cloud()
9+
with mock.patch.dict(os.environ, {}, clear=True):
10+
assert not is_running_in_cloud()
11+
12+
with mock.patch.dict(os.environ, {"LAI_RUNNING_IN_CLOUD": "0"}, clear=True):
13+
assert not is_running_in_cloud()
14+
15+
# in the cloud, LIGHTNING_APP_STATE_URL is defined
16+
with mock.patch.dict(os.environ, {"LIGHTNING_APP_STATE_URL": "defined"}, clear=True):
17+
assert is_running_in_cloud()
18+
19+
# LAI_RUNNING_IN_CLOUD is used to fake the value of `is_running_in_cloud` when loading the app for --cloud
20+
with mock.patch.dict(os.environ, {"LAI_RUNNING_IN_CLOUD": "1"}):
21+
assert is_running_in_cloud()

0 commit comments

Comments
 (0)