diff --git a/front/lib/front/models/workflow.ex b/front/lib/front/models/workflow.ex index 851e7ce04..87f5ba7fe 100644 --- a/front/lib/front/models/workflow.ex +++ b/front/lib/front/models/workflow.ex @@ -115,7 +115,8 @@ defmodule Front.Models.Workflow do nil response -> - Cacheman.put(:front, cache_key, encode(response)) + ttl = get_ttl_for_workflow(response) + Cacheman.put(:front, cache_key, encode(response), ttl: ttl) response end end @@ -137,7 +138,8 @@ defmodule Front.Models.Workflow do non_cached_workflows = find_many(non_cached_ids, :from_api) Enum.each(non_cached_workflows, fn workflow -> - Cacheman.put(:front, workflow_cache_key(workflow.id), encode(workflow)) + ttl = get_ttl_for_workflow(workflow) + Cacheman.put(:front, workflow_cache_key(workflow.id), encode(workflow), ttl: ttl) end) cached_workflows ++ non_cached_workflows @@ -350,7 +352,8 @@ defmodule Front.Models.Workflow do requester_id: workflow.requester_id, created_at: DateTime.from_unix!(workflow.created_at.seconds), triggered_by: TriggeredBy.key(workflow.triggered_by), - rerun_of: workflow.rerun_of + rerun_of: workflow.rerun_of, + pipelines: [] } |> then(fn workflow when preload? -> @@ -526,4 +529,21 @@ defmodule Front.Models.Workflow do workflow end + + defp has_active_pipelines?(workflow) do + workflow.pipelines + |> Enum.any?(fn pipeline -> + pipeline.state in [:RUNNING, :STOPPING, :PENDING, :QUEUING, :INITIALIZING] + end) + end + + defp get_ttl_for_workflow(workflow) do + if has_active_pipelines?(workflow) do + # Finite cache for workflows with active pipelines + :timer.hours(2) + else + # Never expire for completed workflows + :infinity + end + end end diff --git a/front/lib/front/workflow_page/pipeline_status/model.ex b/front/lib/front/workflow_page/pipeline_status/model.ex index c7e25384d..5ffcf86da 100644 --- a/front/lib/front/workflow_page/pipeline_status/model.ex +++ b/front/lib/front/workflow_page/pipeline_status/model.ex @@ -11,7 +11,8 @@ defmodule Front.WorkflowPage.PipelineStatus.Model do def load(pipeline_id) do Cacheman.fetch(:front, cache_key(pipeline_id), fn -> pipeline_status = load_pipeline_status_from_api(pipeline_id) - Cacheman.put(:front, cache_key(pipeline_id), pipeline_status) + ttl = get_ttl_for_status(pipeline_status) + Cacheman.put(:front, cache_key(pipeline_id), pipeline_status, ttl: ttl) {:ok, pipeline_status} end) end @@ -36,4 +37,11 @@ defmodule Front.WorkflowPage.PipelineStatus.Model do defp pipeline_favicon_status(:DONE, :STOPPED), do: "stopped" defp pipeline_favicon_status(:DONE, :CANCELED), do: "canceled" defp pipeline_favicon_status(_, _), do: "pending" + + # Running pipelines should be cached for a limited time + defp get_ttl_for_status(status) when status in ["running", "stopping", "pending"], + do: :timer.minutes(15) + + # Use default - infinite cache for completed pipelines + defp get_ttl_for_status(_status), do: :infinity end