2022-06-30 20:45:15 +00:00
|
|
|
import pathlib
|
|
|
|
|
2023-02-01 11:07:00 +00:00
|
|
|
from lightning.app import CloudCompute, LightningApp, LightningFlow, LightningWork
|
|
|
|
from lightning.app.storage.path import _artifacts_path, _filesystem
|
2023-02-17 01:52:46 +00:00
|
|
|
from lightning.app.utilities.enum import WorkStageStatus
|
2022-06-30 20:45:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SourceFileWriterWork(LightningWork):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__(cache_calls=False, parallel=True, cloud_compute=CloudCompute(idle_timeout=5))
|
|
|
|
self.counter = 0
|
|
|
|
self.value = None
|
|
|
|
self.path = None
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
self.path = "lit://boring_file.txt"
|
|
|
|
with open(self.path, "w") as f:
|
|
|
|
f.write("path")
|
|
|
|
self.counter += 1
|
|
|
|
|
|
|
|
|
|
|
|
class DestinationWork(LightningWork):
|
|
|
|
def run(self, path):
|
|
|
|
assert path.exists()
|
|
|
|
|
|
|
|
|
|
|
|
class RootFlow(LightningFlow):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
self.make_check = True
|
|
|
|
self.work = SourceFileWriterWork()
|
|
|
|
self.dest_work = DestinationWork(parallel=True)
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
if self.work.counter == 0:
|
|
|
|
self.work.run()
|
|
|
|
|
2023-02-17 01:52:46 +00:00
|
|
|
elif self.work.status.stage == WorkStageStatus.STOPPED and self.make_check:
|
|
|
|
succeeded_statuses = [status for status in self.work.statuses if status.stage == WorkStageStatus.SUCCEEDED]
|
|
|
|
# Ensure the work succeeded at some point
|
|
|
|
assert len(succeeded_statuses) > 0
|
|
|
|
succeeded_status = succeeded_statuses[-1]
|
|
|
|
|
|
|
|
stopped_statuses = [status for status in self.work.statuses if status.stage == WorkStageStatus.STOPPED]
|
|
|
|
|
|
|
|
# We want to check that the work started shutting down withing the required timeframe, so we take the first
|
|
|
|
# status that has `stage == STOPPED`.
|
|
|
|
stopped_status = stopped_statuses[0]
|
|
|
|
|
2022-06-30 20:45:15 +00:00
|
|
|
# Note: Account for the controlplane, k8s, SIGTERM handler delays.
|
2023-02-17 01:52:46 +00:00
|
|
|
assert (stopped_status.timestamp - succeeded_status.timestamp) < 20
|
|
|
|
|
2022-10-28 13:57:35 +00:00
|
|
|
fs = _filesystem()
|
|
|
|
destination_path = _artifacts_path(self.work) / pathlib.Path(*self.work.path.resolve().parts[1:])
|
2022-06-30 20:45:15 +00:00
|
|
|
assert fs.exists(destination_path)
|
|
|
|
self.dest_work.run(self.work.path)
|
|
|
|
self.make_check = False
|
|
|
|
print("Successfully stopped SourceFileWriterWork.")
|
|
|
|
|
|
|
|
if self.dest_work.status.stage == WorkStageStatus.SUCCEEDED:
|
|
|
|
print("Stopping work")
|
|
|
|
self.dest_work.stop()
|
|
|
|
|
|
|
|
if self.dest_work.status.stage == WorkStageStatus.STOPPED:
|
|
|
|
print(self.dest_work.statuses)
|
|
|
|
print("Application End")
|
2023-01-17 11:06:50 +00:00
|
|
|
self.stop()
|
2022-06-30 20:45:15 +00:00
|
|
|
|
|
|
|
|
2022-11-09 20:46:31 +00:00
|
|
|
app = LightningApp(RootFlow(), log_level="debug")
|