<commit_before>defmodule WhistlerNewsReader.FeedRefresher do

  alias WhistlerNewsReader.FeedFetcherWorker
  alias WhistlerNewsReader.StoreEntryHelper
  alias WhistlerNewsReader.Feed
  alias WhistlerNewsReader.Repo

  require Logger

  def refresh(feed) do
    Logger.info "FeedRefresher - START refreshing feed id: #{feed.id}, title: #{feed.title}"

    result = with {:ok, json_body}     <- FeedFetcherWorker.fetch(feed.feed_url),
         {:ok, parsed_attrs}  <- ElixirFeedParser.parse(json_body),
         {:ok, _updated_feed} <- update_last_refreshed_at(feed),
         do: StoreEntryHelper.store_entries(feed, parsed_attrs.entries)

    Logger.info "FeedRefresher - DONE refreshing feed id: #{feed.id}, title: #{feed.title}"
    result
  end

  defp update_last_refreshed_at(feed) do
    feed
    |> Feed.changeset(%{last_refreshed_at: Ecto.DateTime.utc})
    |> Repo.update
  end
end<commit_msg>refactor(feed refresh): skip with statements to save memory<commit_after>defmodule WhistlerNewsReader.FeedRefresher do

  alias WhistlerNewsReader.FeedFetcherWorker
  alias WhistlerNewsReader.StoreEntryHelper
  alias WhistlerNewsReader.Feed
  alias WhistlerNewsReader.Repo

  require Logger

  def refresh(feed) do
    Logger.info "FeedRefresher - START refreshing feed id: #{feed.id}, title: #{feed.title}"

    # Try not using with statement or nested case statements
    # Since this will blow up memory
    {:ok, json_body} = FeedFetcherWorker.fetch(feed.feed_url)
    {:ok, parsed_attrs} = ElixirFeedParser.parse(json_body)
    {:ok, _updated_feed} = update_last_refreshed_at(feed)
    StoreEntryHelper.store_entries(feed, parsed_attrs.entries)

    Logger.info "FeedRefresher - DONE refreshing feed id: #{feed.id}, title: #{feed.title}"
  end

  defp update_last_refreshed_at(feed) do
    feed
    |> Feed.changeset(%{last_refreshed_at: Ecto.DateTime.utc})
    |> Repo.update
  end
end
{'fraction_non_alphanumeric': 0.09667194928684628, 'fraction_numerical': 0.0, 'mean_word_length': 4.396011396011396, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2986287', 'n_tokens_mistral': 642, 'n_tokens_neox': 617, 'n_words': 135}