Skip to content
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
26 changes: 23 additions & 3 deletions front/lib/front/models/workflow.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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? ->
Expand Down Expand Up @@ -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
10 changes: 9 additions & 1 deletion front/lib/front/workflow_page/pipeline_status/model.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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