У меня есть такой тип Enum.each
, который проходит через многие из них.
exids
|> Enum.each(fn (exid) ->
request_from_seaweedfs("#{@seaweedfs}/#{exid}/snapshots/recordings/", "Directories", "Name")
|> Enum.sort |> Enum.each(fn (year) ->
request_from_seaweedfs("#{@seaweedfs}/#{exid}/snapshots/recordings/#{year}/", "Directories", "Name")
|> Enum.sort |> Enum.each(fn (month) ->
request_from_seaweedfs("#{@seaweedfs}/#{exid}/snapshots/recordings/#{year}/#{month}/", "Directories", "Name")
|> Enum.sort |> Enum.each(fn (day) ->
request_from_seaweedfs("#{@seaweedfs}/#{exid}/snapshots/recordings/#{year}/#{month}/#{day}/", "Directories", "Name")
|> Enum.sort |> Enum.each(fn (hour) ->
request_from_seaweedfs("#{@seaweedfs}/#{exid}/snapshots/recordings/#{year}/#{month}/#{day}/#{hour}/?limit=3600", "Files", "name")
|> Enum.sort |> Enum.each(fn (file) ->
exist_on_seaweed?("/#{exid}/snapshots/recordings/#{year}/#{month}/#{day}/#{hour}/#{file}")
|> copy_or_skip("/#{exid}/snapshots/recordings/#{year}/#{month}/#{day}/#{hour}/#{file}")
save_current_directory(exid, year, month, day, hour, file)
end)
end)
end)
end)
end)
next_exid_index = Enum.find_index(exids, fn(x) -> x == exid end)
File.write!("#{@root_dir}/moving_old_data", "#{Enum.at(exids, next_exid_index + 1)}")
end)
Это очень длительный цикл, но он не обрабатывает логику остановки и возобновления.
Я попытался сохранить текущие данные в файл и возобновить их оттуда при перезагрузке как
exids
|> clean_already_completed(0)
|> Enum.each(fn (exid) ->
request_from_seaweedfs("#{@seaweedfs}/#{exid}/snapshots/recordings/", "Directories", "Name")
|> Enum.sort |> clean_already_completed(1) |> Enum.each(fn (year) ->
request_from_seaweedfs("#{@seaweedfs}/#{exid}/snapshots/recordings/#{year}/", "Directories", "Name")
|> Enum.sort |> clean_already_completed(2) |> Enum.each(fn (month) ->
request_from_seaweedfs("#{@seaweedfs}/#{exid}/snapshots/recordings/#{year}/#{month}/", "Directories", "Name")
|> Enum.sort |> clean_already_completed(3) |> Enum.each(fn (day) ->
request_from_seaweedfs("#{@seaweedfs}/#{exid}/snapshots/recordings/#{year}/#{month}/#{day}/", "Directories", "Name")
|> Enum.sort |> clean_already_completed(4) |> Enum.each(fn (hour) ->
request_from_seaweedfs("#{@seaweedfs}/#{exid}/snapshots/recordings/#{year}/#{month}/#{day}/#{hour}/?limit=3600", "Files", "name")
|> Enum.sort |> clean_already_completed(5) |> Enum.each(fn (file) ->
exist_on_seaweed?("/#{exid}/snapshots/recordings/#{year}/#{month}/#{day}/#{hour}/#{file}")
|> copy_or_skip("/#{exid}/snapshots/recordings/#{year}/#{month}/#{day}/#{hour}/#{file}")
save_current_directory(exid, year, month, day, hour, file)
end)
end)
end)
end)
end)
next_exid_index = Enum.find_index(exids, fn(x) -> x == exid end)
File.write!("#{@root_dir}/moving_old_data", "#{Enum.at(exids, next_exid_index + 1)}")
end)
, тогда как clean_already_completed
работает так.
defp clean_already_completed(list, index), do: get_recent_value(index) |> dont_reduce?(list)
defp dont_reduce?(nil, list), do: list
defp dont_reduce?(last, list), do: Enum.drop_while(list, fn el -> el != last end)
defp get_recent_value(index), do: read_recent_file() |> Enum.at(index)
defp read_recent_file, do: File.read("#{@root_dir}/moving_old_data") |> file_is_present()
defp file_is_present({:error, :enoent}), do: []
defp file_is_present({:ok, ""}), do: []
defp file_is_present({:ok, data}), do: data |> String.split(" ")
Но это не работает, как ожидалось, когдаЯ останавливаюсь и перезапускаю его, даже во время работы.Он пропускает следующий год, дни и месяцы.
В сохраненном файле у меня есть такие данные
1-granby-row 2017 03 18 05 43_49_000.jpg
Например, при сбое я просто хочу возобновить каждый цикл сэти значения.вышеприведенная строка объясняется как exid year month day hour file
.
Есть ли какой-нибудь возможный способ сделать это лучше?что из информации файла, если весь цикл начинается снова, возобновить его оттуда?