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

feat: add LANGFLOW_AUTO_SAVING_INTERVAL .env variable for flow auto-saving debounce #3478

Merged
merged 3 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/backend/base/langflow/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ def run(
help="Defines if the auto save is enabled.",
envvar="LANGFLOW_AUTO_SAVING",
),
auto_saving_interval: bool = typer.Option(
True,
help="Defines the debounce time for the auto save.",
envvar="LANGFLOW_AUTO_SAVING_INTERVAL",
),
):
"""
Run Langflow.
Expand All @@ -143,6 +148,7 @@ def run(
components_path=components_path,
store=store,
auto_saving=auto_saving,
auto_saving_interval=auto_saving_interval,
)
# create path object if path is provided
static_files_dir: Optional[Path] = Path(path) if path else None
Expand Down
1 change: 1 addition & 0 deletions src/backend/base/langflow/api/v1/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,3 +331,4 @@ class FlowDataRequest(BaseModel):
class ConfigResponse(BaseModel):
frontend_timeout: int
auto_saving: bool
auto_saving_interval: int
2 changes: 2 additions & 0 deletions src/backend/base/langflow/services/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ class Settings(BaseSettings):
# Config
auto_saving: bool = True
"""If set to True, Langflow will auto save flows."""
auto_saving_interval: int = 300
"""The interval in ms at which Langflow will auto save flows."""

@field_validator("dev")
@classmethod
Expand Down
4 changes: 4 additions & 0 deletions src/backend/base/langflow/utils/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,7 @@ def update_settings(
components_path: Optional[Path] = None,
store: bool = True,
auto_saving: bool = True,
auto_saving_interval: int = 300,
):
"""Update the settings from a config file."""
from langflow.services.utils import initialize_settings_service
Expand All @@ -455,6 +456,9 @@ def update_settings(
if not auto_saving:
logger.debug("Setting auto_saving to False")
settings_service.settings.update_settings(auto_saving=False)
if auto_saving_interval:
logger.debug(f"Setting auto_saving_interval to {auto_saving_interval}")
settings_service.settings.update_settings(auto_saving_interval=auto_saving_interval)


def is_class_method(func, cls):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { UseRequestProcessor } from "../../services/request-processor";
export interface ConfigResponse {
frontend_timeout: number;
auto_saving: boolean;
auto_saving_interval: number;
}

export const useGetConfigQuery: useQueryFunctionType<
Expand Down
6 changes: 4 additions & 2 deletions src/frontend/src/hooks/flows/use-autosave-flow.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { SAVE_DEBOUNCE_TIME } from "@/constants/constants";
import useFlowsManagerStore from "@/stores/flowsManagerStore";
import { FlowType } from "@/types/flow";
import { useDebounce } from "../use-debounce";
Expand All @@ -7,12 +6,15 @@ import useSaveFlow from "./use-save-flow";
const useAutoSaveFlow = () => {
const saveFlow = useSaveFlow();
const autoSaving = useFlowsManagerStore((state) => state.autoSaving);
const autoSavingInterval = useFlowsManagerStore(
(state) => state.autoSavingInterval,
);

const autoSaveFlow = useDebounce((flow?: FlowType) => {
if (autoSaving) {
saveFlow(flow);
}
}, SAVE_DEBOUNCE_TIME);
}, autoSavingInterval);

return autoSaveFlow;
};
Expand Down
4 changes: 4 additions & 0 deletions src/frontend/src/hooks/use-save-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import { useGetConfigQuery } from "../controllers/API/queries/config/use-get-con
function useSaveConfig() {
const { data } = useGetConfigQuery();
const setAutoSaving = useFlowsManagerStore((state) => state.setAutoSaving);
const setAutoSavingInterval = useFlowsManagerStore(
(state) => state.setAutoSavingInterval,
);

useEffect(() => {
if (data) {
Expand All @@ -15,6 +18,7 @@ function useSaveConfig() {
axios.defaults.baseURL = "";
axios.defaults.timeout = timeoutInMilliseconds;
setAutoSaving(data.auto_saving);
setAutoSavingInterval(data.auto_saving_interval);
}
}, [data]);
}
Expand Down
4 changes: 4 additions & 0 deletions src/frontend/src/stores/flowsManagerStore.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { SAVE_DEBOUNCE_TIME } from "@/constants/constants";
import { cloneDeep } from "lodash";
import { create } from "zustand";
import { FlowType } from "../types/flow";
Expand All @@ -18,6 +19,9 @@ const future = {};
const useFlowsManagerStore = create<FlowsManagerStoreType>((set, get) => ({
autoSaving: true,
setAutoSaving: (autoSaving: boolean) => set({ autoSaving }),
autoSavingInterval: SAVE_DEBOUNCE_TIME,
setAutoSavingInterval: (autoSavingInterval: number) =>
set({ autoSavingInterval }),
examples: [],
setExamples: (examples: FlowType[]) => {
set({ examples });
Expand Down
2 changes: 2 additions & 0 deletions src/frontend/src/types/zustand/flowsManager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export type FlowsManagerStoreType = {
searchFlowsComponents: string;
selectedFlowsComponentsCards: string[];
setSelectedFlowsComponentsCards: (selected: string[]) => void;
autoSavingInterval: number;
setAutoSavingInterval: (autoSavingInterval: number) => void;
};

export type UseUndoRedoOptions = {
Expand Down
Loading