retry write file
This commit is contained in:
1 parent
672d74732c
commit
a4ead1f7ba
2 files changed
+10
-2
No files matched your search
@@ -126,11 +126,14 @@ defmodule FeedReader.Collector do
|
|||||||
path =
|
path =
|
||||||
Path.join(System.tmp_dir!(), "feed_reader-parse-#{System.unique_integer([:positive])}.xml")
|
Path.join(System.tmp_dir!(), "feed_reader-parse-#{System.unique_integer([:positive])}.xml")
|
||||||
|
|
||||||
case File.open(path, [:write, :binary]) do
|
case File.open(path, [:write, :binary, :exclusive]) do
|
||||||
{:ok, file} ->
|
{:ok, file} ->
|
||||||
File.close(file)
|
File.close(file)
|
||||||
{:ok, path}
|
{:ok, path}
|
||||||
|
|
||||||
|
{:error, :eexist} ->
|
||||||
|
temporary_path()
|
||||||
|
|
||||||
{:error, reason} ->
|
{:error, reason} ->
|
||||||
{:error, reason}
|
{:error, reason}
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -228,7 +228,11 @@ defmodule FeedReader.Downloader do
|
|||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
defp open_temp_file do
|
defp open_temp_file(attempts \\ 5)
|
||||||
|
|
||||||
|
defp open_temp_file(0), do: {:error, :eexist}
|
||||||
|
|
||||||
|
defp open_temp_file(attempts) do
|
||||||
path =
|
path =
|
||||||
Path.join(
|
Path.join(
|
||||||
System.tmp_dir!(),
|
System.tmp_dir!(),
|
||||||
@@ -237,6 +241,7 @@ defmodule FeedReader.Downloader do
|
|||||||
|
|
||||||
case File.open(path, [:write, :binary, :exclusive]) do
|
case File.open(path, [:write, :binary, :exclusive]) do
|
||||||
{:ok, file} -> {:ok, path, file}
|
{:ok, file} -> {:ok, path, file}
|
||||||
|
{:error, :eexist} -> open_temp_file(attempts - 1)
|
||||||
error -> error
|
error -> error
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in new issue
Block a user