From 51bbf9f59db8b723b13c1a994309b06482b836b3 Mon Sep 17 00:00:00 2001 From: danilo silva Date: Wed, 10 Jun 2020 20:56:29 +0000 Subject: [PATCH] scheduling reloading of data from archive every day at 6.00 and 18.00 --- config/config.exs | 5 ++++ lib/openpod/application.ex | 3 +- lib/openpod/boundary/archive_reloader.ex | 9 ++++++ lib/openpod/boundary/archive_scheduler.ex | 3 ++ lib/openpod/boundary/archive_server.ex | 36 ++++++++++++++++++----- mix.exs | 1 + mix.lock | 3 ++ 7 files changed, 52 insertions(+), 8 deletions(-) create mode 100644 lib/openpod/boundary/archive_reloader.ex create mode 100644 lib/openpod/boundary/archive_scheduler.ex diff --git a/config/config.exs b/config/config.exs index 066768d..54bc159 100644 --- a/config/config.exs +++ b/config/config.exs @@ -29,6 +29,11 @@ config :logger, :console, # Use Jason for JSON parsing in Phoenix config :phoenix, :json_library, Jason +config :openpod, Openpod.Boundary.ArchiveScheduler, + jobs: [ + {"0 6-18 * * *", {Openpod.Boundary.ArchiveReloader, :reload, []}}, + ] + # Import environment specific config. This must remain at the bottom # of this file so it overrides the configuration defined above. import_config "#{Mix.env()}.exs" diff --git a/lib/openpod/application.ex b/lib/openpod/application.ex index 0cc4bfa..0522858 100644 --- a/lib/openpod/application.ex +++ b/lib/openpod/application.ex @@ -19,9 +19,10 @@ defmodule Openpod.Application do # Start the PubSub system {Phoenix.PubSub, name: Openpod.PubSub}, # Start the Endpoint (http/https) - OpenpodWeb.Endpoint + OpenpodWeb.Endpoint, # Start a worker by calling: Openpod.Worker.start_link(arg) # {Openpod.Worker, arg} + Openpod.Boundary.ArchiveScheduler, ] # See https://hexdocs.pm/elixir/Supervisor.html diff --git a/lib/openpod/boundary/archive_reloader.ex b/lib/openpod/boundary/archive_reloader.ex new file mode 100644 index 0000000..c65a87d --- /dev/null +++ b/lib/openpod/boundary/archive_reloader.ex @@ -0,0 +1,9 @@ +defmodule Openpod.Boundary.ArchiveReloader do + + @archive_supervisor :archive_supervisor + + def reload() do + DynamicSupervisor.which_children(@archive_supervisor) + |> Enum.each(fn {_, pid, _, _} -> Openpod.Boundary.ArchiveServer.reload(pid) end) + end +end diff --git a/lib/openpod/boundary/archive_scheduler.ex b/lib/openpod/boundary/archive_scheduler.ex new file mode 100644 index 0000000..c75976a --- /dev/null +++ b/lib/openpod/boundary/archive_scheduler.ex @@ -0,0 +1,3 @@ +defmodule Openpod.Boundary.ArchiveScheduler do + use Quantum, otp_app: :openpod +end diff --git a/lib/openpod/boundary/archive_server.ex b/lib/openpod/boundary/archive_server.ex index 838a920..ddc4147 100644 --- a/lib/openpod/boundary/archive_server.ex +++ b/lib/openpod/boundary/archive_server.ex @@ -30,27 +30,49 @@ defmodule Openpod.Boundary.ArchiveServer do GenServer.call(via(identifier), :get_feed) end + def reload(pid) when is_pid(pid) do + Logger.debug "starting reload..." + GenServer.cast(pid, :reload) + end + def reload(identifier) do cache_or_fetch(identifier) GenServer.call(via(identifier), :reload) end + def count(pid) when is_pid(pid) do + GenServer.call(pid, :count) + end + + def count(identifier) when is_binary(identifier) do + GenServer.call(via(identifier), :count) + end + ########## ### SERVER def init(identifier) do - {:ok, %{identifier: identifier, feed_data: fetch_feed_data(identifier)}} + {:ok, %{count: 1, identifier: identifier, feed_data: fetch_feed_data(identifier)}} end - def handle_call(:get_feed, _from, feed) do + def handle_call(:get_feed, _from, feed = %{count: count, feed_data: feed_data}) do Logger.debug "retrieve cached feed data for #{feed[:identifier]}..." - {:reply, feed[:feed_data], feed, Application.fetch_env!(:openpod, :cache_duration)} + {:reply, feed_data, %{feed | count: count + 1}, Application.fetch_env!(:openpod, :cache_duration)} end - def handle_call(:reload, _from, feed) do - Logger.debug "discard cached feed data for #{feed[:identifier]}..." - feed_data = fetch_feed_data(feed[:identifier]) - {:reply, feed_data, %{feed | feed_data: feed_data}} + def handle_call(:reload, _from, feed = %{count: count, identifier: identifier}) do + Logger.debug "discard cached feed data for #{identifier}..." + feed_data = fetch_feed_data(identifier) + {:reply, feed_data, %{feed | count: count + 1, feed_data: feed_data}} + end + + def handle_call(:count, _from, feed = %{identifier: identifier, count: count}) do + {:reply, %{identifier: identifier, count: count}, feed} + end + + def handle_cast(:reload, feed = %{identifier: identifier}) do + Logger.debug "discard cached feed data for #{identifier}..." + {:noreply, %{feed | feed_data: fetch_feed_data(identifier)}} end def handle_info(:timeout, %{identifier: identifier}) do diff --git a/mix.exs b/mix.exs index 946a21e..a1d3322 100644 --- a/mix.exs +++ b/mix.exs @@ -48,6 +48,7 @@ defmodule Openpod.MixProject do {:calendar, "~> 1.0.0"}, {:hackney, "~> 1.15"}, {:html_entities, "~> 0.3"}, + {:quantum, "~> 3.0-rc"}, {:observer_cli, "~> 1.5"}, ] ++ deps_dev() ++ deps_release() end diff --git a/mix.lock b/mix.lock index 6d5eb26..75392a8 100644 --- a/mix.lock +++ b/mix.lock @@ -6,6 +6,7 @@ "cowboy": {:hex, :cowboy, "2.7.0", "91ed100138a764355f43316b1d23d7ff6bdb0de4ea618cb5d8677c93a7a2f115", [:rebar3], [{:cowlib, "~> 2.8.0", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "~> 1.7.1", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm", "04fd8c6a39edc6aaa9c26123009200fc61f92a3a94f3178c527b70b767c6e605"}, "cowlib": {:hex, :cowlib, "2.8.0", "fd0ff1787db84ac415b8211573e9a30a3ebe71b5cbff7f720089972b2319c8a4", [:rebar3], [], "hexpm", "79f954a7021b302186a950a32869dbc185523d99d3e44ce430cd1f3289f41ed4"}, "credo": {:hex, :credo, "1.4.0", "92339d4cbadd1e88b5ee43d427b639b68a11071b6f73854e33638e30a0ea11f5", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "1fd3b70dce216574ce3c18bdf510b57e7c4c85c2ec9cad4bff854abaf7e58658"}, + "crontab": {:hex, :crontab, "1.1.10", "dc9bb1f4299138d47bce38341f5dcbee0aa6c205e864fba7bc847f3b5cb48241", [:mix], [{:ecto, "~> 1.0 or ~> 2.0 or ~> 3.0", [hex: :ecto, repo: "hexpm", optional: true]}], "hexpm", "1347d889d1a0eda997990876b4894359e34bfbbd688acbb0ba28a2795ca40685"}, "dialyxir": {:hex, :dialyxir, "1.0.0", "6a1fa629f7881a9f5aaf3a78f094b2a51a0357c843871b8bc98824e7342d00a5", [:mix], [{:erlex, ">= 0.2.6", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "aeb06588145fac14ca08d8061a142d52753dbc2cf7f0d00fc1013f53f8654654"}, "distillery": {:hex, :distillery, "2.1.1", "f9332afc2eec8a1a2b86f22429e068ef35f84a93ea1718265e740d90dd367814", [:mix], [{:artificery, "~> 0.2", [hex: :artificery, repo: "hexpm", optional: false]}], "hexpm", "bbc7008b0161a6f130d8d903b5b3232351fccc9c31a991f8fcbf2a12ace22995"}, "elixir_xml_to_map": {:hex, :elixir_xml_to_map, "1.0.1", "8eaac89644e033f472e19f66a88288a3e36a621cf695a7fd323a1dc481b15c15", [:mix], [{:erlsom, "~>1.4", [hex: :erlsom, repo: "hexpm", optional: false]}], "hexpm", "630e61fc23496c0981e6e605f5505bd477d29c2449d929453e1a762aa364fa99"}, @@ -13,6 +14,7 @@ "erlsom": {:hex, :erlsom, "1.5.0", "c5a5cdd0ee0e8dca62bcc4b13ff08da24fdefc16ccd8b25282a2fda2ba1be24a", [:rebar3], [], "hexpm", "55a9dbf9cfa77fcfc108bd8e2c4f9f784dea228a8f4b06ea10b684944946955a"}, "excoveralls": {:hex, :excoveralls, "0.12.3", "2142be7cb978a3ae78385487edda6d1aff0e482ffc6123877bb7270a8ffbcfe0", [:mix], [{:hackney, "~> 1.0", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "568a3e616c264283f5dea5b020783ae40eef3f7ee2163f7a67cbd7b35bcadada"}, "file_system": {:hex, :file_system, "0.2.8", "f632bd287927a1eed2b718f22af727c5aeaccc9a98d8c2bd7bff709e851dc986", [:mix], [], "hexpm", "97a3b6f8d63ef53bd0113070102db2ce05352ecf0d25390eb8d747c2bde98bca"}, + "gen_stage": {:hex, :gen_stage, "1.0.0", "51c8ae56ff54f9a2a604ca583798c210ad245f415115453b773b621c49776df5", [:mix], [], "hexpm", "1d9fc978db5305ac54e6f5fec7adf80cd893b1000cf78271564c516aa2af7706"}, "gettext": {:hex, :gettext, "0.18.0", "406d6b9e0e3278162c2ae1de0a60270452c553536772167e2d701f028116f870", [:mix], [], "hexpm", "c3f850be6367ebe1a08616c2158affe4a23231c70391050bf359d5f92f66a571"}, "hackney": {:hex, :hackney, "1.15.2", "07e33c794f8f8964ee86cebec1a8ed88db5070e52e904b8f12209773c1036085", [:rebar3], [{:certifi, "2.5.1", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "6.0.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "1.0.1", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~>1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "1.1.5", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm", "e0100f8ef7d1124222c11ad362c857d3df7cb5f4204054f9f0f4a728666591fc"}, "html_entities": {:hex, :html_entities, "0.5.1", "1c9715058b42c35a2ab65edc5b36d0ea66dd083767bef6e3edb57870ef556549", [:mix], [], "hexpm", "30efab070904eb897ff05cd52fa61c1025d7f8ef3a9ca250bc4e6513d16c32de"}, @@ -32,6 +34,7 @@ "plug": {:hex, :plug, "1.10.1", "c56a6d9da7042d581159bcbaef873ba9d87f15dce85420b0d287bca19f40f9bd", [:mix], [{:mime, "~> 1.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: true]}], "hexpm", "b5cd52259817eb8a31f2454912ba1cff4990bca7811918878091cb2ab9e52cb8"}, "plug_cowboy": {:hex, :plug_cowboy, "2.2.1", "fcf58aa33227a4322a050e4783ee99c63c031a2e7f9a2eb7340d55505e17f30f", [:mix], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:plug, "~> 1.7", [hex: :plug, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "3b43de24460d87c0971887286e7a20d40462e48eb7235954681a20cee25ddeb6"}, "plug_crypto": {:hex, :plug_crypto, "1.1.2", "bdd187572cc26dbd95b87136290425f2b580a116d3fb1f564216918c9730d227", [:mix], [], "hexpm", "6b8b608f895b6ffcfad49c37c7883e8df98ae19c6a28113b02aa1e9c5b22d6b5"}, + "quantum": {:hex, :quantum, "3.0.0-rc.3", "2a42d9c7abc8e349bff2baf6f66a3cab09ac4e907e4f0ffc088c9f27718e8f44", [:mix], [{:crontab, "~> 1.1", [hex: :crontab, repo: "hexpm", optional: false]}, {:gen_stage, "~> 0.14 or ~> 1.0", [hex: :gen_stage, repo: "hexpm", optional: false]}], "hexpm", "2c8a3a8783e28d99bd6fd0e380ef6835ae0e42b31bda151d6ab4a97afa84059a"}, "ranch": {:hex, :ranch, "1.7.1", "6b1fab51b49196860b733a49c07604465a47bdb78aa10c1c16a3d199f7f8c881", [:rebar3], [], "hexpm", "451d8527787df716d99dc36162fca05934915db0b6141bbdac2ea8d3c7afc7d7"}, "recon": {:hex, :recon, "2.5.1", "430ffa60685ac1efdfb1fe4c97b8767c92d0d92e6e7c3e8621559ba77598678a", [:mix, :rebar3], [], "hexpm", "5721c6b6d50122d8f68cccac712caa1231f97894bab779eff5ff0f886cb44648"}, "ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.5", "6eaf7ad16cb568bb01753dbbd7a95ff8b91c7979482b95f38443fe2c8852a79b", [:make, :mix, :rebar3], [], "hexpm", "13104d7897e38ed7f044c4de953a6c28597d1c952075eb2e328bc6d6f2bfc496"},