Skip to content

Commit 688837f

Browse files
authored
Fix cache warmer for long running tasks (#403)
1 parent 5e26934 commit 688837f

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

lib/cachex/actions/warm.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ defmodule Cachex.Actions.Warm do
3131
warmers
3232
|> Enum.filter(&filter_mod(&1, only))
3333
|> Enum.map(&spawn_call(&1, wait))
34-
|> Task.yield_many()
34+
|> Task.yield_many(:infinity)
3535
|> Enum.map(&extract_name/1)
3636

3737
{:ok, warmed}

test/cachex/warmer_test.exs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,50 @@ defmodule Cachex.WarmerTest do
1414
assert Cachex.get!(cache, 1) == 1
1515
end
1616

17+
test "warmers with long running tasks" do
18+
# create a test warmer to pass to the cache
19+
TestUtils.create_warmer(:long_running_warmer, fn _ ->
20+
# exceed default timeout of `5000`
21+
Process.sleep(5001)
22+
{:ok, [{1, 1}]}
23+
end)
24+
25+
# create a cache instance with a warmer
26+
cache =
27+
TestUtils.create_cache(warmers: [warmer(module: :long_running_warmer)])
28+
29+
# check that the key was warmed
30+
assert Cachex.get!(cache, 1) == 1
31+
end
32+
33+
test "warmers with long running async tasks" do
34+
# create a test warmer to pass to the cache
35+
TestUtils.create_warmer(:long_running_async_warmer, fn _ ->
36+
{:ok,
37+
[1, 2]
38+
|> Task.async_stream(
39+
fn num ->
40+
# exceed default timeout of `5000`
41+
Process.sleep(5001)
42+
{num, num}
43+
end,
44+
timeout: :infinity
45+
)
46+
|> Stream.map(fn {:ok, result} -> result end)
47+
|> Enum.to_list()}
48+
end)
49+
50+
# create a cache instance with a warmer
51+
cache =
52+
TestUtils.create_cache(
53+
warmers: [warmer(module: :long_running_async_warmer)]
54+
)
55+
56+
# check that the keys were warmed
57+
assert Cachex.get!(cache, 1) == 1
58+
assert Cachex.get!(cache, 2) == 2
59+
end
60+
1761
test "warmers which set values with options" do
1862
# create a test warmer to pass to the cache
1963
TestUtils.create_warmer(:options_warmer, fn _ ->

0 commit comments

Comments
 (0)