Skip to content

Commit f690e89

Browse files
authored
feat(front): add finite cache ttl for running pipelines (#515)
## πŸ“ Description <!-- Describe your changes in detail --> ## βœ… Checklist - [ ] I have tested this change - [ ] This change requires documentation update
1 parent 65d4675 commit f690e89

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

β€Žfront/lib/front/models/workflow.exβ€Ž

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ defmodule Front.Models.Workflow do
115115
nil
116116

117117
response ->
118-
Cacheman.put(:front, cache_key, encode(response))
118+
ttl = get_ttl_for_workflow(response)
119+
Cacheman.put(:front, cache_key, encode(response), ttl: ttl)
119120
response
120121
end
121122
end
@@ -137,7 +138,8 @@ defmodule Front.Models.Workflow do
137138
non_cached_workflows = find_many(non_cached_ids, :from_api)
138139

139140
Enum.each(non_cached_workflows, fn workflow ->
140-
Cacheman.put(:front, workflow_cache_key(workflow.id), encode(workflow))
141+
ttl = get_ttl_for_workflow(workflow)
142+
Cacheman.put(:front, workflow_cache_key(workflow.id), encode(workflow), ttl: ttl)
141143
end)
142144

143145
cached_workflows ++ non_cached_workflows
@@ -350,7 +352,8 @@ defmodule Front.Models.Workflow do
350352
requester_id: workflow.requester_id,
351353
created_at: DateTime.from_unix!(workflow.created_at.seconds),
352354
triggered_by: TriggeredBy.key(workflow.triggered_by),
353-
rerun_of: workflow.rerun_of
355+
rerun_of: workflow.rerun_of,
356+
pipelines: []
354357
}
355358
|> then(fn
356359
workflow when preload? ->
@@ -526,4 +529,21 @@ defmodule Front.Models.Workflow do
526529

527530
workflow
528531
end
532+
533+
defp has_active_pipelines?(workflow) do
534+
workflow.pipelines
535+
|> Enum.any?(fn pipeline ->
536+
pipeline.state in [:RUNNING, :STOPPING, :PENDING, :QUEUING, :INITIALIZING]
537+
end)
538+
end
539+
540+
defp get_ttl_for_workflow(workflow) do
541+
if has_active_pipelines?(workflow) do
542+
# Finite cache for workflows with active pipelines
543+
:timer.hours(2)
544+
else
545+
# Never expire for completed workflows
546+
:infinity
547+
end
548+
end
529549
end

β€Žfront/lib/front/workflow_page/pipeline_status/model.exβ€Ž

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ defmodule Front.WorkflowPage.PipelineStatus.Model do
1111
def load(pipeline_id) do
1212
Cacheman.fetch(:front, cache_key(pipeline_id), fn ->
1313
pipeline_status = load_pipeline_status_from_api(pipeline_id)
14-
Cacheman.put(:front, cache_key(pipeline_id), pipeline_status)
14+
ttl = get_ttl_for_status(pipeline_status)
15+
Cacheman.put(:front, cache_key(pipeline_id), pipeline_status, ttl: ttl)
1516
{:ok, pipeline_status}
1617
end)
1718
end
@@ -36,4 +37,11 @@ defmodule Front.WorkflowPage.PipelineStatus.Model do
3637
defp pipeline_favicon_status(:DONE, :STOPPED), do: "stopped"
3738
defp pipeline_favicon_status(:DONE, :CANCELED), do: "canceled"
3839
defp pipeline_favicon_status(_, _), do: "pending"
40+
41+
# Running pipelines should be cached for a limited time
42+
defp get_ttl_for_status(status) when status in ["running", "stopping", "pending"],
43+
do: :timer.minutes(15)
44+
45+
# Use default - infinite cache for completed pipelines
46+
defp get_ttl_for_status(_status), do: :infinity
3947
end

0 commit comments

Comments
Β (0)